INSERT - Date/Time INTO SQL ??
-
Hi All, I am an app in VS C++ 2005 that uses an SQL Server 2005 database. I am having trouble understanding how to enter a date into the database. The field data type is:
smalldatetime
. The date and time has been assigned to a string and would have a value like '30/05/2007 5:30PM
' When I write the query like:INSERT INTO MyTable(Date) VALUES(MySTRING)
I end up with an error and assume it's to do with the way O have formatted the string - am I on the right track ? Fritzables. -
Hi All, I am an app in VS C++ 2005 that uses an SQL Server 2005 database. I am having trouble understanding how to enter a date into the database. The field data type is:
smalldatetime
. The date and time has been assigned to a string and would have a value like '30/05/2007 5:30PM
' When I write the query like:INSERT INTO MyTable(Date) VALUES(MySTRING)
I end up with an error and assume it's to do with the way O have formatted the string - am I on the right track ? Fritzables.Hi Fritzables, It is very difficult to say on which track you are, without knowing the error message. But I expect the problem is the date format. You can try two things:
SET DATEFROMAT DMY
Or you can format the string to:'2007-05-20 5:30PM'
I hope this helps.Wout Louwers
-
Hi Fritzables, It is very difficult to say on which track you are, without knowing the error message. But I expect the problem is the date format. You can try two things:
SET DATEFROMAT DMY
Or you can format the string to:'2007-05-20 5:30PM'
I hope this helps.Wout Louwers
G'Day Wout, If I was to enter the sting at:
30/05/2007 5:30PM
then the error message goes something like:syntax error near 5.
Ok, so if I was to use theSET DATEFORMAT DMY
, at what point would I define this - sorry for the stupid question. Regards Fritzables. -
G'Day Wout, If I was to enter the sting at:
30/05/2007 5:30PM
then the error message goes something like:syntax error near 5.
Ok, so if I was to use theSET DATEFORMAT DMY
, at what point would I define this - sorry for the stupid question. Regards Fritzables.Ah, the errormessage explains something. You should try:
INSERT INTO MyTable(Date) VALUES('30/5/2007 5:30PM')
I think you forgot the quotation marks. If that does not work you can try:SET DATEFORMAT dmy;INSERT INTO MyTable(Date) VALUES('30/5/2007 5:30PM')
Wout Louwers
-
Ah, the errormessage explains something. You should try:
INSERT INTO MyTable(Date) VALUES('30/5/2007 5:30PM')
I think you forgot the quotation marks. If that does not work you can try:SET DATEFORMAT dmy;INSERT INTO MyTable(Date) VALUES('30/5/2007 5:30PM')
Wout Louwers
Wout - you're an absolute legend !!! Yea, it was the missing quotes that put all things back in order. Thanks a million. Fritzables.