SQL Server CLR Function & SQL Server Time Zone Using CLR Function

With the help of this blog, obtaining and analyzing the data with SQL in Visual Studio is now simple. extensive knowledge of the operation of queries, the use of user-defined SQL CLR methods, and the automatic generation of time using time zone functions.

The.NET with SQL CLR Server allows the development of database objects such as CLR stored procedures, user-defined SQL functions, triggers, aggregate functions, and user-defined types with any of the.NET languages.

SQL Server CLR also allows you to increase the capabilities of the SQL Server database engine. C# or VB.Net are programming languages used in the Microsoft.Net framework.

1. User-Defined  SQL CLR Function

The steps for developing a new Common Language Runtime -CLR User Designed function are listed below.

  •    To begin, open Visual Studio and create a new SQL-Server Database Project.
  •    To create a new Scalar value SQL CLR function, right-click the Project button and select Add->New Item->SQL CLR C#.

  • The New File for the writing function in C# will then be opened.

  • Right-click the SQL Server project in the solution explorer after writing the function. Select “Publish” from the menu.

After selecting the target database project, select Publish or Generate Script.

  • This Function will create the database and is easily seen in the SQL Server Management Studio.
  • This function, which can be easily viewed in SQL Server Management Studio, will create the database.

  • The SQL Function is identical and equivalent to the SQL statement that users would typically perform.

 2. Time Zone and Daylight-Saving Time

The SYSDATETIMEOFFSET() function returns the offset value for the current local date, time, and time zone. Time zone offset values are represented as [+|-] hh:mm (for example, +05:30 for India). The name of the time zone cannot be defined from this figure, though. For instance, Sri Lanka and India both have the same offset value. Similarly, it is impossible to determine if it is Daylight Saving Time (DST) with this method.

The following CLR functions can be useful for obtaining these values.

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 the 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 the current time is daylight saving, then return the daylight name, else it will return the Standard Name of the time zone.
        if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now))
        return TimeZone.CurrentTimeZone.DaylightName;
        else
        return TimeZone.CurrentTimeZone.StandardName;
     }

};

Users can now develop user-defined functions in C# or VB.Net and utilize.net development services and third-party DLLs, as shown in the CLR function example from the above.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories