Tuesday, March 27, 2012

Date Query

Hi
I am trying to write a SQL statement that gives me a date in the
future. The requirements are:-
The date should be Wednesday at 17.30.
The date must be at least 5 full days in advance.
So if it is Monday 1st, return Wednesday 10th 17.30.
If it is Friday 5th 17.29, return Wednesday 10th 17.30.
If it is Friday 5th 17.31, return Wednesday 17th 17.30.
Any help would be appreciated!
Thanks
G
Try this one here (partly tested)
ALTER Function NextWantedDay
(
@.Startdate datetime,
@.EstWeekday INT,
@.EstTime DATETIME
)
RETURNS DATETIME
AS
BEGIN
DECLARE @.EstDate DATETIME
IF (DATEPART(dw,@.Startdate) = @.EstWeekday AND
CONVERT(VARCHAR(10),@.Startdate,108) <=
CONVERT(VARCHAR(10),@.EstTime,108))
RETURN
CONVERT(VARCHAR(50),CONVERT(Varchar(10),@.Startdate ,112),113) + @.EstTime
BEGIN
SET @.Startdate = @.Startdate +1
WHILE DATEPART(dw,@.Startdate) <> @.EstWeekday
BEGIN
SET @.Startdate = @.Startdate + 1
END
END
RETURN CONVERT(VARCHAR(50),CONVERT(Varchar(10),@.Startdate ,112),113) +
@.EstTime
END
Select dbo.NextWantedDay(getdate(),1,'09:00')
HTH, Jens Suessmeyer.

No comments:

Post a Comment