Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 working with a variable

Author  Topic 

sven2
Yak Posting Veteran

57 Posts

Posted - 2009-12-04 : 04:54:08
Hello,

How can I change a date in a SQL to a variable?
For example:

This is a part of a working SQL:

strSQL = strSQL & " WHERE (DATEADD(Month, Item_stam.Geldigheidsperiode, Personeels_evaluaties.Toevoegdatum) < CONVERT(DATETIME, '2009-12-04', 102))"

This doesn't work anymore:

datetoday = date

strSQL = strSQL & " WHERE (DATEADD(Month, Item_stam.Geldigheidsperiode, Personeels_evaluaties.Toevoegdatum) < CONVERT(DATETIME, '" & datetoday & "', 102))"

What is wrong with the second SQL?

Thanks in advance,
Sven.

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-12-04 : 05:18:45
The problem is here,
You are trying to convert datetime datatype to datetime..

CONVERT(DATETIME, '" & datetoday & "', 102)
replace it with
CONVERT(VARCHAR, '" & datetoday & "', 102)


Balaji.K
Go to Top of Page

sven2
Yak Posting Veteran

57 Posts

Posted - 2009-12-04 : 05:53:43
Hello,

thank you ... like you wrote, convert datetime to datetime isn't working. I've got it working.

Best regards,
Sven.
Go to Top of Page

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-12-04 : 06:02:12
quote:
Originally posted by sven2

Hello,

thank you ... like you wrote, convert datetime to datetime isn't working. I've got it working.

Best regards,
Sven.



Ok.. You are always Welcome!!


Balaji.K
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 07:28:46
It is better you use parameter instead of concatenating the value

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -