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)
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)
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
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
Use below query :
SELECT FORMAT ( GETDATE(), 'yyyy/MM/dd', 'ar-SA' )
FORMAT
function was introduced with SQL Server 2012
and as pointed in the docs it is available in all supported versions. –
Grandfather 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 |
+--------------+--------------+
Try with 120:
SELECT CONVERT(VARCHAR(10), GETDATE(), 120)
You can do that using the following query:
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120),'-','/')
© 2022 - 2024 — McMap. All rights reserved.