You have a problem with Hijri dates and SQL Server?
Let’s first discuss the problem. We use Hijri calendar and SQL server uses Gregorian calendar. And SQL server has restriction on the Gregorian dates earlier than 1753.
But SQL server saves dates internally in another format (It’s neutral and doesn’t depend on any calendar)
So we need to convert the dates directly from SQL Server internal format to Hijri calendar format.
To accomplish this we will use the convert function to convert the dates into Hijri calendar and save it into nvarchar column. This will solve the problem of entering the year 1425 (current year in Hijri) and also will give you the ability to save the Hijri dates in Arabic names.
But we still have the problem of sorting!! So we need to add the convert statement in the order by statement to force SQL Server to deal with our dates as dates.
This is the theoretical part. Let’s have example.
First we will build test table with this script
/************For demonstration purpose only and MUST be tested before usage **********************/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[test]
GO
CREATE TABLE [dbo].[test] (
[date] [nvarchar] (50) COLLATE Arabic_CI_AI NULL
) ON [PRIMARY]
GO
/**********************************************************************************/
To add dates to our table use this script
/************For demonstration purpose only and MUST be tested before usage **********************/
insert test
values(convert(nchar(30),getdate(),131))
/**********************************************************************************/
This script will add the Hijri date as 8/01/1425 & If you want to add it in the format 8 محرم 1425 (Arabic months names) use this script
/************For demonstration purpose only and MUST be tested before usage **********************/
insert test
values(convert(nchar(30),getdate(),130))
/**********************************************************************************/
Ok. What’s next?
We need to sort these dates in Ascending or descending order so we have to reconvert these character data into dates again
/************For demonstration purpose only and MUST be tested before usage **********************/
select * from test order by convert(datetime,[date],131)
/**********************************************************************************/
Have fun with dates,
Mohamed
Comments
Anonymous
September 22, 2005
If you want to store dates independently of calendar format and preserve ordering then bigint might be what you want.
If using .Net you can just store the .Ticks property and then recreate your date by passing the .tick into the constructor.
Let's you put the date into the database in one calendar and pull it out in a totally different one.
[)amienAnonymous
February 22, 2006
Very useful blog. Thank you.Anonymous
October 31, 2006
What is your name? Where do you live?Anonymous
January 18, 2007
The site's very professional! Keep up the good work! <a href= http://docs.google.com/View?docid=df2wwh2p_7c7mc89 >Debt Consolidation</a> [url=http://docs.google.com/View?docid=df2wwh2p_7c7mc89]Debt Consolidation[/url] Best RegardsAnonymous
January 19, 2007
Yo! Cool stuff! Thanks for being here. Please also visit my site: <a href= http://fm7.biz/0l6t >ephedra</a> [url=http://fm7.biz/0l6t]ephedra[/url]Anonymous
February 12, 2007
Hi! <a href= thanks for sharing ></a> [url=thanks for sharing][/url]Anonymous
February 16, 2007
Hi! <a href= e44e1a258640a70d949440fd55590ee8 ></a> [url=e44e1a258640a70d949440fd55590ee8 ][/url]Anonymous
March 09, 2007
i did all things ,but there is problems and i cannot find explanation for it, after i have convert the date Hajji for example today date i got the equivalent hijjire date but i found it, less than the right Hijri date by one date , though i have configure the regional setting and the date right in the windows date-time window. I developed asp.net to convert from Georgian to Hijri but the Hijri date also less than the current Hijri by one day I took the same code on windows application and it give me the right dateAnonymous
April 18, 2007
The Best Catalog. <a href=http://healthpiece.info/>Real">http://healthpiece.info/>Real Catalog</a>[url=http://healthpiece.info/]The Real Catalog[/url]Anonymous
April 27, 2007
<a href= http://itemcontent.info/travel-leisure/page_1.html >travel leisure</a> [url=http://itemcontent.info/travel-leisure/page_1.html]travel leisure[/url] <a href= http://itemcontent.info/beauty/page_1.html >Beauty</a> [url=http://itemcontent.info/beauty/page_1.html]Beauty[/url] <a href= http://itemcontent.info/wedding-marriage/page_1.html >Wedding marriage</a> [url=http://itemcontent.info/wedding-marriage/page_1.html]Wedding marriage[/url] <a href= http://itemcontent.info/blogs/page_1.html >blogs</a> [url=http://itemcontent.info/blogs/page_1.html]blogs[/url]Anonymous
May 24, 2007
<a href="http://extremedrawing.tripod.com/">adult comics</a>Anonymous
May 24, 2007
<a href="http://extremedrawing.tripod.com/">adult comics</a>Anonymous
August 14, 2007
Let’s first discuss the problem. We use Hijri calendar and SQL server uses Gregorian calendar. And SQL server has restriction on the Gregorian dates earlier than 1753. I do not agree. Go to http://apartments.waw.pl/Anonymous
September 25, 2008
i have a problem with hijri date and i need a help