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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.