Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting GETDATE() to Hijri date to yyyymmdd

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)

like image 650
Hussein ALSHAMIRI Avatar asked Mar 14 '17 12:03

Hussein ALSHAMIRI


People also ask

What format is Getdate ()?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the CURRENT_TIMESTAMP function.

How do I change the date format of a column in SQL?

You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')


1 Answers

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
like image 187
Gordon Linoff Avatar answered Sep 28 '22 00:09

Gordon Linoff