SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds Follow SQL Server Performance on Twitter


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
SQL Azure
Developer
General DBA
PowerShell
Windows Server
ASP.NET / ADO.NET
SQL Azure

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Create a Performance Baseline Repository
Visual Studio LightSwitch Tutorial
Manage Database Projects With Visual Studio 2010
Auditing with Microsoft Assessment and Planning (MAP) Toolkit 5.0 - ...

More     
 
Latest FAQ's

SQL Agent job getting suspended.
Queries which include DMFs return a syntax error ...
Could not find stored procedure 'dbo.sp_MSins_dboTest'
How to change server name when replication is enabled.

More     
   
Latest Software Reviews

Confio Ignite PI 8 E studio De Un Caso
dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...

More     

articles >> developer >> Fun with Numbers in Transact-SQL Queries ...

Fun with Numbers in Transact-SQL Queries

By : Vyas Kondreddi
Jun 30, 2002

Page 2 / 4

Recently, someone asked in a Microsoft public SQL Server newsgroup how can you sort characters within a string? For example, a string contains 'CBA', and he wanted to sort the characters within the string and make it 'ABC'. Instead of going for a procedural solution, just for fun, I tried approaching it in a relational way (T-SQL specific). In this article, I'll show you how we can make use of a numbers table for writing this kind of innovative queries. 

Let's first create a numbers table called 'Numbers', with only one column called 'Number'. The 'Number' column is an IDENTITY column with a primary key and clustered index defined on it. A clustered index in this case improves the performance of queries, as we will be querying this numbers table for ranges of numbers. 

The following script will drop the 'Numbers' table if it already exists, create it, and populate the table with numbers 1 to 8000 (We need only 8000 numbers as a char/varchar variable or column can have a maximum of 8000 characters). Notice the use of "DEFAULT VALUES". That's how one should insert rows into a table with a single column, and that column happens to be the IDENTITY column. I'm using the IDENTITY column here, just for the sake of convenience and simplicity, and there's nothing stopping you from using a smallint column and insert 8000 rows by incrementing a variable.

 

SET NOCOUNT ON
GO

IF EXISTS
(
SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'Numbers'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_TYPE = 'BASE TABLE'
)
BEGIN
DROP TABLE dbo.Numbers
END
GO

CREATE TABLE dbo.Numbers
(
Number smallint IDENTITY(1, 1) PRIMARY KEY
)
GO

WHILE 1 = 1
BEGIN
INSERT INTO dbo.Numbers DEFAULT VALUES

IF @@IDENTITY = 8000 
BEGIN
BREAK
END
END
GO

 

Now let us address the problem at hand: "Sorting the characters within a string." 

The following script will use the Numbers table to split the string into individual characters and create a derived table, from which it creates a sorted string using an aggregate concatenation query. This is a much better approach towards this problem, compared to you implementing a sorting algorithm in your scripts. Why reinvent the wheel when SQL Server can do the required sorting?

 

DECLARE @input varchar(100), @output varchar(100), @len smallint
SET @input = 'CDBEA'
SET @output = ''
SET @len = LEN(@input)

SELECT @output = @output + Val 
FROM 
(
SELECT TOP 100 PERCENT SUBSTRING(@input, Number, 1) AS Val
FROM dbo.Numbers (NOLOCK)
WHERE Number <= @len
ORDER BY Val
) AS Derived

SELECT @input AS 'Original string', 
@output AS 'Sorted string'

 

This idea can be extended for other scenarios. How about extracting the unique characters from a given string? That is, given an input of 'abbcccdddd', output 'abcd'. The following script will do exactly that, using the DISTINCT keyword. Again, this approach is making use of SQL Server's ability to return only distinct rows from a given set. It would be a complicated task if you have to implement this ability on your own.

 

DECLARE @input varchar(100), @output varchar(100), @len smallint
SET @input = 'ABBCCCDDDD'
SET @output = ''
SET @len = LEN(@input)

SELECT @output = @output + Val
FROM
(
SELECT DISTINCT TOP 100 PERCENT SUBSTRING(@input, Number, 1) AS Val
FROM dbo.Numbers (NOLOCK)
WHERE Number <= @len
ORDER BY Val
) AS Derived

SELECT @input AS 'Original string',
@output AS 'Sorted string with UNIQUE characters only'

Ask A Question In the Forums

<< Prev Page     Next Page>>    












C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | ASP.NET Hosting | Windows Server Hosting | Windows Server Help | Windows Phone Pro | Silverlight Ace | LightSwitch Tutorial | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Sonasoft | Andy Khanna | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved