Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
How do I use a varible in place of the "dd", or "hh" as I would like to pass the SP the amount of time to look back. Heres an example:ALTER PROCEDURE [dbo].[CHSP_OR_UPDATES_001] @Interval VARCHAR(5) = "dd"ASDECLARE @DATETHEN datetimeSET @DATETHEN = DATEADD(@Interval, - 1, @DATENOW)The DateAdd function won't work.Once again many thanks!
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-01-13 : 10:16:12
you've either use dynamic sql or like below
ALTER PROCEDURE [dbo].[CHSP_OR_UPDATES_001] @Interval VARCHAR(5) = 'dd'ASDECLARE @DATETHEN datetimeSET @DATETHEN = CASE @Interval WHEN 'hh' THEN DATEADD(hh, - 1, @DATENOW) WHEN 'dd' THEN DATEADD(dd, - 1, @DATENOW) WHEN 'ss' THEN DATEADD(ss, - 1, @DATENOW) ... ENDGO
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2009-01-13 : 10:21:36
Also, 1 hour is 3600 seconds and 1 day is 86400 seconds.E 12°55'05.63"N 56°04'39.26"
Ahh yes visakh16, sorry you've actually given me that code before but due to massively changing what I had it got lost in there somewhere. Many thanks again :-)