September 29, 2008 at 6:39 am
I try to insert into table a string variable date to column datetime:
my Query:
"INSERT INTO tblDate (myDate , WorkerNum) VALUES ( ' + d + ',
'" + workerNum + "')";
but its give me this error :
"Conversion failed when converting datetime from character string"
I know that the format in column datetime sql is ad/mm/yyyy 00:00:00
and the format in my variable "myDate" in c# is dd/mm/yyyy, but i can't convert any of them. What can i do?
Thanks
September 29, 2008 at 6:43 am
I forgot to add that I try to convert "myDate" like this:
DateTime d = DateTime.Parse(myDate);
but without success.
September 29, 2008 at 8:01 am
check out the CONVERT function in SQL Server, it will let you explicity decide what format the string is in to convert to date.
CONVERT(datetime, @mystringdatevariable, 103)
for examle, would expect the string to be in 'DD/MM/YYYY' format.
September 30, 2008 at 7:50 am
Its been used like this:
"INSERT INTO tblDate (CONVERT(varchar,myDate ,103), WorkerNum) VALUES ( ' + date + ', '" + workerNum + "')";
this time i get error: 'syntax incorrect near the conver' somthing like this
September 30, 2008 at 8:19 pm
No... you have the conversion on one of the columns in the Insert list... it has to be on the Date column in the Values list, instead.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 1, 2008 at 6:50 pm
mazal0404 (9/29/2008)
"INSERT INTO tblDate (myDate , WorkerNum) VALUES ( ' + d + ','" + workerNum + "')";
According to this, you're trying to convert the string ' + d + ' into a datetime value. Those characters do not form any kind of datetime value no matter what format you specify.
If you are generating a string to execute as dynamic sql, then the result should look like this:
INSERT INTO tblDate(myDate, WorkerNum) VALUES ('01/01/2008', 42)
but the actual string being generated from your example above will look like this:
INSERT INTO tblDate(myDate, WorkerNum) VALUES (01/01/2008, '42')
Having the quotes around 42 (or whatever value WorkerNum contains) is unnecessary but not a problem -- it will convert 42 to a string then implicitly convert it back to a number. (I'm assuming the WorkerNum is a numeric type.) However, the date string MUST have quotes around it.
Tomm Carr
--
Version Normal Form -- http://groups.google.com/group/vrdbms
October 1, 2009 at 9:00 am
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply