Home

Awesome

SQL CLR Jalali Date Time Utility

The aim of this project is to make it more easier for developers to work with jalali (shamsi - شمسی) dates.

💡 Installation

SQL Server 2005-2014

sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
sp_configure 'clr enabled', 1;  
GO
RECONFIGURE;  
GO  

SQL Server 2017+

sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
sp_configure 'clr enabled', 1;  
GO
EXEC sp_configure 'clr strict security',0;
GO
RECONFIGURE;  
GO  

🕹 Usage

★ Convert Gregorian To Jalali:

Suppose that GETDATE() Method in sql server returns 2016/09/22 15:04:33

select dbo.GregorianToJalali(GETDATE(),'yy') -- returns 95
select dbo.GregorianToJalali(GETDATE(),'yyyy') -- returns 1395
select dbo.GregorianToJalali(GETDATE(),'yyyy-MM') -- returns 1395-07
select dbo.GregorianToJalali(GETDATE(),'yyyy-M') -- returns 1395-7
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd') -- returns 1395/07/01
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd hh:mm tt') -- returns 1395/07/01 03:04 ب ظ
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd hh:mm:ss tt') -- returns 1395/07/01 03:04:33 ب ظ
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd HH:mm') -- returns 1395/07/01 15:04
select dbo.GregorianToJalali(GETDATE(),'yyyy MMMM dddd') -- returns 1395 پنج شنبه مهر
★ Convert Jajali Date To Gregorian:
select dbo.JalaliToGregorian('95-06-11','-') --returns 2016-09-01 00:00:00.000
select dbo.JalaliToGregorian('1395/06/11','/') --returns 2016-09-01 00:00:00.000
★ Some times you need to have the first and last day of a persian month in gregorian date (specially in reporting)
select dbo.GetJalaliLastDayOfMonth(GETDATE()) --returns 2016-10-21 00:00:00.000 which is equal to 1395/07/30
select dbo.GetJalaliFirstDayOfMonth(GETDATE()) --returns 2016-09-22 00:00:00.000 which is equal to 1395/07/01