Archive
Using ORDER BY with CASE in SQL Queries
ORDER BY clause orders the result set of a SQL query for a particular or specific set of columns provided in ORDER BY clause.
Ever wonder to tweak that order. Lets take a scenario when you fill out an online form. When you reach at Country dropdown you get lots of County names to select. But if your Country is not listed there, there an “Other” option and that too at the bottom. Why it is not ordered with other Country names alphabetically? How come it reached that bottom? Lets see how can we get that resultset by using ORDER BY CASE.
-- Create a Country table CREATE TABLE Country (ID INT IDENTITY(1,1), cname VARCHAR(50)) -- Insert dummy data INSERT INTO Country SELECT 'Afghanistan' UNION SELECT 'Australia' UNION SELECT 'France' UNION SELECT 'Oman' UNION SELECT 'United States' UNION SELECT 'Singapore' UNION SELECT 'Other' -- SELECT with plain ORDER BY SELECT * FROM Country ORDER BY cname
ID cname
1 Afghanistan
2 Australia
3 France
4 Oman
5 Other
6 Singapore
7 United States
Here you will see the "Other" option just below "Oman". It should not be here,
because it is not a Country name and the user should should see this
option at the bottom.
-- SELECT with ORDER BY CASE SELECT * FROM Country ORDER BY CASE WHEN cname='other' THEN 1 ELSE 0 END
ID cname
1 Afghanistan
2 Australia
3 France
4 Oman
6 Singapore
7 United States
5 Other
Applying ORDER BY clause with CASE tweaks the "Other" option and places it at the
bottom. Other than Country this logic can be applied for other things like,
University Name, City, State, etc.
-- Final Cleanup DROP TABLE Country
[NOT] IN, [NOT] EXISTS, JOINS with NULL values
Using NOT IN could be dangerous and may result to incorrect results at times.
Let’s check this by a step by step example:
use [tempdb] go -- Create test tables and fill test data: CREATE TABLE [dbo].TestTable(AID int NOT NULL) INSERT INTO TestTable(AID) values (1) INSERT INTO TestTable(AID) values (2) INSERT INTO TestTable(AID) values (3) INSERT INTO TestTable(AID) values (4) go create TABLE TableWithNull(BID int NULL DEFAULT(NULL)) INSERT INTO TableWithNull(BID) VALUES(NULL) INSERT INTO TableWithNull(BID) VALUES(0) INSERT INTO TableWithNull(BID) VALUES(1) INSERT INTO TableWithNull(BID) VALUES(2) GO -- Check inserted records: SELECT * FROM TableWithNull -- NULL, 0, 1, 2 select * from TestTable -- 1, 2, 3, 4
–> Using IN & NOT IN
select * from TestTable where AID in (SELECT BID FROM TableWithNull) -- 1, 2 select * from TestTable where AID not in (SELECT BID FROM TableWithNull) -- 0 rows (expected 3, 4)
SQL is a set-based language, any set containing NULL value makes it whole as NULL. Because NULL is unknown and anything added to it also becomes unknown. Thus above SQL statement results 0 rows. To make it work you have to treat the NULL and add a WHERE clause or use EXISTS instead of IN shown below:
select * from TestTable where AID not in (SELECT BID FROM TableWithNull where BID is not null) -- 3, 4 (expected result) go
–> Using EXISTS & NOT EXISTS
select * from TestTable A where exists (SELECT * FROM TableWithNull B where A.AID = B.BID) -- 1, 2 select * from TestTable A where not exists (SELECT * FROM TableWithNull B where A.AID = B.BID) -- 3, 4 go
NOTE: EXISTS keyword is a good way to check sub-queries, this makes them co-related sub-queries. With EXISTS you don’t even need to provide any column name, just use “SELECT *”, as it does not use the SELECT list at all.
With IN you can only compare one column, but with EXISTS you can compare multiple columns within outer & inner queries (sub-queries or derived queries).
–> Using JOINS in place of IN() & EXISTS()
select A.* -- 1, 2 from TestTable A JOIN TableWithNull B ON A.AID = B.BID select A.* -- 3, 4 from TestTable A LEFT JOIN TableWithNull B ON A.AID = B.BID WHERE B.BID IS NULL go
–> Here’s another catch, weird behavior:
select * from TestTable where AID not in (SELECT AID FROM TableWithNull) -- Query works even when AID column is not in [TableWithNULL] table. -- [AID] column does not belong to [TableWithNull] table, but the query still work and won't give any error. -- So its always adviced to use table alias, shown below: select * from TestTable A where A.AID not in (SELECT B.AID FROM TableWithNull B) -- Invalid column name 'AID'.
NOTE: So always try to provide ALIAS you your tables and use it with COLUMN names in your queries.
-- Final Cleanup drop table TableWithNull drop table TestTable go
SELECT an XML string to a table – SQL Server
A simple way to SELECT XML string in a tabular format by using Nodes and OPENXML().
–> By using FOR XML AUTO:
USE [AdventureWorks] GO --// Generate an XML result-set: SELECT TOP 5 ContactID, FirstName, LastName FROM Person.Contact FOR XML AUTO GO
Output as XML String: <Person.Contact ContactID="1" FirstName="Gustavo" LastName="Achong"/> <Person.Contact ContactID="2" FirstName="Catherine" LastName="Abel"/> <Person.Contact ContactID="3" FirstName="Kim" LastName="Abercrombie"/> <Person.Contact ContactID="4" FirstName="Humberto" LastName="Acevedo"/> <Person.Contact ContactID="5" FirstName="Pilar" LastName="Ackerman"/>
–> SELECT by using XML Nodes:
DECLARE @xml XML
SET @xml = N'
<root><Person.Contact ContactID="1" FirstName="Gustavo" LastName="Achong"/>
<Person.Contact ContactID="2" FirstName="Catherine" LastName="Abel"/>
<Person.Contact ContactID="3" FirstName="Kim" LastName="Abercrombie"/>
<Person.Contact ContactID="4" FirstName="Humberto" LastName="Acevedo"/>
<Person.Contact ContactID="5" FirstName="Pilar" LastName="Ackerman"/></root>'
SELECT
Tab.Col.value('@ContactID','int') AS ContactID,
Tab.Col.value('@FirstName','varchar(20)') AS FirstName,
Tab.Col.value('@LastName','varchar(20)') AS LastName
FROM @xml.nodes('/root/Person.Contact') Tab(Col)
GO
Output:
ContactID FirstName LastName
1 Gustavo Achong
2 Catherine Abel
3 Kim Abercrombie
4 Humberto Acevedo
5 Pilar Ackerman
–> SELECT by using OPENXML:
DECLARE @xml XML SET @xml = N' <root><Person.Contact ContactID="1" FirstName="Gustavo" LastName="Achong"/> <Person.Contact ContactID="2" FirstName="Catherine" LastName="Abel"/> <Person.Contact ContactID="3" FirstName="Kim" LastName="Abercrombie"/> <Person.Contact ContactID="4" FirstName="Humberto" LastName="Acevedo"/> <Person.Contact ContactID="5" FirstName="Pilar" LastName="Ackerman"/></root>' DECLARE @docHandle int -- Create internal representation of the XML document and return the xml-doc Handle ID EXEC sp_xml_preparedocument @docHandle OUTPUT, @xml select @docHandle -- 37 SELECT * FROM OPENXML(@docHandle, N'//Person.Contact') WITH (ContactID INT, FirstName VARCHAR(20), LastName VARCHAR(20))
Output:
ContactID FirstName LastName
1 Gustavo Achong
2 Catherine Abel
3 Kim Abercrombie
4 Humberto Acevedo
5 Pilar Ackerman
-- Remove xml-doc Handle EXEC sp_xml_removedocument @docHandle GO
>> Check & Subscribe my [YouTube videos] on SQL Server.
Creating “Dynamic PIVOT” scripts in SQL Server
My previous PIVOT UNPIVOT post describes to organize data in a cross tab fashion or to transpose columns into rows and vice-versa.
This is fine only when you know how many columns you need. But when columns vary according to the query then how could you apply them on your scripts. The only way is to store them in a string variable at runtime and apply them in a dynamic SQL query, shown below.
This problem was also discussed on MSDN’s following link: http://social.msdn.microsoft.com/Forums/en/transactsql/thread/26f86fd6-6d06-4cc5-9723-9ee4685bd48a where I proposed an answer also described below:
USE [tempdb] GO -- Create test tables create table table1 (number int, desc varchar(20), location int, numberatlocation int) create table table2 (code int, name varchar(20)) -- Insert test data insert into table1 values (12345,'test',1000,5) insert into table1 values (12345,'test',1001,2) insert into table1 values (12345,'test',1002,4) insert into table1 values (12345,'test',1003,9) insert into table1 values (12345,'test',1004,7) insert into table2 values (1000,'loc1') insert into table2 values (1001,'loc2') insert into table2 values (1002,'loc3') insert into table2 values (1003,'loc4') insert into table2 values (1004,'loc5') -- Static PIVOT select number, description, [loc1], [loc2], [loc3], [loc4], [loc5] from (select number, desc, numberatlocation, name from table1 join table2 on table1.location=table2.code)p PIVOT(MAX (numberatlocation) FOR Name IN ( [loc1], [loc2], [loc3], [loc4], [loc5] ) ) AS pvt ORDER BY number
Output of Static query:
number desc loc1 loc2 loc3 loc4 loc5
12345 test 5 2 4 9 7
-- Dynamic PIVOT -- Lets add one more record on both the tables to check the results insert into table1 values (12345,'test',1005,3) insert into table2 values (1005,'loc6') declare @col varchar(1000) declare @sql varchar(2000) select @col = COALESCE(@col + ', ','') + QUOTENAME(name) from table2 select @col -- This gives: [loc1], [loc2], [loc3], [loc4], [loc5], [loc6] -- Now setting this @col variable in the Dynamic SQL. set @sql = ' select number, desc, ' + @col + ' from (select number, desc, numberatlocation, name from table1 join table2 on table1.location=table2.code)p PIVOT(MAX (numberatlocation) FOR Name IN ( ' + @col + ' ) ) AS pvt ORDER BY number' print @sql exec (@sql)
Output of Dynamic query:
number desc loc1 loc2 loc3 loc4 loc5 loc6
12345 test 5 2 4 9 7 3
-- Final Cleanup drop table table1 drop table table2





Generate SCRIPT or SOURCE CODE of DB Objects: Functions, Stored Procedures, Views, Triggers, ect
The metadata in SQL Server contains the information and code of the Database Objects, like Functions, Stored Procedures, Views, Triggers, etc.
Following are the 3 main ways you can get or generate Script or Source Code of these DB Objects:
USE [AdventureWorks] GO -- Method #1 SELECT ROUTINE_DEFINITION, ROUTINE_NAME, ROUTINE_TYPE FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'ufnGetContactInformation'; GO -- Method #2 select c.text, object_name(c.id), o.type from sys.syscomments c join sys.sysobjects o on o.id = c.id where c.id = object_ID('ufnGetContactInformation'); GO <p>-- Method #3 exec sp_helptext 'dbo.ufnGetContactInformation'; GOMore about SQL Server metadata: https://sqlwithmanoj.wordpress.com/2010/12/06/querying-sql-server-metadata/