Home > SQL Server > SQL Server Date Format – Convert to varchar

SQL Server Date Format – Convert to varchar

Below is a nice little script to output the various formats displayed by sql server when using convert(varchar, getdate(), n).

It just outputs the current date in the various formats so you can quickly find the one your after

Declare @date datetime
Declare @i int
set @date = getdate()
set @i = 1

while(@i <= 31)
begin
  if(@i not in (15, 16, 17, 18, 19, 22, 23, 24, 25, 28, 29))
  begin

  if @i < 22
    print convert(varchar, @i) + ': ' + convert(varchar, @date, @i)

    print convert(varchar, @i+100) + ': ' + convert(varchar, @date, @i+100)

  end
  set @i = @i + 1
end

Hope it helps

Advertisements
Categories: SQL Server
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: