I searched and tried many examples unable to solve my hijri date is like,
19/07/1440
I tried this query
SELECT TOP 200
DATEPART(YEAR, EndDateHejri)
FROM
student
but I'm getting this error
Conversion failed when converting date and/or time from character string
I'm unable to solve error - hoping for your suggestions
5条答案
按热度按时间yshpjwxd1#
I bit of Google-Fu and format 131 should help you convert Hijri dates into Gregorian Dates...
Unfortunately, all the date functions (
DATEPART()
,DATENAME()
, evenDATEADD()
, etc) are all based on manipulating Gregorian dates. So you can't use them.So, you're forced to use string manipulation.
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=901209ae0cdcf38cdcdea8afad4fd034
c0vxltue2#
Posting a different answer. As the OP is only after the year part, and they've stated that it's always in the format
00/00/yyyy
why not just useRIGHT
? So:z9smfwbn3#
I tried answer @Vishnu Chandel it's working for me .
And full code is :
nfg76nw04#
Please try below code to get the correct output.
8fq7wneg5#
SELECT TOP 200
CAST(FORMAT(EndDateHejri,'yyyy','ar') as int) [EndDateHejriYear] FROM student