Search code examples
t-sqldatehijri

Convert Hijri Shamsi date format tsql


I want convert my Hijri Shamsi date '92/2/3' to this format : '92/02/03'

With this code, I get this error :The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

Declare @Str Varchar(10) = '92/2/3'
Select Convert(Varchar(10), Convert(DateTime, @Str), 111)

How can i change format of date ?


Solution

    1. Since you don't have centuries you should use 11, not 111 as your conversion code
    2. You need to use 11 in the inner convert to tell it what format it is converting FROM

      Declare @Str Varchar(10) = '92/2/3'    
      Select Convert(Varchar(10), Convert(DateTime, @Str,11), 11)