How to save Hijri dates in sql server tables? What type of column?

后端 未结 4 827
无人及你
无人及你 2021-01-18 18:21

How to save Hijri dates (0000/01/01-9999/01/01) in Microsoft SQL Server tables? What type of column?
I choose datetime2, is it right?

CRE         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-18 18:42

    I dont know why people downvote this but if you have ever written a program that interacts with Persian dates and you need to generate reports based on dates and times you will understand why you should store it as pure string value. sql server lacks the ability to convert UTC to different time zones and you should use clr to do that and many hosting environment does not allow you to do that

    I recommend you to store them in fixed format like yyyy/MM/dd HH:mm:ss in char(19) column if you ever want to have reports based on Hijri date, also store the UTC time in another column for further uses or globalization.

    If you store them with the pattern I mentioned above you can compare them easily and also get specific part of the date time (year, hour, ...) just by extracting part of the value by using SUBSTRING

    Keep in mind that you can always store the UTC time with time zone in your database but my experience shows that you will have difficulties with reports soon or later.

    Another point is that never try to store Hijri dates in DateTimes because you can not do that. for example 1394/02/31 is a Hijri date but you can not have 31st day of February in DateTime.

    Assume you want to generate report of items that are created in each day. if you store the date just as UTC you must to convert each date time to Hijri by specific time zone difference and daylight time saving and then group by it and you will face huge performance degradation. For example try to calculate the report for 1393/12/20 to 1394/01/10 and then you will understand why not use UTC date times for this kind of reporting stuffs.

提交回复
热议问题