Groups | Blog | Home
all groups > sql server new users > july 2006 >

sql server new users : SQL query problem with date/time format


JL
7/7/2006 2:21:44 PM
I am using VB.Net in VS2005 against SQL2005. I am writing an UPDATE
query and getting an error on my date/time fields. An example of my
query string is -
DOB = #3/12/1949 12:00:00 AM"

I get the error - "Incorrect syntax nar '12'

The way I generate the above string is from a data table and I simply
convert the field to a string. So I am confused why I get a syntax
error?

TIA,
JL
7/7/2006 3:10:06 PM
OK..I figured it out...use single quote not #in the string.

John

[quoted text, click to view]
JL
7/7/2006 3:12:31 PM
Hi Hugo,
Thanks for the very quick reply! I did figure out the single quote vs
# but your suggestion on formats is excellent and I will follow them.

Thanks again,
John

On Sat, 08 Jul 2006 00:08:14 +0200, Hugo Kornelis
[quoted text, click to view]
Arnie Rowland
7/7/2006 3:20:00 PM
A couple of good resources related to datetime issues for your 'toolbox' is:

http://www.karaszi.com/SQLServer/info_datetime.asp
http://omnibuzz-sql.blogspot.com/2006/06/date-functions-in-sql-server-faq.html

--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."




[quoted text, click to view]

JL
7/7/2006 3:43:47 PM
Hi Arnie,
Excellent refernces. Thank you very much!

John

On Fri, 7 Jul 2006 15:20:00 -0700, "Arnie Rowland" <arnie@1568.com>
[quoted text, click to view]
Hugo Kornelis
7/8/2006 12:08:14 AM
[quoted text, click to view]

Hi John,

First, SQL Server uses single quotes (') to delimit date and time
constants, not hash symbols (#) or double quotes (").

Second. to prevent misinterpretation of the string (for instance, the
date above can be interpreted as march 12 or december 3, depending on
locale settings), you should stick to these formats:

* 'yyyymmdd' - date only. Note: no punctuation is used at all, just a
character string of 8 numeric characters. Time will default to midnight.
* 'yyyy-mm-ddThh:mm:ss' - date and time. Note: the parts of the date are
seperated by dashes; the parts of the time are seperated by colons and
an uppercase T seperates date from time.
* 'yyyy-mm-ddThh:mm:ss.mmm' - date and time with millisecond precision.
Note that SQL Server rounds to the nearest 1/300th second!
* 'hh:mm:ss' or 'hh:mm:ss.ttt' - time only. Date defaults to 1/1/1900.

--
AddThis Social Bookmark Button