How To: Handling Invalid (Illegal or Special) Characters in XML
Not all special characters cause an issue especially if most XML is generated using FOR XML PATH which takes care of encoding in 99% cases and encode any special characters not permitted in an XML...
View ArticleSQL Server Parameter Sniffing and Statistics Used in Query Optimizer
Problem: Stored procedure was taking about 1-2 seconds when run in SQL Query Analyzer while with the same parameters timed out after 90 seconds if run via .NET application and using .Net SqlClient Data...
View ArticleTransformation: Split String to Rows (Simple Split Function)
DECLARE @statement NVARCHAR(MAX); SET @statement = N' CREATE FUNCTION [MySchemaName].[Split] ( @StringToSplit nvarchar(2000), @SplitOn nvarchar(5) ) RETURNS @RtnValue table ( Id int identity(1,1),...
View ArticleTransformation: Rollup Rows or List to String
DECLARE @Temp TABLE (String VARCHAR(10), Id INT) DECLARE @Rollup VARCHAR(MAX) DECLARE @Separator VARCHAR(3) SET @Separator = ','; INSERT INTO @Temp (String, Id) SELECT 'flower', 1 UNION SELECT...
View ArticleTransformation: Create Dynamic Date Table (First Day of Month, Last Day of...
-- @myTable is helpful in JOIN with another table when using GROUP BY in let's say Order -- table when we need a SUM of $$$ by Date including the date with no orders for a given date SET NOCOUNT ON...
View ArticleTransformation: Dates and Strings
-- Create Date declare @year int, @month int, @day int select @year = 2009, @month= 3, @day = 3; select dateadd(mm,(@year-1900)* 12 + @month - 1,0) + (@day -1) [Date] -- Get Last Name out of Full Name...
View ArticleTransformation: Pivot Columns to Rows using XML
SELECT MyTable.myColumn.value('local-name(.)', 'VARCHAR(100)') AS ColumnHeader, MyTable.myColumn.value('.', 'VARCHAR(MAX)') AS Value FROM ( SELECT ( SELECT * FROM Production.Product WHERE ProductID =...
View ArticleTransformation: Example of Recursive Query Using Common Table Expressions...
DECLARE @Temp TABLE (PrimaryKey INT, ParentKey INT) INSERT INTO @Temp (PrimaryKey, ParentKey) SELECT 1, 0 UNION SELECT 2, 1 UNION SELECT 3, 2 UNION SELECT 4, 3; WITH TopParent(PrimaryKey, ParentLevel,...
View ArticleTransformation: Pivot Word to Letters
DECLARE @statement NVARCHAR(MAX); SET @statement = N' CREATE FUNCTION [dbo].[SplitWordToLetters] ( @StringToSplit nvarchar(2000) ) RETURNS @RtnValue table ( Id int identity(1,1), Letter NCHAR(1) ) AS...
View ArticleHow To: Use msdb.dbo.sp_send_dbmail to Email CSV Files
Sending simple CSV files that open in Excel should be uncomplicated. Maximum attachment size is 2 MB. Reference: sp_send_dbmail (Transact-SQL) DECLARE @subject NVARCHAR(500), @body NVARCHAR(MAX),...
View Article
More Pages to Explore .....