Converting getdate () to hijri dates from yyyymmdd

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

I already tried this script

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

but it gives me this format (16/06/1438) and I really need it (1438/06/16)

+6
source share
6 answers

SQL Server does not offer many formatting options for such dates, so just create one yourself:

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

Unfortunately. The right idea, the 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
+3
source

If you have SQL Server 2012 or higher,

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

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

1438/06/16
+1
source

- convert varchar, date, varchar .

format() with 'ar-SA', , 1 , convert() 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)

Demo version of rexter: http://rextester.com/LIX82417

returns

+--------------+--------------+
|    Method    |  conversion  |
+--------------+--------------+
| multiconvert | 14380616     |
| format       | 14380615     |
| style131     | 16/06/1438   |
+--------------+--------------+
+1
source

Try using 120:

SELECT CONVERT(VARCHAR(10), GETDATE(), 120) 
0
source

Use the following query:

SELECT FORMAT ( GETDATE(), 'yyyy/MM/dd', 'ar-SA' )
0
source

You can do this using the following query:

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120),'-','/')
0
source

Source: https://habr.com/ru/post/1015728/


All Articles