Hi everyone,

This is just a quick script for a scalar function to return the first day of the current week.
With a little modification you could add a parameter so you could pass it a date and have it return the first day of that week.
It's also easy to adjust the numeric value for the days in the event your week starts on Sunday.

Have fun!

CREATE FUNCTION F_wtd () 
returns DATE 
AS 
  BEGIN 
      DECLARE @res AS DATE 

      SELECT TOP 1 @res = Cast(Dateadd(dd, ( CASE Datename(dw, Getdate()) 
                                               WHEN 'Monday' THEN 1 
                                               WHEN 'Tuesday' THEN 2 
                                               WHEN 'Wednesday' THEN 3 
                                               WHEN 'Thursday' THEN 4 
                                               WHEN 'Friday' THEN 5 
                                               WHEN 'Saturday' THEN 6 
                                               WHEN 'Sunday' THEN 7 
                                             END * -1 ), Getdate()) AS DATE) 

      RETURN @res 
  END 

 

0
0
0
s2sdefault