Question about Parsing and Case Statement

  • I have to write a SQL script where one of the requirement is

    DeliveryIDcharacters 1-7 not equal to 'aaaaaaa' AND

    (If Today is Monday and 2nd character = M OR

    If Today is Tuesday and 3rd character = T

    If Today is Wednesday and 4th character = W

    If Today is Thursday and 5th character = T

    If Today is Friday and 6th character = F

    If Today is Saturday and 7th character = S

    If Today is Sunday and 1st character = S)

    Do i have to use case statement or functions

  • This solution might be slow for large volumes of data but does what you need. Any option will be slow because you have seven values in one column and that's against the normalization rules. You're using at least 7 bytes of space when using a 7 bit columns will need less than one byte.

    I'm assuming @@DATEFIRST = 7.

    SELECT *

    FROM (VALUES ('aaaaaaa'),

    ('SMTWTFS'),

    ('SaTWaFS'),

    ('SMaWaFS'),

    ('aMTWTFa'))x(schedule)

    WHERE SUBSTRING(schedule, DATEPART(dw, GETDATE()),1) = LEFT( DATENAME( dw, GETDATE()), 1)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply