August 1, 2017

Converting String to GUID

DECLARE @STRING NVARCHAR(50) = '447d16b308b24073ac5a7bf07c14bc8g'

SET @STRING  = REPLACE(REPLACE(@str,'0x',''), '-' ,'')



April 28, 2017

SSIS: Script Component Asynchronous Transformation

We can configure the SSIS Script component output as a Synchronous or Asynchronous as required. Using the Synchronous setting each input row is processed as it passes through the component, where as Asynchronous setting waits until multiple input rows have been received before processing.

I had to use this when I was integrating two system using SSIS package. The scenario was to send the massive amount of data in a chunk from the source system into the other system in the JSON format. This Asynchronous setting helped to send the data in a limited batch size.

In order to configure in Asynchronous setting, we need to configure Output 0 to be an asynchronous output. Click on Output 0 in the Inputs and outputs and set the Synchronous property to None.

February 6, 2017

Loading Dll from Embedded Resource to SSIS Script Component

1) Add dll file as a reference.
2) Added dll must be saved in the project as a Embedded Resource.
        a) Right click on the added dll file
        b) Build Action -> Embedded Resource
        c) Copy to Output Directory -> Do not Copy

Use the script below:

 static ScriptMain()
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        Assembly executingAssembly = Assembly.GetExecutingAssembly();

        if (args.Name.Contains("AddedFile1"))
            if (AddedFile1 == null)
                using (Stream stream = executingAssembly.GetManifestResourceStream("AssemblyName.AddedFile1.dll"))
                    byte[] assemblyRawBytes = new byte[stream.Length];
                    stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
                    IdentityModel = Assembly.Load(assemblyRawBytes);
            return AddedFile1;
        else if (args.Name.Contains("AddedFile2"))
            if (Newtonsoft == null)
                using (Stream stream = executingAssembly.GetManifestResourceStream("AssemblyName.AddedFile2.dll"))
                    byte[] assemblyRawBytes = new byte[stream.Length];
                    stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
                    Newtonsoft = Assembly.Load(assemblyRawBytes);
            return AddedFile2;
        return null;

We can get AssemblyName from the properties of the script component project.

May 4, 2016

Getting Nth Position Char Index

I had to write a function which will return NthCharIndex when text, subtext and position to return its char index. Below function will help to get it.

@text    nvarchar(MAX),
@subText nvarchar(MAX),
@numCharIndex INT
DECLARE @subTextIndex INT
DECLARE @lastSubTextIndex INT
DECLARE @count INT = 0

SET @subTextLen = LEN(@subText)
SET @startPos = 1
SET @lastSubTextIndex = 0

SET @subTextIndex = CHARINDEX(@subText, @text, @startPos)

WHILE @subTextIndex > 0
IF @count = @numCharIndex

SET @lastSubTextIndex = @subTextIndex
SET @startPos = @subTextIndex + @subTextLen
SET @subTextIndex = CHARINDEX(@subText, @text, @startPos)
SET @count = @count + 1

RETURN @lastSubTextIndex


SELECT [Control].[GetNthCharIndex]('Avesh Dhakal', 'a', 1)

Output: 1

SELECT [Control].[GetNthCharIndex]('Avesh Dhakal', 'a', 2)

Output: 9

SELECT [Control].[GetNthCharIndex]('Avesh Dhakal', 'a', 3)

Output: 11

March 14, 2016

SSRS: The version of the report server database is either in a format that is not valid, or it cannot be read. The found version is '162'. The expected version is '147'.

My laptop has 2 SQL Server instances namely SQLServer2008R2 and SQLServer2012. Today I messed up 2008 R2 reporting services when I was trying to configure 2008 R2 Reporting Services using SQLServer2012 Reporting Services Configuration Manager because for some reason 2008R2 Reporting Services Configuration Manager was denying to connect with the 2008R2 reporting server.

When I was trying to open report server through browser it was throwing an error "The version of the report server database is either in a format that is not valid, or it cannot be read. The found version is '162'. The expected version is '147'.". It made me really frustrated. On ReportServer DB, I found a table named [ReportServer].dbo.[ServerUpgradeHistory]. It contains information about the Server Upgrade. I updated 162 to 147 and it worked well.

SET tbl.ServerVersion = '162'
FROM [ReportServer].dbo.[ServerUpgradeHistory] tbl
WHERE UpgradeID = 3

February 16, 2016

Return Date Part from DATETIME - SQL Server

Most of the time we need date part only from datetime format in SQL Server. 

1) Old method

2) On SQL Server 2008 and higher

Above two methods helps to keep original date format and returns date part only from datetime.

3) If you want in specific date format (US date format)

February 1, 2016

SSRS Default Date Parameters

Default date value in SSRS will help to provide the date/time range (Start and End Date) as needed. This will help to reduce the report execution time, it also helps end users because they don't need to provide date range every time they run the report.

For Example:

1)Beginning of current month
=DateSerial(Year(Date.Now), Month(Date.Now), 1)

2)End of current month
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, DateSerial(Year(Date.Now), Month(Date.Now), 1)))

3) Beginning of  last month

=DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))

4) End of last month
=DateAdd(DateInterval.DAY, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))

5) End of last month 11 PM
=DateAdd(DateInterval.Hour, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))

1) Double click on the date parameters.

2) Click on "Default Values". Select "Specify values" and click on Add Button and then function (fx)

3) Then, provide the formula and click OK.