Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateDiff based on Weekday 1

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
Hello,

I have a script that uses a DateDiff to calculate the dfference between the time and order is created inthe database and the schedule shipdate. This works fine during the weekdays Monday - Thursday but on Friday the date diff between Friday and Monday is 3 days.

DATEDIFF(D,CONVERT (VARCHAR(30),SH.CREATION_DATE_TIME_STAMP,1), SH.SCHEDULED_SHIP_DATE) BETWEEN 0 AND 1

Is there a way to have the datediff look at between 0 and 1 Monday to Thursday and 0 to 3 is the date is Friday?

Thanks in advance
RJL
 
You can use the DATEPART function to get the day of the week, but be careful and read what SET DATEFIRST does before you use this function.


Here's a working example to illustrate how you can get the day of week. To utilize it with your code, you can use a CASE statement.

Code:
SET DATEFIRST 7

DECLARE @Date DATETIME
SELECT @Date = '20081205'


IF DATEPART(dw, @Date) = 6
BEGIN
	PRINT 'Its Friday'
END
ELSE
BEGIN
	PRINT 'Its not Friday'
END

Here's an example with a where clause
Code:
SELECT *
FROM SomeTable
WHERE DATEDIFF(D,CONVERT (VARCHAR(30),SH.CREATION_DATE_TIME_STAMP,1), SH.SCHEDULED_SHIP_DATE) BETWEEN 0 AND
  CASE
    WHEN DATEPART(dw, SomeDateColumn) = 6 THEN  3  --It's Friday
    ELSE  1 --It's not Friday
  END
 
Worked like a charm. Just what the doctor order.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top