Write shortest T-SQL code to print 0.00 without using any number and built-in function
Thursday, October 4, 2012
Monday, October 1, 2012
What is Tally Table in SQL Server?
In this post, I am going to explain about Tally table and it’s
uses in T-SQL programming.
I use Tally table to generate 25 years of dates, so my Tally tables will have values from 1 to 10,000 (25 years * 365.25 days = 9131.25)
SET NOCOUNT ON;
IF OBJECT_ID('dbo.Tally') IS NOT NULL DROP TABLE dbo.Tally
GO
-- Define how many rows you want in Tally table.
-- I am inserting only 10000 rows
SET ROWCOUNT 10000
SELECT IDENTITY(INT, 1, 1) ID
INTO dbo.Tally
FROM master.sys.all_columns c
CROSS JOIN master.sys.all_columns c1
-- you may use one more cross join if tally table required hundreds of million rows
SET ROWCOUNT 0
-- ADD (unique) clustered index
CREATE UNIQUE CLUSTERED INDEX PKC_Tally ON dbo.Tally (ID)
GO
How to use
Tally table in T-SQL and what are the advantages?
SELECT DATEADD(DD, ID-1, @BeginDate) [Date]
,DAY(DATEADD(DD, ID-1, @BeginDate)) [Day]
,MONTH(DATEADD(DD, ID-1, @BeginDate)) [Month]
,YEAR(DATEADD(DD, ID-1, @BeginDate)) [Year]
FROM dbo.Tally
WHERE ID <= DATEDIFF(DD, @BeginDate, @EndDate) + 1
Find a Character Positions in a String using Tally Table
SET @Str = 'Hari,Jon,Ravi,Vijay,Peter,Max' --Input String
SELECT ID AS CharPosition
FROM dbo.Tally
WHERE ID <= LEN(@Str)
AND SUBSTRING(@Str, ID, 1) = @FindChar
ORDER BY ID
Find count of all the occurrences of a Character in a String using Tally Table
SET @Str = 'Hari,Jon,Ravi,Vijay,Peter,Max' --Input String
SELECT ID AS CharPosition
FROM dbo.Tally
WHERE ID <= LEN(@Str)
AND SUBSTRING(@Str, ID, 1) = @FindChar
) AS Temp
Split Comma Seperated values using Tally table
SET @Str = 'Hari,Jon,Ravi,Vijay,Peter,Max'
WHERE ID < LEN(@Str)
AND SUBSTRING(@Str, ID, 1) = @Delimiter
A Tally table is
like any other table but having a single column of sequential numbers, values
starting from 1 (or 0) to some N (int) number.
The largest number in the Tally table should be based on what
suits your system, application, or database most. So don't use very high
number. Also column of Tally should be indexed for better performance.I use Tally table to generate 25 years of dates, so my Tally tables will have values from 1 to 10,000 (25 years * 365.25 days = 9131.25)
How to
Create a Tally Table
There
are several methods to create a Tally table. I will use one of the simplest and
obvious option - WHILE loop because it's easier to explain and simpler than
others.SET NOCOUNT ON;
IF OBJECT_ID('dbo.Tally') IS NOT NULL DROP TABLE dbo.Tally
GO
-- Define how many rows you want in Tally table.
-- I am inserting only 10000 rows
SET ROWCOUNT 10000
SELECT IDENTITY(INT, 1, 1) ID
INTO dbo.Tally
FROM master.sys.all_columns c
CROSS JOIN master.sys.all_columns c1
-- you may use one more cross join if tally table required hundreds of million rows
SET ROWCOUNT 0
-- ADD (unique) clustered index
CREATE UNIQUE CLUSTERED INDEX PKC_Tally ON dbo.Tally (ID)
GO
There are several advantages of a Tally
table. Here are some of the examples:
I will explain these advantages with
examples.
Generate Date Range using Tally Table
Ø To generate Date Range for
given Start Date and End Date
Ø To Manipulate strings, like:
·
Find the positions of a character in a string.
·
Find the total occurances of a character in a string
·
Split comma seperated values
Generate Date Range using Tally Table
Generally
you would require a WHILE loop to create Date Range values. However, it is very
easy to generate date range using tally. Its much faster than WHILE loop:
-- Generate Date range
DECLARE @BeginDate DATE = '2001-01-01', @EndDate DATE = '2025-12-31'SELECT DATEADD(DD, ID-1, @BeginDate) [Date]
,DAY(DATEADD(DD, ID-1, @BeginDate)) [Day]
,MONTH(DATEADD(DD, ID-1, @BeginDate)) [Month]
,YEAR(DATEADD(DD, ID-1, @BeginDate)) [Year]
FROM dbo.Tally
WHERE ID <= DATEDIFF(DD, @BeginDate, @EndDate) + 1
Here
is the output:
Find a Character Positions in a String using Tally Table
You
can find a character position using string functions and WHILE loop. But Tally
table makes it much simpler than any other method, yet faster. Here is an
example:
-- Find the position numbers of comma
in a given string.
DECLARE @Str VARCHAR(1000), @FindChar CHAR(1) = ','SET @Str = 'Hari,Jon,Ravi,Vijay,Peter,Max' --Input String
SELECT ID AS CharPosition
FROM dbo.Tally
WHERE ID <= LEN(@Str)
AND SUBSTRING(@Str, ID, 1) = @FindChar
ORDER BY ID
Here
is the output:
Find count of all the occurrences of a Character in a String using Tally Table
You
can find the count of all the occurrences of a character in a string using
Tally table by slightly modifying above query:
-- Find the occurrences of a character
in a given string.
DECLARE @Str VARCHAR(1000), @FindChar CHAR(1) = 'a'SET @Str = 'Hari,Jon,Ravi,Vijay,Peter,Max' --Input String
SELECT COUNT(1) AS CharCount
FROM (SELECT ID AS CharPosition
FROM dbo.Tally
WHERE ID <= LEN(@Str)
AND SUBSTRING(@Str, ID, 1) = @FindChar
) AS Temp
Here
is the output:
Split Comma Seperated values using Tally table
The
logic implemented in that function could be much simpler by using Tally table.
You can split the values without WHILE loop. It would be interesting to compare
the performance of these two mechanism.
--Split Comma Seperated values
DECLARE @Str VARCHAR(1000), @Delimiter CHAR(1) = ','SET @Str = 'Hari,Jon,Ravi,Vijay,Peter,Max'
-- Append delimiter at the beginning
and end
SET @Str = @Delimiter + @Str + @Delimiter
SELECT SUBSTRING(@Str, ID+1, CHARINDEX(@Delimiter, @Str, ID+1) - ID-1) SplitedString
FROM dbo.Tally WHERE ID < LEN(@Str)
AND SUBSTRING(@Str, ID, 1) = @Delimiter
Here
is the output:
Subscribe to:
Posts (Atom)