Question

I realise this is quite a common problem banded around forums, but I am at a loss as to what I've done wrong here.

I have a 'Current' table something like:

ID(uniqueidentifier)
Name(nvarchar max)
FileName(nvarchar max)
FileVersion(smallint)
CurrentVersionDate(datetime)

The CurrentVersionDate is entered using the GETDATE() function within SQLserver.

When a user hit's 'Create file' on my website, the details of this table are transferred to a 'History' Table. So I use a stored procedure to select * details from 'Current' and save each value as a variable (i'm using vb.net in a visual studio project). I'll show the date variable as this is the problem. I'm saving this as a 'date' variable:

Dim dDate as Date
dDate = dr("CurrentVersionDate")

Then I send these variables to another stored procedure to enter into the 'History' table:

Execute sp_InsertHistory '" & ID & "','" & strName& "','" & strFile & "','" & intVersion & "','" & dDate & "'"

I declare the date variable in the stored procedure as:

@Date datetime,

with the insert statement:

INSERT INTO History
(ID, Name, FileName, FileVersion, VersionDate)
VALUES
(@ID, @Name, @FileName, @FileVersion, @Date)

The 'History' table has the same setup as the 'Current' table. Stepping through the code, this error is caught:

Error converting data type varchar to datetime

Surely the variables isn't varchar? And even if it is, the string is surely in the correct format for SQLserver because I'm just selecting the date value SQL has given! Any thoughts?

Was it helpful?

Solution

Since you are putting the date between quotes, is a varchar '" & dDate & "'", but I think the problem you have with the conversion could be that you are retrieving the date in your local format and you have to insert the date in the SQL Server in his own format.

Try inserting the date as a string in this format: dDate.ToString("MM/dd/yyyy HH:mm:ss")

OTHER TIPS

the string is surely in the correct format for SQLserver

How do you know that? To be safe, I'd use string in unambiguous format: YYYY-MM-DDTHH:MM:SS, e.g. 2013-05-13T01:01:00.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top