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
 Comparing two dates, return value on date range

Author  Topic 

Velnias
Yak Posting Veteran

58 Posts

Posted - 2008-10-14 : 10:47:07
Hi What I would like to do is as follows

1)Input Parameters are two dates
2) COmpare difference between the two dates
3) Retrieve the difference and calculate wether it is a) less than 0 Days, between 0 and 30 and greater than 30
4) Based on where the value drops return a different string
5) Retrieve the string in .net

this is in sql2005

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-14 : 10:56:50
[code]CREATE FUNCTION GetDiffStatus
(
@Start datetime,
@End datetime
)
RETURNS varchar(100)
AS
BEGIN
DECLARE @Diff int,@RetStatus varchar(100)

SET @Diff=DATEDIFF(dd,@Start,@End)
SET @RetStatus =CASE
WHEN @Diff=0 THEN 'FirstValue'
WHEN @Diff BETWEEN 0 AND 30 THEN 'Second Value'
ELSE 'ThirdValue'
END

RETURN @RetStatus
END[/code]

then call this as follows

[code]SELECT dbo.GetDiffStatus(startdatevalue,enddatevalue)[/code]

then use a variable in .net to receive this value
Go to Top of Page

Velnias
Yak Posting Veteran

58 Posts

Posted - 2008-10-14 : 11:41:55
Thanks a lot visakh16 worked nicely
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-14 : 12:43:12
quote:
Originally posted by Velnias

Thanks a lot visakh16 worked nicely


welcome
Go to Top of Page
   

- Advertisement -