List Of Data Types And Their Sizes


The below table shows the storage sizes of the different data types in SQL Server.

[table caption=”DateTime Data Types” th=”0″ width=”500″ colwidth=”150|150″]
date,3 bytes
datetime,8 bytes
datetime2,6 – 8 bytes (depending on precision)
datetimeoffset,10 bytes
smalldatetime,4 bytes
time,5 bytes

[table caption=”Numeric Data Types” th=”0″ width=”500″ colwidth=”150|150″]
decimal,5 – 17 bytes
numeric,5 – 17 bytes
float,4 or 8 bytes
real,4 bytes
bigint,8 bytes
int,4 bytes
smallint,2 bytes
tinyint,1 byte
money,8 bytes
smallmoney,4 bytes

[table caption=”String” th=”0″ width=”500″ colwidth=”150|150″]
char,size defined in table
varchar,2 bytes + data size
text,data size
nchar,2 times size defined in table
nvarchar,2 bytes + 2 times data size
ntext,2 times data size

[table caption=”Binary” th=”0″ width=”500″ colwidth=”150|150″]
binary,size defined in table
varbinary,2 bytes + data size
image,2 bytes + data size

[table caption=”Other” th=”0″ width=”500″ colwidth=”150|150″]
bit,1 byte
hierarchyid,5 bytes
uniqueidentifier,16 bytes


Parse XML With SQL Server


SQL Server has a lot of useful functionality built-in when it comes to XML.  Today I want to show you an easy way to parse XML data in to a usable format inside your query.

For this example, I will be parsing XML data into a temp table, then inserting that data in to a table.


-- Load the XML data in to a variable to work with.
-- This would typically be passed as a parameter to a stored proc
SET	@XMLToParse =	'<Animals>

-- Declare temp table to parse data into
DECLARE	@ParsingTable  TABLE
	(Animal	VARCHAR(100))

-- Parse the XML in to the temp table declared above
INTO	@ParsingTable
SELECT	xmlData.A.value('.', 'VARCHAR(100)') AS Animal
FROM	@XMLToParse.nodes('Animals/LandAnimals/Animal') xmlData(A)

-- Insert into the actual table from the temp table

INTO	Animals
FROM	@ParsingTable

The statement that parses the XML has 2 parts to it.

FROM   @XMLToParse.nodes('Animals/LandAnimals/Animal') xmlData(A)

This part says to parse the @XMLToParse variable (previously filled with the XML file) using the method built in to SQL Server called .nodes.  It specifies the repeating node to be Animals/LandAnimals/Animal.  It assigns an aliases this XML parsed records as a table named xmlData.  The (A) is the column name of the rowset.  This will be referenced in the select part of the statement.


SELECT  xmlData.A.value('.', 'VARCHAR(100)') AS Animal

The select part of this statement references xmlData (which is the table aliases) and A (which is the column named for that table).  It calls the .value function to return the value from the table/column.  For the .value function, you pass in 2 elements.

The first element is the field.  In this case we are passing in just a period.  We do this because there is no node below the Animal node.  If there was, we would need to specify it here..

The second parameter is the datatype that you would like the value to be casted as.  Then I always aliases the field to something relevant.

Although parsing XML can be a little confusing in SQL Server, it is very powerful.  This is a great way to pass bulk data to a stored procedure from any type of client application.

SQL Server Management Studio Keyboard Shortcuts


Although some of these keyboard shortcuts may seem a little odd at first… once you start using them I think that you’ll love them and use them all the time.

Ctrl + R – Show/Hide the results pane

Ctrl + N – New query window

Ctrl + Shift + U – Switch current selection to uppercase

Ctrl + Shift + L – Switch current selection to lowercase

F5 – Execute the currently selected query (or entire query if nothing is selected)

Ctrl + Tab – Switch to the next open tab

Ctrl + Shift + Tab – Switch to the previous open tab

Ctrl + Space – Pops up IntelliSense complete if it is not already open

Ctrl + Shift + R – Refresh local IntelliSense cache

F8 – Open the Object Explorer if not already open


 What other SSMS keyboard shortcuts do you use?

Difference Between UNION and UNION All


The UNION operation and the UNION ALL operation perform almost the same operation.  They are both used to combine two result sets in to one result set.  The main difference between the two operations is that the UNION operation will return the unique records in the final result set.  The UNION ALL operation will return any duplicates in the final result set.

In the examples below, we will assume we have two tables with the following data.  Notice that the values Cat and Dog exist in both tables.
[table width=”0″ colwidth=”100|100″ colalign=”left|left”]

The UNION Operation

FROM	Table1


FROM	Table2

[table width=”0″ colwidth=”100″ colalign=”left”]

Notice that the duplicate Cat and Dog values were removed from the result.

The UNION ALL Operation

FROM	Table1


FROM	Table2

[table width=”0″ colwidth=”100″ colalign=”left”]

Notice that the duplicate Cat and Dog values are left in the result.

Speed Considerations

One final difference between UNION and UNION ALL is the speed difference.  Because the UNION operation needs to return a distinct set of values, this will take extra processing time.  A general rule is that if you know that the combined data is going to be unique already… just use the plain UNION ALL operation.  Only use the UNION operation if there are duplicates that you would like to have remove.
NOTE:  The UNION and UNION ALL operations are actually one operation.  The ALL keyword is just an attribute.  Because they are so different, I find it is easier to think of them both as separate operations.


SQL Server Quarter From Date


One question that we get a lot is how to get the quarter from a date.  SQL Server has a very easy way to get the quarter from a date (since SQL 2005) using the DATEPART command.

To get the quarter from the current date

SELECT	DATEPART(quarter, GETDATE()) AS Quarter_From_Date

To get the quarter from a different date


SET		@DateToCheck = '7/1/2014'

SELECT	DATEPART(quarter, @DateToCheck) AS Quarter_From_Date