Counting how many times a string appears in a document

Following code can be used to find occurances of a word you are searching that appear in a document or sql string. Assume you are searching for a way to find number of occurances of a word ‘abc’ appear in the long text. Set document length of document as varchar(max) and pass the document text into it. Then declare another variable with fixed character length for text you are interested in and execute the select statement as below:

DECLARE @MY_DOCUMENT VARCHAR(MAX)
SET @MY_DOCUMENT = ‘asdfgf,adefgfh,abc,abc,oneoonn,abc,abc,runway, abad, bard, abc,abcd,eed,ddeo,’
DECLARE @My_WORD CHAR(3)
SET @My_WORD = ‘abc’
SELECT (LEN(@MY_DOCUMENT ) – LEN(REPLACE(@MY_DOCUMENT , @My_WORD , ”))) / LEN( @My_WORD )

Technet Link to Identify SSIS Errors:
Refer to this MSDN Documentation site for obtaining detailed information about the Errors often encountered in SSIS Packages.
http://technet.microsoft.com/en-us/library/ms345164(v=sql.100).aspx

Format phone numbers in TSQL

Often We are faced with situation when Phone numbers are not input into the SQL tables with correct formatting. This poses a challenge of formatting on the presentation side. The code snippet below should help in solving this issue. Tweak this should you have additional formatting needs

SELECT
phone,
CASE LEN(phone)
WHEN 11 THEN LEFT(phone,1)+
STUFF(STUFF(STUFF(phone,1,1,’ (‘),6,0,’) ‘),11,0,’-‘)
WHEN 10 THEN
STUFF(STUFF(STUFF(phone,1,0,’ (‘),6,0,’) ‘),11,0,’-‘)
WHEN 7 THEN
STUFF(phone,4,0,’-‘)
ELSE phone
END as phone
FROM
(
SELECT
‘18002223333’ Phone UNION all
SELECT
‘8002223333’ Phone UNION all
SELECT
‘2224353’ Phone
)
as tablephone

Note:: “CASE” part of the expression is the actual solution to the issue. Tablephone was populated only to show execution results.

Calculating Dates in TSQL

Firt day of the previous month:
1) SELECT DATEADD(MONTH, DATEDIFF(MONTH, ‘20120101’, GETDATE()) -1, ‘20120101’)
This displays the first day of the month with Timestamp and hh:mm:ss in 00:00:000 format

2) SELECT dateadd(m,-1,DateAdd(d, -1.0 * DatePart(D, getdate()) + 1, getdate()))
This is another way of calculating time subtracting the days from the current day. This gives more accurate with precise timestamp.

You can tweak the above to get the first day of any month going back or forward in time.
also you can use variables to determine months using the above logic. For example in your reports you want the last 12 months of data from the user entered date.
You can use the following to the begin month as under:

DECLARE @SelectedDate AS DATETIME,
@NumberOfMonths AS INT
Select @SelectedDate = GetDate(), @NumberofMonths = -1
SELECT DATEADD(MONTH, DATEDIFF(MONTH, ‘20120101’, @SelectedDate) +@NumberOfMonths, ‘20120101’)

Using a TRY CAT…

Using a TRY CATCH BLOCK IN SQL CODE FOR trapping Errors

 

When a stored procedure is executed in unattended mode, it could throw one or many error or exceptions.  There is more than one way to try and catch this errors and send it as a mail to the users when stored procedure fails to execute.  Today I will discuss one method to display this message using two important SQL Functions/Tasks.

Step 1: Catch the exception in a container and store in SQL table

Step 2: Mail the information in the container to the users

Step1 : Catch the exception: For this we often use the TRY CATCH BLOCK WITHIN THE SQL Stored Procedure.

Example

 

CREATE/ALTER PROCEDURE   <SCHEMA NAME>.< NAME OF PROCEDURE>

AS

SET NOCOUNT ON

BEGIN  TRY

<Logic for the Stored Proc is inserted here>

END TRY

BEGIN CATCH   

  SELECT  

ERROR_NUMBER()

                                , ERROR_SEVERITY()

                                , ERROR_STATE()

                                , ERROR_PROCEDURE()

                                , ERROR_LINE()

                                , ERROR_MESSAGE()

To do this we need to create a SQL Table as under:

CREATE TABLE ErrorLog

{

@error_number INT,

@error_severity INT,

@erro_state INT,

@procedure_name VARCHAR(MAX),

@error_line INT,

@error_message VARCHAR(MAX)

}

INSERT INTO ERRORLOG

VALUES

SELECT @error_number = ERROR_NUMBER()

                                , @error_severity = ERROR_SEVERITY()

                                , @erro_state = ERROR_STATE()

                                , @procedure_name = ERROR_PROCEDURE()

                                , @error_line = ERROR_LINE()

                                , @error_message = ERROR_MESSAGE()

 

–SELECT @emess AS ErrorMessage, @eline AS ErrorLine

   EXECUTE [usp_ErrorLogInsert]

 @ERROR_NUMBER  ,

@ERROR_SEVERITY ,

@ERROR_STATE ,

@ERROR_PROCEDURE ,

@ERROR_LINE ,

@ERROR_MESSAGE

END CATCH;

 If we want to catch the error messages into SQL server table, we need the following steps

Step 1: Create a Error Log Table on SQL Server.  We need appropriate table create permissions to

create a permanent table on SQL Server.

–Create Table

CREATE TABLE ErrorLog

{

Error_number INT,

Error_severity INT,

ErrorSstate INT,

ProcedureName VARCHAR(MAX),

ErrorLine INT,

@error_message VARCHAR(MAX),

 

}

 Step 2 : Create a procedure to insert data by called procedure.  The called procedure will use this procedure to send data and populate table

CREATE Procedure [dbo].[usp_ErrorLogInsert]

 (   @ERROR_NUMBER int

    ,@ERROR_SEVERITY int

    ,@ERROR_STATE int

    ,@ERROR_PROCEDURE AS varchar(max)

    ,@ERROR_LINE AS int

    ,@ERROR_MESSAGE varchar(max)

      )    

AS

 Set Nocount on

 INSERT INTO [dbo].[ErrorLog]

           ([ERROR_NUMBER]

           ,[ERROR_SEVERITY]

           ,[ERROR_STATE]

           ,[ERROR_PROCEDURE]

           ,[ERROR_LINE]

           ,[ERROR_MESSAGE])

     VALUES

           ( @ERROR_NUMBER

    ,@ERROR_SEVERITY

    ,@ERROR_STATE

    ,@ERROR_PROCEDURE

    ,@ERROR_LINE

    ,@ERROR_MESSAGE)

Aside

Some Common SSRS Report Functions

1. To get Today’s date
= Today

2. To get 3 days back from today

=DateAdd(“d”, -3, Today)

3. Get First Day of the Month

=DateAdd(“D”, -1.0 * DatePart(“D”, Today) + 1, Today)

The trick is using today and using a negative multiplier to go back those many days.

4. Get Last Day of the Month

=DateAdd(“m”, 1, DateAdd(“d”, -1.0 * DatePart(“d”, Today), Today))

5. Get the First Day of the Last Month

=DateAdd(“D”, -1.0 * DatePart(“D”, Today) + 1, DateAdd(“m”, -1, Today))

6. Get the Name of the weekday

=WeekdayName(DatePart(“w”, Today))

7.  If today is Monday then go to Friday as last week day. Else yesterday is the last weekday

=IIF(WeekdayName(DatePart(“w”, Today))=”Monday”,DateAdd(“d”, -3, Today),DateAdd(“d”, -1, Today))

8. Formatting Dates  use format() function

This Year

=Format(Today,”yyyy”)

Years as 1900-9999

This Month

=Format(Today,”M”)

Months as 1-12

Months in 2 digits.   Months until October will come with leading 0

=Format(Today,”MM”)

Months as 01-12

Get Month Name (Jan –   Dec)

=Format(Today,”MMM”)

Months as Jan-Dec

Get Month by Name (Full   Month Name)

=Format(Today,”MMMM”)

Months as   January-December

Get date without 0 prefix

=Format(Today,”d”)

Days as 1-31

Get date with 0 prefix   until 10th day of the month

=Format(Today,”dd”)

Days as 01-31

Get short name of   day (Mon –Sat)

=Format(Today,”ddd”)

Days as Sun-Sat

Get full name of the   day

=Format(Today,”dddd”)

Days as   Sunday-Saturday

Get Date formatted  in dd/mm/yyyy hh:mm:ss tt

=Format(cdate(“01/31/2009   16:00:00″),”dd MM yyyy hh:mm:ss tt”)

31/01/2009 08:00:00   PM

9. Convert String to Date

 =CDate( “1/” & Parameters!StartMonth.Value & “/” & Parameters!StartYear.Value)

10. SWITCH statement – An alternative to IIF/CASE (Returns a value depending on which condition is true)
=SWITCH(WeekdayName(Fields!Date.Value) = “Monday”,”Blue”,
WeekdayName(Fields!Date.Value) = “Tuesday”,”Green”,
WeekdayName(Fields!Date.Value) = “Wednesday”,”Red”)

 

This is perhaps one of the useful functions to use instead of long IIFs

11. Format Numbers as Currency – The result for the following will be $1000.00
=FormatCurrency(1000)

12. Convert integer values to string
= CStr(123123)

Store Stored procedure errors into a SQL table

Using a TRY CATCH BLOCK IN SQL CODE FOR trapping Errors

 

When a stored procedure is executed in unattended mode, it could throw one or many error or exceptions.  There is more than one way to try and catch this errors and send it as a mail to the users when stored procedure fails to execute.  Today I will discuss one method to display this message using two important SQL Functions/Tasks.

Step 1: Catch the exception in a container and store in SQL table

Step 2: Mail the information in the container to the users

 

Step1 : Catch the exception: For this we often use the TRY CATCH BLOCK WITHIN THE SQL Stored Procedure.

Example

 

CREATE/ALTER PROCEDURE   <SCHEMA NAME>.< NAME OF PROCEDURE>

AS

SET NOCOUNT ON

BEGIN  TRY

<Logic for the Stored Proc is inserted here>

END TRY

BEGIN CATCH   

  SELECT  

ERROR_NUMBER()

                                , ERROR_SEVERITY()

                                , ERROR_STATE()

                                , ERROR_PROCEDURE()

                                , ERROR_LINE()

                                , ERROR_MESSAGE()

 

When a SQL server fails to execute a query or a procedure, it displays the above system messages.  Using the TRY CATCH block we are collecting all of them in a place and convey it to the user.  If the SQL procedure is running unattended and you might like to create a SQL table and put the data in a table for logging purposes.

To do this we need to create a SQL Table as under:

CREATE TABLE ErrorLog

{

@error_number INT,

@error_severity INT,

@erro_state INT,

@procedure_name VARCHAR(MAX),

@error_line INT,

@error_message VARCHAR(MAX)

}

INSERT INTO ERRORLOG

VALUES

SELECT @error_number = ERROR_NUMBER()

                                , @error_severity = ERROR_SEVERITY()

                                , @erro_state = ERROR_STATE()

                                , @procedure_name = ERROR_PROCEDURE()

                                , @error_line = ERROR_LINE()

                                , @error_message = ERROR_MESSAGE()

 

–SELECT @emess AS ErrorMessage, @eline AS ErrorLine

   EXECUTE [usp_ErrorLogInsert]

 @ERROR_NUMBER  ,

@ERROR_SEVERITY ,

@ERROR_STATE ,

@ERROR_PROCEDURE ,

@ERROR_LINE ,

@ERROR_MESSAGE

END CATCH;

 

 

 

CREATE Procedure [dbo].[usp_ErrorLogInsert]

 (   @ERROR_NUMBER int

    ,@ERROR_SEVERITY int

    ,@ERROR_STATE int

    ,@ERROR_PROCEDURE AS varchar(max)

    ,@ERROR_LINE AS int

    ,@ERROR_MESSAGE varchar(max)

      )    

AS

 

Set Nocount on

 

 

INSERT INTO [dbo].[ErrorLog]

           ([ERROR_NUMBER]

           ,[ERROR_SEVERITY]

           ,[ERROR_STATE]

           ,[ERROR_PROCEDURE]

           ,[ERROR_LINE]

           ,[ERROR_MESSAGE])

     VALUES

           ( @ERROR_NUMBER

    ,@ERROR_SEVERITY

    ,@ERROR_STATE

    ,@ERROR_PROCEDURE

    ,@ERROR_LINE

    ,@ERROR_MESSAGE)

Some Common Report Functions

/*

This Function get the original value, checks to see if the amount is negative.  If negative, sets the value to 0.  If value is positive, leave the amount unchanged

*/

‘————–Function to Reset NegativeValues to 0 if negative

Public Function ResetNegativeAmounts(ByVal OriginalAmount AS Decimal) AS Decimal

If OriginalAmount < 0.00

ResetNegativeAmounts = 0.00

ELSE

ResetNegativeAmounts  = OriginalAmount

End If

End Function

‘——————————–

Public Function KeepNegativeAmount(ByVal OriginalAmount AS Decimal) AS Decimal

If OriginalAmount < 0.00

KeepNegativeAmount =OriginalAmount

else

KeepNegativeAmount  = 0.00

End If

End Function

‘——————————–

Public Function SetRowColor(ByVal rowNumber as Integer) AS String

If rowNumber Mod 2 = 0

SetRowColor = “Grey”

Else

SetRowColor = “White”

End If

End Function

‘——————————–

Interestingly This Mod function can be used to set more than 2 colors for a report.  For example, if someone wants to show 3 colors in alternate rows, we can modify the mod function to use Mod 3 TO get 3 alternating colors for rows like this..

‘——————————–

Public Function SetThreeRowColor(ByVal rowNumber as Integer) AS String

If         rowNumber Mod 3 = 0

SetThreeRowColor = “Grey”

Elseif

rowNumber Mod 3 = 1

SetThreeRowColor = “White”

Else

SetThreeRowColor = “Green”

End If

End Function

‘——————————–

‘Display only the last 4 characters of an SSN

‘——————————–

Public Function SetSSNLastFour(ByVal FullSSN as String) AS String

If LEN(FullSSN) <> 0

SetSSNLastFour= “XXX-XX-” + RIGHT(FullSSN, 4)

End If

End Function

‘——————————-

 

How to change a column color in SSRS

How To:  Set color for a row or column in SSRS Reports

There are situations when the Users of a report want to see a column or row in a particular color based on results.  For example the users may want to represent negative amounts or percentages to be displayed in a color to catch the attention of the user of the report.

There is more than one way to get the row or column coded.  I will share you one way of achieving the results.

Steps:

  1.  Select the column or row that needs to be color coded
  2. Open Properties window (F4) in Report
  3. Click Font -> Color property
  4. Choose <<Expression>>
  5. Enter the following code

=Code.SetColor(Code.SetDisplayValue(<<Field Name>>, “Black”)

SetColor is a custom code that you need to write and paste in “Report” Code Window

    ‘—PUBLIC FUNCTION TO SET COLOR TO RED IF THE VALUES IS LESS THAN 0 —-

    Public Function SetColor(ByVal amount as Decimal, ByVal DefaultColor as String) AS String

    If amount < 0

    SetColor = “Red”

    Else

    SetColor = DefaultColor

    End If

    End Function 

    ‘—PUBLIC FUNCTION TO DISPLAY FIELD VALUE–

Public Shared Function SetDisplayValue(ByVal amount As Decimal)    

if IsNothing(amount)    

SetDisplayValue = 0    

Else    

SetDisplayValue = amount    

End If    

End Function

As you can see above the Public functions can be called any of the report columns are rows.  This facilitates code reuse.

SSIS gameplan..

In this Introduction to SSIS, I will walk you through the basic ETL tasks.  ETL as Extract Transform and Load process helps as a good backend process to absorb and refine the data obtained from multiple source systems.   SSIS packages use multitude of “Transformation” tasks, each suiting a particular purpose in mind.

When we open BIDS and open a new SSIS project, a default package with name “Package.dtsx” is created by the application.

All packages have extension of “.dtsx”.  If while renaming a package, we forget to add this extension, the package is moved to “Miscellaneous” folder.

This leaning Tutorial spans into 9 parts,

Part 1: Control Flows and Connection Managers

Part 2: Data Flow Tasks

Part 3: Event Handlers

Part 4: Package Explorer

Part 5: SSIS Variables

Part 6: Package Deployment and Security

Part 7: Advanced Learning

Part 8: Useful resources

Part 9:  Q & A

Enough of the talk.  Let’s learn SSIS through web videos provided in the site!

Previous Older Entries