SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sp_send_dbmail - date compare in query-variable


sp_send_dbmail - date compare in query-variable

Author
Message
thoyah
thoyah
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 15
Hello,

i have the following problem in a query-variable of the internal procedure sp_send_dbmail.

1.
I have declared a variable like this and i have assigned a value to this variable:

DECLARE @comparedate AS datetime
SET @comparedate = GETDATE() - 4 (today minus 4 days)

2.
Now i want to use the value of this variable @comparedate as a comparison value in a query-variable which than is used as a parameter of the procedure sp_send_dbmail. For that i have built this SET Command:

SET @abfrage='SELECT Anlagedatum, Artikel FROM S_artikel WHERE Firma = ''200'' AND Anlagedatum >=' + @comparedate + ' ORDER BY Anlagedatum'

Here i always get this error-message:

Meldung 241, Ebene 16, Status 1, Zeile 26
Error converting a string into a datetime value (translated by me with from german into english Language using my very less english knowledge)

Does anybody could help me and tell me if this can be handled like i do respectively how the select-Staement should be defined in a right way.

Thank You very much.

Info:
i work on a MS SQL Server 2005 Enterprise Edition SP4.

Thomas
amrut.kumbar 7070
amrut.kumbar 7070
SSC Veteran
SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)

Group: General Forum Members
Points: 207 Visits: 150
SELECT DATEADD(HH,14,CAST(DATEADD(DD,-1,CAST(GETDATE() AS DATE)) AS DATETIME)) AS "Yesterday'sDate@2O'Clock"
SrcName
SrcName
Old Hand
Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)

Group: General Forum Members
Points: 316 Visits: 394
it shoud be
DECLARE @comparedate AS varchar(20)
SET @comparedate = convert(varchar(20), dateadd(D,-4, GETDATE()), 102)

declare @abfrage varchar(2050)
SET @abfrage='SELECT Anlagedatum, Artikel FROM S_artikel WHERE Firma = ''200'' AND convert(varchar(20),Anlagedatum,102) >=''' + @comparedate + ''' ORDER BY Anlagedatum'

if your anlagedatum is datetime type, if not then convert first to datetime
and convert to varchar in format you want (102, 104 ...).

be cautious when use Dynamic query from sql injection
amrut.kumbar 7070
amrut.kumbar 7070
SSC Veteran
SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)

Group: General Forum Members
Points: 207 Visits: 150
This query is not for this question...Sorry;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search