SQL UPDATE for date data-type

G

Guest

Hi folks,

I have 2 code snippets. One works and one does not.

This first hard-coded snippet works

Dim queryString As String = "UPDATE [rfi] SET [dateresolved] = '8/16/2006'
WHERE ([rfi].[id] = @id)"

This second snippet does not work

Dim dtNow As DateTime = DateTime.Now
dRes = dtNow.ToShortDateString
Dim queryString As String = "UPDATE [rfi] SET [dateresolved] = dRes WHERE
([rfi].[id] = @id)"

dateresolved is a Date/Time data-type. The first snippet uses single quotes
around the date but I would think the second snippet would be accepted as a
string. If I debug the value of dRes, I receive the proper format of
"8/16/2006"

Any clues you can leave me with?

Thanks,
- Glenn
 
M

Mark Rae

Dim dtNow As DateTime = DateTime.Now
dRes = dtNow.ToShortDateString
Dim queryString As String = "UPDATE [rfi] SET [dateresolved] = dRes WHERE
([rfi].[id] = @id)"

dateresolved is a Date/Time data-type. The first snippet uses single
quotes
around the date but I would think the second snippet would be accepted as
a
string. If I debug the value of dRes, I receive the proper format of
"8/16/2006"

Yes, but SQL Server doesn't know that. If you're building up your SQL
dynamically, then you need to format it properly...

Dim queryString As String = "UPDATE [rfi] SET [dateresolved] = '" &
dRes.ToString("dd MMM yyyy") & "' WHERE ([rfi].[id] = @id)"
Any clues you can leave me with?

Use parameterised queries or, better still, stored procedures.
 
B

bruce barker \(sqlwork.com\)

in your sql statement

UPDATE [rfi] SET [dateresolved] = dRes
WHERE ([rfi].[id] = @id)"

dRes is assumed to be a column name, as variables start with a "@". change
it to a var and pass the parameter.

-- bruce (sqlwork.com)
 
G

Guest

Mark,

Thanks. It works. Hurray !!!

I'm sorry I did not think to concatenate things. Just not thinking, I guess.

- Glenn

Mark Rae said:
Dim dtNow As DateTime = DateTime.Now
dRes = dtNow.ToShortDateString
Dim queryString As String = "UPDATE [rfi] SET [dateresolved] = dRes WHERE
([rfi].[id] = @id)"

dateresolved is a Date/Time data-type. The first snippet uses single
quotes
around the date but I would think the second snippet would be accepted as
a
string. If I debug the value of dRes, I receive the proper format of
"8/16/2006"

Yes, but SQL Server doesn't know that. If you're building up your SQL
dynamically, then you need to format it properly...

Dim queryString As String = "UPDATE [rfi] SET [dateresolved] = '" &
dRes.ToString("dd MMM yyyy") & "' WHERE ([rfi].[id] = @id)"
Any clues you can leave me with?

Use parameterised queries or, better still, stored procedures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top