SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sql to bypass "dayofweek" and "days" functions


sql to bypass "dayofweek" and "days" functions

Author
Message
pma85
pma85
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 9
This one's a bit more challenging!

We are transitioning to sql server from a different type of system which made use of "days" and "dayofweek" functions which transform dates to unique serial numbers and extracts the day of week and turns it into a number so that we can use dates for calculations. But sql server apparently doesn't have these functions available. Does anyone know another way to reconstruct this calculation?

CASE
WHEN DAYOFWEEK(convert(date,kp_aspirationalduedate)) = 7 THEN DAYS(convert(date,kp_aspirationalduedate)) - DAYS(CURRENT DATE) - 1
WHEN DAYOFWEEK(convert(date,kp_aspirationalduedate)) = 1 THEN DAYS(convert(date,kp_aspirationalduedate)) - DAYS(CURRENT DATE) - 2
ELSE DAYS(convert(date,kp_aspirationalduedate)) - DAYS(CURRENT DATE) END AS "DAYS"

Thanks!
Joe Torre
Joe Torre
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6198 Visits: 1142

SELECT

CASE

WHEN DatePart(dw,(Convert(date, kp_aspirationalschedule)))=7 THEN DateDiff(dd, GetDate(), kp_aspirationalschedule)-1

WHEN DatePart(dw,(Convert(date, kp_aspirationalschedule)))=1 THEN DateDiff(dd, GetDate(), kp_aspirationalschedule)-2

ELSE DateDiff(dd, GetDate(),Convert(date, kp_aspirationalschedule))

END AS [DAYS]



ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)

Group: General Forum Members
Points: 77966 Visits: 9020
DayOfWeek 7 is ambiguous in SQL Server, since SQL allows the week to start on any day that you specify.

Instead, I'd strongly urge you to use known day values. SQL's base date is day 0 / 1900-01-01, a Monday. Therefore, a days displacement from that date can be used to determine day of week while ignoring current SQL date settings.

My best guess is that your code is adjusting Saturday back one day and Sunday back two days, if not, adjust the code below as needed. 0=Mon,6=Sun. The CTE is just for initial use; after a while, you won't need it.


;WITH days AS
(
SELECT 5 AS Saturday, 6 AS Sunday
)
...
CASE
WHEN DATEDIFF(DAY, '19000101', convert(date,kp_aspirationalduedate)) % 7 = Saturday THEN ...
WHEN DATEDIFF(DAY, '19000101', convert(date,kp_aspirationalduedate)) % 7 = Sunday THEN ...
ELSE ... END AS "DAYS"



SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search