views:

35

answers:

1

Hi, I am not sure how to make an automaticed script that incrments all dates in a database. I was asked if the date is Friday, the script needs to increment the next business date to Monday, so I will need some logic in the script.

This can be easily done in C# or any other programming language. But, I was required that the script must be automaticed, identially a native SQL script (*.sql). I am confused whether this is possible at all.

+2  A: 
UPDATE yourtable
SET yourdate = dateadd(dd, 3, yourdate)
WHERE datepart(weekday,yourdate)=6

you can manipulate the WHERE clause however you want. It will add 3 days to each date in your database which corresponds to your WHERE clause.

Alexander
Just be careful that you check that weekday 6 is Friday on your server setup. Alternatively you can use `DATENAME(WEEKDAY, yourdate) = 'Friday'` - again, be careful as to which language setting is used.
Will A
UPDATE yourtableSET yourdate = dateadd(dd, CASE datepart(weekday,yourdate) WHEN 6 THEN 3 ELSE 1 END, yourdate)
pascal
Thanks, it works. You're a genius, didn't thought there would be an elegant solution like this.
Kinderchocolate