Formatting date time in SQL Server

Hi

In SQL Server we have many option to format a date-time string. One of the very first needs is the current date-time. We use the getdate() function to get the current date-time. This is very much used in the process where we also store the date-time of the data insertion or editing in the database. The function provides date-time based on the server it is in.

But if you need a universal date-time then you need to use the getutcdate() function. But many a time we also need to change the format of the date-time, or convert it to a string and then change the format of the string. Here is a small list of the format that can be applied for the date-time.

  • select convert(varchar, getdate(), 1) 04/06/07
  • select convert(varchar, getdate(), 2) 07.06.04
  • select convert(varchar, getdate(), 3) 06/04/07
  • select convert(varchar, getdate(), 4) 06.04.07
  • select convert(varchar, getdate(), 5) 06-04-07
  • select convert(varchar, getdate(), 6) 06 Dec 07
  • select convert(varchar, getdate(), 7) Dec 06, 07
  • select convert(varchar, getdate(), 10) 04-06-07
  • select convert(varchar, getdate(), 11) 07/04/06
  • select convert(varchar, getdate(), 101) 04/06/2007
  • select convert(varchar, getdate(), 102) 2007.04.06
  • select convert(varchar, getdate(), 103) 06/04/2007
  • select convert(varchar, getdate(), 104) 06.04.2007
  • select convert(varchar, getdate(), 105) 06-04-2007
  • select convert(varchar, getdate(), 106) 06 Dec 2007
  • select convert(varchar, getdate(), 107) Dec 06, 2007
  • select convert(varchar, getdate(), 110) 04-06-2007
  • select convert(varchar, getdate(), 111) 2007/04/06

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 10/11/2007 5:20:56 AM

thanks

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia