How to display only Hijri Year in SQL Server?

I only want to show the year of Hijri. How to do it in SQL SERVER

1.When I try to convert Hijri to Gregorian using GETDATE (). It displays the output correctly

SELECT CONVERT(nchar, GETDATE(), 131)
output
------
18/10/1438  4:02:48:407PM     

SELECT CONVERT(nchar, '2016-10-02 12:00:00.000', 131)
Output
------
2016-10-02 12:00:00.000       

      

  1. I want to show the Hijri year only as we show only the Gregorian year as shown below.
select year(GETDATE())
output
------
2017

**Expecting output**
1438 (if I give Gregorian Date, it should display only Gregorian Year)

      

+3


source to share


3 answers


Example

select right(convert(nvarchar(10), getdate(), 131),4) 

      

Returns



1438

      

You can specify the final result as INT if you like

+3


source


I think this is a better solution than substring



DECLARE @DateTime AS DATETIME
SET @DateTime=GETDATE()
SELECT 
    FORMAT(@DateTime,'yyyy','ar')AS [Gregorian date to Hijri date]

output
-------
1438

      

+2


source


you can use substring

like

select substring(CONVERT(varchar, GETDATE(),131), 7, 4)

      

Output:

1438

      

+1


source







All Articles