Click here to Skip to main content
15,885,742 members
Articles / Database Development / SQL Server
Tip/Trick

Find all SQL Server DateTime Formats

Rate me:
Please Sign up or sign in to vote.
4.75/5 (7 votes)
25 Mar 2010CPOL 15.5K   5   2
When using DateTime with SQL Server, I often forget what the parameters are that allow formatting DateTime in specific ways. When that happens, I fire up this code fragment in SQL Server Management Studio and it neatly lists out every possible parameter with example output.declare @Loop...
When using DateTime with SQL Server, I often forget what the parameters are that allow formatting DateTime in specific ways. When that happens, I fire up this code fragment in SQL Server Management Studio and it neatly lists out every possible parameter with example output.

SQL
declare @Loop int
set @Loop = -1

declare @table table
(
    [Date] nvarchar(50) not null,
    [Param] int not null
)

while @Loop <= 150
begin
    set @Loop = @Loop + 1

    begin try
        insert into @table
        select convert(nvarchar, getdate(), @Loop), @Loop
    end try

    begin catch
        continue
    end catch
end

select * from @table


Sample output:

MSIL
Date                                               Param
-------------------------------------------------- -----------
Mar 24 2010  8:22AM                                0
03/24/10                                           1
10.03.24                                           2
24/03/10                                           3
24.03.10                                           4
24-03-10                                           5
24 Mar 10                                          6
Mar 24, 10                                         7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSimply press F1 and write date formats.. Pin
ran.sharabi23-Mar-10 22:50
ran.sharabi23-Mar-10 22:50 
GeneralRe: Simply press F1 and write date formats.. Pin
R. Giskard Reventlov24-Mar-10 0:49
R. Giskard Reventlov24-Mar-10 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.