8

I am trying to get the Hijri GETDATE() and convert it into this format yyyymmdd

I have already tried this script

SELECT CONVERT(VARCHAR(10), GETDATE(), 131)  

but it gives me this format ( 16/06/1438 ) and what I actually need is (1438/06/16)

mohsen mashhadi
  • 201
  • 2
  • 8
  • This is very well documented in the [SO Documentation](http://stackoverflow.com/documentation/sql-server/1471/dates#t=201703141241406432461) – Siyual Mar 14 '17 at 12:42
  • There's no built in conversion to get what you're asking for - you'd need to assemble it yourself using lots of string mangling - or, if possible, do this formatting in another layer that hopefully has better calendar/localization/formatting tools available than those found in T-SQL. – Damien_The_Unbeliever Mar 14 '17 at 12:42
  • Depending on your SQL Server version (tag it please), you might be able to use Format – Nick.Mc Mar 14 '17 at 13:21
  • It's already solved and I'm using SQL2012 @Nick.McDermaid – Hussein ALSHAMIRI Mar 15 '17 at 12:46
  • The usual thing to do now is post your solution so that everyone can benefit, or at least give the answers below some closure. – Nick.Mc Mar 15 '17 at 12:48

6 Answers6

3

SQL Server does not offer a wealth of formatting options for such dates, so just construct it yourself:

SELECT (RIGHT(CONVERT(VARCHAR(10), GETDATE(), 131), 4) + '/' +
        CONVERT(VARCHAR(5), GETDATE(), 131)
       ) as hj_yyyymmdd

Oops. Right idea, wrong implementation:

SELECT (RIGHT(CONVERT(VARCHAR(10), GETDATE(), 131), 4) +
        SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 131), 3, 4) +
        LEFT(CONVERT(VARCHAR(10), GETDATE(), 131), 2)
   ) AS hj_yyyymmdd
Venu
  • 455
  • 2
  • 7
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

If you are having SQL Server 2012 and above,

SELECT FORMAT(GETDATE()+1,'yyyy/MM/dd','ar')

It will give you the below result for the date 2017/03/14

1438/06/16
Venu
  • 455
  • 2
  • 7
1

Use below query :

SELECT FORMAT ( GETDATE(), 'yyyy/MM/dd', 'ar-SA' )
KristofMols
  • 3,487
  • 2
  • 38
  • 48
Mansoor
  • 4,061
  • 1
  • 17
  • 27
  • The ‍‍‍```FORMAT``` function was introduced with ```SQL Server 2012``` and as pointed in the docs it is available in all supported versions. – Sina Jul 17 '23 at 10:09
1

Another option is to convert to varchar then date then varchar again.

Using format() with 'ar-SA' seems to return 1 day earlier than using convert() with style 131.

select Method='multiconvert'
  ,conversion = convert(varchar(10)
    ,convert(date,convert(varchar(12),getdate(),131),103),112)
union all 
select 'format'
  , format ( getdate(), 'yyyyMMdd', 'ar-SA' )
union all
select 'style131'
  ,convert(varchar(12),getdate(),131)

rextester demo: http://rextester.com/LIX82417

returns

+--------------+--------------+
|    Method    |  conversion  |
+--------------+--------------+
| multiconvert | 14380616     |
| format       | 14380615     |
| style131     | 16/06/1438   |
+--------------+--------------+
SqlZim
  • 37,248
  • 6
  • 41
  • 59
0

Try with 120:

SELECT CONVERT(VARCHAR(10), GETDATE(), 120) 
Carlos
  • 1,638
  • 5
  • 21
  • 39
0

You can do that using the following query:

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120),'-','/')
KristofMols
  • 3,487
  • 2
  • 38
  • 48
udhaya kumar
  • 161
  • 6