How to split a string in sql server

 UdayarumilliSplitstringHow to split a string in sql server

We have a requirement to capture the list of key words in the given list of statements. In the below example will see How to split a string in sql server

There is a table called “SearchQueries”. It’s been having a list of queries, now the requirement is to split each and every statement into individual words and needs to be stored in a table “KeyWords”.

 Now the requirement is to split and store keywords from each record as below along with the number of occurrences.

 Example:

KeyWord       Count

Describe           12

Change              2

Data                  10

Capture              1

Etc…….

Script to create table and populate with data:

USE udayarumilli;
GO
CREATE TABLE SearchQueries(ID INT IDENTITY,Query NVARCHAR(MAX));
GO
INSERT INTO SearchQueries(Query)
SELECT'How to design 2012 SSRS reports using SQL Server Data Tools'
UNION
SELECT'What are the new features added in SSRS 2012'
UNION
SELECT'Describe Change Data Capture for SSIS in SQL Server 2012'
UNION
SELECT'What are the new features added in SSIS 2012'
UNION
SELECT'SSIS 2012 Support for Variable Number of Columns in a Flat File'
UNION
SELECT'SSIS Package Format Changed and the Specs are Open Source New in SSIS 2012'
UNION
SELECT'What are the new features added in SSAS 2012';
GO

Create a table to hold the keywords:

CREATE TABLE KeyWords(ID INT IDENTITY,Word NVARCHAR(255));

Now create a stored procedure to split the given statement into individual words.

IF EXISTS(SELECT 1 FROM SYS.SYSOBJECTS WHERE TYPE='P'AND NAME='usp_split_string')
BEGIN
	DROP PROCEDURE usp_split_string;
END
GO
CREATE PROCEDURE usp_split_string(@Txt NVARCHAR(MAX))
AS
BEGIN
SET NOCOUNT ON;
DECLARE @temp TABLE (KeyWord NVARCHAR(250));
DECLARE @data NVARCHAR(MAX),
		@delimiter NVARCHAR(10),
		@pos	INT,
		@start	INT,
		@len	INT,
		@end	INT;

SELECT	@data = @Txt,
		@delimiter=' ',
		@len=LEN('.'+@delimiter+'.')- 2,
		@end=LEN(@data)+ 1,
		@start=1,
		@pos=0;
WHILE (@pos < @end)
BEGIN
	SET @pos = CHARINDEX(@delimiter,@data,@start);
	IF (@pos= 0) SET @pos=@end;

	INSERT @temp (KeyWord)
	SELECT SUBSTRING(@data,@start,@pos-@start);
	SET @start=@pos+@len;
END
INSERT INTO KeyWords(word)
SELECT KeyWord FROM @temp;
END

The stored procedure splits the statement into individual words. For example if we input a statement “SQL Server 2012” to the stored procedure it splits the statement into words an stored into table “KeyWords”.

i.e

SQL

Server

2012

Using the same logic we will apply the logic to existing table “SearchQueries” and prepare a list of keywords from the table.

BEGIN
	DECLARE @Max INT,
			@i INT,
			@STMT VARCHAR(100),
			@Category VARCHAR(200);

	TRUNCATE TABLE KeyWords;
	SELECT @max = MAX(ID) FROM SearchQueries;
	SET @i= 1;
	WHILE(@i <= @Max)
	BEGIN
		SELECT	@stmt = Query
		FROM	SearchQueries
		WHERE	ID = @i;

		EXEC usp_split_string @stmt;
		SET	@i = @i + 1;
	END
END

Execute the above script to accomplish the task.

Check the table for the list of keywords.

Finally capture the keywords and corresponding counts.

SELECT	Word,
	Count(1)AS'Total_Count'
FROM	KeyWords
GROUP BY Word
ORDER BY Total_Count DESC;
Posted in SQL Development | Tagged , , , , , , , , , , | Leave a comment

Drill Down Reports in SSRS with examples

UDAYARUMILLI_REPORTS

Drill Down Reports in SSRS with examples

We will see how to deal with drill down reports in SSRS with examples. For this report you can create a table and populate data from the post.

Let’s create a report using SQL Server 2012 Data tools. Create SSRS report using SSRS report wizard.

Sales Table Creation Script: sales_table_script



Substitute the query in design query window.

SELECT	SYear AS 'Year',
		Country,
		Organization,
		Zone,
		Product,
		Total_SoldOut,
		Total_Price
FROM	sales
ORDER BY [Year]





Now the report looks like below.

Do required alignments and update the report name and now the report looks like below.

Preview the report to make sure it’s been working fine and retrieving data.

Now we’ll go ahead and design drill down report base don year.

Under row group add a parent group for the column year.



Preview the report to have a drill down report.

Now edit the group details as below.

Now preview the report.

Expand the year check the view.

To give more clear view we will remove the column “Year”  (repeated values).

Preview the report; it will be the final view of the drill down report.

            This is the way to implement drill down view for SSRS reports.

Posted in MSBI, SQL Development, SQL Server DBA, SSRS | Tagged , , , , , , , , , , , | 5 Comments

How to create Parameterized report in SSRS 2012


How to create Parameterized report in SSRS 2012

udayarumilli_sql_server_2012_ssrs

In the previous post we have created a simple report. We are going to use the same report to demonstrate the parameterized report.

 

 

Under the same report solution copy and paste the Simple_Report and rename it as “Parameterized_Report”.

Now we can modify the report to accept parameters.

 

Now create a new stored procedure to accept the parameters as below.

 

USE[DBAConnect]
GO
CREATEPROCEDURE[dbo].[usp_Sales_Report_Parameterized](
@OrganizationVARCHAR(100)=’ALL’,
@CountryVARCHAR(100)=’ALL’,
@ZoneVARCHAR(20)=’ALL’,
@ProductVARCHAR(100)=’ALL’,
@YearCHAR(4)=’ALL’)
AS
BEGIN
SETNOCOUNTON
DECLARE@FilterVARCHAR(1000),@SQLVARCHAR(8000)
SET@Filter=”

 

IF (@Organization<>’ALL’)
BEGIN
SET@Filter=’ AND Organization = ”’+@Organization+””
END

 

IF (@Country<>’ALL’)
BEGIN
SET@Filter=@Filter+’ AND Country = ”’+@Country+””
END

 

IF (@Zone<>’ALL’)
BEGIN
SET@Filter=@Filter+’ AND Zone = ”’+@Zone+””
END

 

IF (@Product<>’ALL’)
BEGIN
SET@Filter=@Filter+’ AND Product = ”’+@Product+””
END

 

IF (@Year<>’ALL’)
BEGIN
SET@Filter=@Filter+’ AND SYear = ”’+@Year+””
END
SET@SQL=’
SELECT”Organization”=Organization,
”Country”=Country,
”Zone”=Zone,
”Product”=Product,
”Year”=SYear,
”Total_SoldOut”=Total_SoldOut,
”Total_Price”=Total_Price
FROM[dbo].[SALES]
WHERE1 = 1 ‘+@Filter

 

PRINT@SQL
EXECUTE (@SQL)
END

 

I have written a stored procedure to accept parameters and retrieve result set dynamically. Now we’ll change the report. Follow below steps.

Change the procedure execution statement to
“EXEC [usp_Sales_Report_Parameterized]  @Organization”



Add a new data set to support List of Organizations as an input to the report.

Use the query to retrieve the input set
SELECT’ALL’ASOrganization  FROMSALES
UNION
SELECTDISTINCTOrganizationASOrganizationFROMSALES;


Now map the newly created dataset to the parameter “Organization”.




Now preview the report, by default “ALL” is selected for Organization hence all records retrieved.

Now select an organization from the list.


Now let’s include all parameters as below.
  • @Country
  • @Zone
  • @Product
  • @Year

 

Parameters are added. Now add datasets to provide inputs for parameters as below.

 

DataSet :
Country:
SELECT’ALL’ASCountry
UNION
SELECTDISTINCTCountryASCountryFROMSALES

 

Zone:
SELECT’ALL’ASZone
UNION
SELECTDISTINCTZoneASZoneFROMSALES

 

Product:
SELECT’ALL’ASProduct
UNION
SELECTDISTINCTProductASProductFROMSALES

 

Year:
SELECT’ALL’AS[Year]
UNION
SELECTDISTINCTSYearAS[Year]FROMSALES

 

As we have done mapping between dataset and parameter for “Organization” follow those steps and map all datasets to corresponding parameters. Finally it looks like as below.

Now preview the report to check the output. As we designed by default all parameters are shown as “ALL”

Now select Organization and Country values.

Now select Zone and Product values as below.

This is a simple way to implement parameterized reports.

 

Posted in MSBI, SQL Development, SSRS | Tagged , , , , , , , , , , , , | 1 Comment