April 13, 2011 at 12:10 pm
How Can I convert this code with IF structure ?
DECLARE @Offset SMALLINT
SELECT @Offset = CASE WHEN @Start_time = 'Daily' THEN null WHEN @Start_time =
'Weekly' THEN -7 WHEN @Start_time = 'Monthly' THEN -30
END
SELECT StartParam = CONVERT(datetime,DATEADD(dd,@Offset,GetDate()),103), EndParam =
CONVERT(datetime,GetDate(),103)
April 13, 2011 at 12:13 pm
Robson Brandão (4/13/2011)
How Can I convert this code with IF structure ?DECLARE @Offset SMALLINT
SELECT @Offset = CASE WHEN @Start_time = 'Daily' THEN null WHEN @Start_time =
'Weekly' THEN -7 WHEN @Start_time = 'Monthly' THEN -30
END
SELECT StartParam = CONVERT(datetime,DATEADD(dd,@Offset,GetDate()),103), EndParam =
CONVERT(datetime,GetDate(),103)
This is obviously a code snippet to a larger function/proc, but why would you want to convert it? What are you hoping to gain going to an IF/THEN/ELSE structure that you're not getting from the CASE function?
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
April 13, 2011 at 12:28 pm
For some reason, people want to use IF you put any other procedure, even if the performance goes down.
April 13, 2011 at 12:38 pm
Like Craig said, this is not optimal.
But to use IFs, you could replace the Select statement with:
IF @Start_time = 'Daily'
SET @Offset = NULL
IF @Start_time = 'Weekly'
SET @Offset = -7
IF @Start_time = 'Monthly'
SET @Offset = -30
Edit > Craig, not Brandie
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
April 13, 2011 at 12:45 pm
ok, i've seen people knee jerk on things like that...
You could mask the CASE statement as a SELECT (or JOIN it to your other data) like this:
/*--Results
(No column name)StartParamEndParam
DailyNULL2011-04-13 14:44:10.230*/
DECLARE @Start_time AS VARCHAR(30)
SET @Start_time = 'Daily'
SELECT
@Start_time,
StartParam = CONVERT(datetime,DATEADD(dd,Offset,GetDate()),103),
EndParam = CONVERT(datetime,GetDate(),103)
FROM (SELECT 'Daily' AS ST,NULL AS Offset UNION ALL
SELECT 'Weekly' AS ST, -7 UNION ALL
SELECT 'Monthly' AS ST, -30 ) X
WHERE @Start_time = ST
Lowell
April 13, 2011 at 12:53 pm
thanks..
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply