CLR Function & Time Zone in SQL Server

Businesses logic has been running behind applications, tools, and software that makes their lives easier by reducing runtime, accelerating productivity hours, and decreasing man-hours. It is imperative for businesses to maintain large databases yet make it simpler to fetch and utilize the database. In SQL Server, UDF (User-defined function) is a programming language that accepts multiple parameters and utilizes the same approved parameters to deliver a specific outcome. The .NET with SQL CLR Server allows any of the .NET languages to be used to create database objects such as CLR stored procedures, user-defined SQL functions, triggers, aggregate functions, and user-defined types.

SQL Server CLR also enables you to enhance the SQL Server database engine’s capabilities Microsoft .NET framework’s programming language like C# or VB.Net.

1. User-Defined SQL CLR Function

Following are steps to create a new Common Language Runtime -CLR User Designed function.

  1. To start with, Create a new SQL-Server Database Project in Visual Studio.
  2. Right click on the Project button and select Add->New Item->SQL CLR C# to create a new Scalar value SQL CLR function.
    Add SQL CLR C# User Defined Function
  3. It will then open a New File for writing function in C#.
    Write SQL CLR Function in C#

We can write C# code here, and furthermore we can add required DLLs reference. and deploy. We can also add third-party DLL and deploy third party DLL in the C# code.

  • After writing the function, from the solution explorer, right-click on the SQL Server project. Select the Publish option.
    Publish Database

Now select the target database project, then click on Publish or Generate Script option.

  • This Function will create the database and is easily seen in the SQL Server Management Studio.
    Function in SQL Server Management Studio
  • The SQL Function has no difference and is similar to SQL statement that users execute normally.
    Function Execution & Output

2. Time Zone and Daylight-Saving Time

SYSDATETIMEOFFSET() Function returns current local date, time & time zone offset value. Time zone offset values is represented as [+|-] hh:mm (E.g. for India, Its +05:30). However, from this value, the time zone name is not identified. For example, the time zones of India and Sri Lanka have the same offset value. Similarly, using this function, one cannot identify that this is Daylight Saving Time (DST) or not.

To get these values, the following CLR functions can be helpful.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
///
/// Check current time is in Daylight Saving time
///
/// If current time zone is daylight saving, then return true else false
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlBoolean IsDayLightSavingTime()
{
return TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now);
}
///
/// Get Current Time Zone Name
///
/// Time Zone Name
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString TimeZoneName()
{
//If current time is daylight saving , then return day light name , else it will return Standard Name of time zone.
if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now))
return TimeZone.CurrentTimeZone.DaylightName;
else
return TimeZone.CurrentTimeZone.StandardName;
}
};
TimeZone

Notes: CLR function might not be available in SQL Server in default options.

Software development companies can enable change by executing the following scripts. However, this command is not recommended. To allow the use of CLR objects, the SQL Server CLR integration should be enabled (Note: By default, SQL Server CLR integration is disabled.) and developers should use the Asymmetric key or certificate.

EXEC sp_configure 'show advanced options', 1;

RECONFIGURE;

EXEC sp_configure 'clr strict security', 0;

RECONFIGURE;

EXEC sp_configure 'clr enabled', 1;

RECONFIGURE;

3. Conclusion

With this blog, Now it would be easy to fetch and analyze data using SQL in Visual Studio. A comprehensive understanding of how queries work, how to use user-defined SQL CLR functions, using time zone functions to automatically generate time. As explained in the above example of the CLR function, users can now create user-defined functions in C# or VB.Net and take advantage of .NET development services and third-party DLLs.

profile-image
Vishal Shah

Vishal Shah has an extensive understanding of multiple application development frameworks and holds an upper hand with newer trends in order to strive and thrive in the dynamic market. He has nurtured his managerial growth in both technical and business aspects and gives his expertise through his blog posts.

Related Service

Know more about our Custom Software Development Services

Learn More Arrow About Custom Software Development

Want to Hire Skilled Developers?

    Comments

    • Leave a message...

    Ready to Build Your Custom Application Solution?

    Tatvasoft is a reputed CMMI level 3 software and mobile app development company. When it comes to software development companies, Tatvasoft strives to be the best.

    Request a Proposal Arrow Icon
    United States Office
    United States +1 503 832 4034
    17304 Preston Road, Suite 800, Dallas, Texas, 75252 +1 503 832 4034
    United Kingdom Office
    United Kingdom +44 742 409 8452
    307, Euston Road,
    London NW1 3AD,
    United Kingdom
    +44 742 409 8452
    Australia Office
    Australia +61 3 9581 2659
    Level 19/180,
    Lonsdale St, Melbourne
    VIC 3000
    +61 3 9581 2659
    Canada Office
    Canada +1 416 567 7664
    4711 Yonge Street,
    10th Floor, Toronto, Ontario, M2N 6K8
    +1 416 567 7664
    Japan Office
    Japan
    902 Pearl Building,
    Miyamae-cho 8-15, Kawasaki-ku,
    Kawasaki-shi, Kanagawa,
    210-0012
    Saudi Office
    Saudi Arabia +966 552 325 560
    6th Floor,
    Al Budoor Tower Prince Mohammed Bin Fahad Road,
    Dammam 34251
    +966 552 325 560
    India Office
    India +91 960 142 1472
    TatvaSoft House,
    Rajpath Club Road, Ahmedabad, Gujarat,
    380054
    1401-1409, RK Empire,
    150 Feet Ring Road,
    Rajkot, Gujarat,
    360004
    +91 960 142 1472