Understanding Date and Time Functions and Operations in SQL Server
page 5 of 8
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 15172/ 285

Using SET DATEFORMAT command in SQL Server 2005

By using SET DATEFORMAT <format> through we can set SQL Server to accept a different format of date string to be assigned and used in DATETIME variable.

For example, try to execute the following query:

DECLARE @dt DATETIME
SET @dt = '23/2008/09'
SELECT @dt

In this query we tried to assign a date string of format DYM (date-year-month) into a DATETIME variable, which SQL Server does not accept by default. SQL Server may show the following error. The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

To allow SQL Server to do so, we need to execute the following statement.

SET DATEFORMAT DYM

Now executing this query again will be successful.

Query

DECLARE @dt DATETIME
SET @dt = '23/2008/09'
SELECT @dt

Result

2008-09-23 00:00:00.000

One important point to note is that SET DATEFORMAT <format> command remains valid only for the current connection.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 3 and type the answer here:

User Comments

Title: awesome   
Name: mahesh
Date: 5/29/2009 4:15:11 PM
Comment:
great job it helped me a lot thank you very much!!!!!!
Title: Great!!!   
Name: Gourik Kumar Bora
Date: 3/31/2009 3:21:57 AM
Comment:
Hi,
Thanks a lot. quite a helpful article..
Title: set datefirst inside function   
Name: ll
Date: 11/17/2008 7:24:10 AM
Comment:
how to use set datefirst inside function
Title: Thanks.   
Name: Abhishek (Author)
Date: 11/13/2008 8:05:34 AM
Comment:
Thanks to all of you for appreaciating my article.
Title: Thanks a Lot   
Name: Krishna Prabhu
Date: 11/13/2008 4:43:50 AM
Comment:
Thanks a lot for giving this artical.It helped in completing project
Title: Thnx a Ton   
Name: Neha Mishra
Date: 11/12/2008 2:15:52 AM
Comment:
Thnx a ton for such a detailed and informatic article. It helped in clearing almost all my doubts.
Title: Thank you   
Name: Vishal s.surana
Date: 11/11/2008 7:44:52 AM
Comment:
Thank you for your nice article.It is very helpful to understand differnt behaviour of datetime datatype.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/22/2009 5:07:49 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search