Click here to Skip to main content
15,314,282 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
i have converted the date from Gregorian to hijri by using this code
SQL
DECLARE @DateTime AS DATETIME
       SET @DTime=GETDATE()
       SELECT @DTime AS [Gregorian Date]
       ,CONVERT(VARCHAR(11),@DTime,131) as [hijri]


i want only converted year so that i can use it in following query .
Posted

Use SUBSTRING[^] to get the year from the result of the CONVERT[^].
SQL
DECLARE @DateTime AS DATETIME 
SET @DateTime=GETDATE( )
SELECT @DateTime AS [Gregorian Date],
       SUBSTRING( CONVERT( VARCHAR(11), @DateTime, 131 ),7 , 4) AS [hijri]
   
Comments
$ultaNn 28-Oct-13 6:21am
   
Thank You
Joezer BH 22-Dec-13 5:06am
   
5ed!
I think we can use YEAR() in case if we are dealing with dates.

SQL
DECLARE @DateTime AS DATETIME

SET @DateTime = GETDATE()

SELECT @DateTime AS [Gregorian Date]
	, CONVERT(VARCHAR(11), @DateTime, 131) AS [hijri]
	, YEAR(CAST(CONVERT(VARCHAR(11), @DateTime, 131) AS DATE)) AS [Hijri_Year]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900