Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mirsaeedi/sqlclr-jalali-date-utility

Includes several functions to make it easier for you to work with Persian dates in SQL Server (2005+)
https://github.com/mirsaeedi/sqlclr-jalali-date-utility

calendar gregorian jalali jalali-calendar jalalidate shamsi sql sql-clr sql-server sqlcmd-mode

Last synced: 19 days ago
JSON representation

Includes several functions to make it easier for you to work with Persian dates in SQL Server (2005+)

Awesome Lists containing this project

README

        

# 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.

* Support .Net Date and Time Formatting (More Information at this page: [Custom Date and Time Format Strings](https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) ).
* Developed with C# (SQL CLR)

## 💡 Installation

* Make sure you have .NET framework 4.7.2+ installed on your server.

* Since this library has beed developed using SQL CLR, firstly you need to configure your SQL Server by [Enabling CLR Integration In SQL Server](https://msdn.microsoft.com/en-us/library/ms131048.aspx)

### SQL Server 2005-2014
```sql
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
```
### SQL Server 2017+
```sql
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
```

* Run the [Deployment Script](https://github.com/mirsaeedi/SQLCLR-Jalali-Date-Utility/blob/master/SqlCLR.JalaliDateUtility/Dist/DeploymentScript.sql) in SQL Server Management Studio to generate the Jalali functions in your database.

* Note that you have to run this script in SQLCMD Mode. So in SQL Management Studio, Select _Query Menu_, and then select _SQLCMD Mode_. Now you will be able to execute the query.

* Please be sure that you have changed the _[your-database-name]_ inside the [Deployment Script](https://github.com/mirsaeedi/SQLCLR-Jalali-Date-Utility/blob/master/SqlCLR.JalaliDateUtility/Dist/DeploymentScript.sql) to your database's name.

## 🕹 Usage

###### ★ Convert Gregorian To Jalali:

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

```sql
select dbo.GregorianToJalali(GETDATE(),'yy') -- returns 95
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy') -- returns 1395
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy-MM') -- returns 1395-07
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy-M') -- returns 1395-7
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd') -- returns 1395/07/01
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd hh:mm tt') -- returns 1395/07/01 03:04 ب ظ
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd hh:mm:ss tt') -- returns 1395/07/01 03:04:33 ب ظ
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy/MM/dd HH:mm') -- returns 1395/07/01 15:04
```

```sql
select dbo.GregorianToJalali(GETDATE(),'yyyy MMMM dddd') -- returns 1395 پنج شنبه مهر
```

###### ★ Convert Jajali Date To Gregorian:

```sql
select dbo.JalaliToGregorian('95-06-11','-') --returns 2016-09-01 00:00:00.000
```

```sql
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)

```sql
select dbo.GetJalaliLastDayOfMonth(GETDATE()) --returns 2016-10-21 00:00:00.000 which is equal to 1395/07/30
```

```sql
select dbo.GetJalaliFirstDayOfMonth(GETDATE()) --returns 2016-09-22 00:00:00.000 which is equal to 1395/07/01
```