Tag Archives: MS SQL Server

SQL Server Date Formats

Date Format Standard SQL Query Output
MM/DD/YY USA SELECT CONVERT(VARCHAR(8), GETDATE(), 1) 05/23/14
MM/DD/YYYY USA SELECT CONVERT(VARCHAR(10), GETDATE(), 101) 05/23/2014
YY.MM.DD ANSI SELECT CONVERT(VARCHAR(8), GETDATE(), 2) 14.05.23
YYYY.MM.DD ANSI SELECT CONVERT(VARCHAR(10), GETDATE(), 102) 2014.05.23
DD/MM/YY British/French SELECT CONVERT(VARCHAR(8), GETDATE(), 3) 23/05/14
DD/MM/YYYY British/French SELECT CONVERT(VARCHAR(10), GETDATE(), 103) 23/05/2014
DD.MM.YY German SELECT CONVERT(VARCHAR(8), GETDATE(), 4) 23.05.14
DD.MM.YYYY German SELECT CONVERT(VARCHAR(10), GETDATE(), 104) 23.05.2014
DD-MM-YY Italian SELECT CONVERT(VARCHAR(8), GETDATE(), 5) 23-05-14
DD-MM-YYYY Italian SELECT CONVERT(VARCHAR(10), GETDATE(), 105) 23-05-2014
HH:MM:SS   SELECT CONVERT(VARCHAR(8), GETDATE(), 108) 07:28:42
MM-DD-YY USA SELECT CONVERT(VARCHAR(8), GETDATE(), 10) 05-23-14
MM-DD-YYYY USA SELECT CONVERT(VARCHAR(10), GETDATE(), 110) 05-23-2014
YY/MM/DD Japan SELECT CONVERT(VARCHAR(8), GETDATE(), 11) 14/05/23
YYYY/MM/DD Japan SELECT CONVERT(VARCHAR(10), GETDATE(), 111) 2014/05/23
YYMMDD ISO SELECT CONVERT(VARCHAR(6), GETDATE(), 12) 140523
YYYYMMDD ISO SELECT CONVERT(VARCHAR(8), GETDATE(), 112) 20140523
HH:MI:SS:MMM(24H)   SELECT CONVERT(VARCHAR(12), GETDATE(), 114) 07:30:15:913
YYYY-MM-DD HH:MI:SS(24h) ODBC Canonical SELECT CONVERT(VARCHAR(19), GETDATE(), 120) 2014-05-23 07:30:28
YYYY-MM-DD HH:MI:SS.MMM(24h) ODBC Canonical
(with milliseconds)
SELECT CONVERT(VARCHAR(23), GETDATE(), 121) 2014-05-23 07:30:49.337
YYYY-MM-DDTHH:MM:SS:MMM ISO8601 SELECT CONVERT(VARCHAR(23), GETDATE(), 126) 2014-05-23T07:31:03.303
DD/MM/YYYY HH:MI:SS:MMMAM Hijiri SELECT CONVERT(VARCHAR(25), GETDATE(), 131) 24/07/1435 7:31:16:803AM

Difference between Sql Server Char and Varchar Data Type

Everyone knows about the basic difference between CHAR and VARCHAR data types. In this article apart from the basic difference, will discuss on one more interesting difference.
CHAR Data Type is a Fixed Length Data Type. For example if you declare a variable/column of CHAR (10) data type, then it will always take 10 bytes irrespective of whether you are storing 1 character or 10 character in this variable or column. And in this example as we have declared this variable/column as CHAR(10), so we can store max 10 characters in this column.
On the other hand VARCHAR is a variable length Data Type. For example if you declare a variable/column of VARCHAR (10) data type, it will take the number of bytes equal to the number of characters stored in this column. So, in this variable/column if you are storing only one character then it will take only one byte and if we are storing 10 characters then it will take 10 bytes. And in this example as we have declared this variable/column as VARCHAR (10), so we can store max 10 characters in this column.
Below example illustrates the basic difference explained above:

DECLARE
@CharName Char(20) = 'Biswajeet',
@VarCharName VarChar(20) = 'Biswajeet';
SELECT DATALENGTH(@CharName) CharSpaceUsed,
DATALENGTH(@VarCharName) VarCharSpaceUsed;

Result:
CharSpaceUsed    VarCharSpaceUsed
————-                —————-
20                            9
(1 row(s) affected)

Below is an interesting difference, which I have observed recently while writing some script. Concatenation of CHAR variables:

DECLARE
@FirstName Char(20) = 'Biswajeet',
@LastName Char(20) = 'Samal';
IF (@FirstName + ' ' + @LastName = 'Biswajeet Samal')
   PRINT 'I was Expecting'
ELSE
   PRINT 'Its different'
SELECT @FirstName + ' ' + @LastName AS Name,
LEN(@FirstName + ' ' + @LastName) AS Length

Result:
Its different
Name                           Length
————————–     ———
Biswajeet Samal        26
(1 row(s) affected)

Concatenation of VARCHAR variables:

DECLARE
@FirstName VarChar(20) = 'Biswajeet',
@LastName VarChar(20) = 'Samal';
IF (@FirstName + ' ' + @LastName = 'Biswajeet Samal')
  PRINT 'I was Expecting'
ELSE
  PRINT 'Its nice'
SELECT @FirstName + ' ' + @LastName AS Name,
LEN(@FirstName + ' ' + @LastName) AS Length;

Result:
I was Expecting
Name                                              Length
—————————————–      ———–
Biswajeet Samal                           17
(1 row(s) affected)

So, it is clear from the above examples that during concatenation of CHAR data type variables, it includes space in-place of unused space in the result of concatenation.

How To: Reset Identity column in SQL Server

This is one of those simple tip posts that may seem obvious and taken for granted by those of us who have been working with MS SQL Server for a while now but maybe a newbie or two out there will find this helpful.

I find myself resetting an identity column value back to 0 after I’ve deleted all the existing records so the table gets a fresh start at primary key 1. Yes, I know all about primary keys not changing and how the value in the primary key doesn’t matter and so on. Sometimes I just like the primary keys starting at 1.

The following line resets the Identity value for the Customer table to 0 so that the next record added starts at 1.

DBCC CHECKIDENT(table_name, RESEED, new_reseed_value)

Example:

DBCC CHECKIDENT('Customer', RESEED, 0)