SQL Server : function to display previous Friday's date if @date is not a Friday -


i newbie advanced sql, need write svf in sql server return previous friday's date if parameter date entered not friday.

for example, today wednesday, 7/1/2015, function should return previous friday 6/26/2015. suppose today friday 7/3/2015, function return 7/3/2015 (no change).

ask if can edit following code:

create function fndatecheck (@date datetime) returns @fridaydate date begin      declare @fridaydate      if         return        else if          return   end go 

i found reference post, quite similar asking it's not function: get last friday's date unless today friday using t-sql

declare @date datetime; set @date = '2012-08-10'  select      case when datepart(weekday, @date) > 5             dateadd(day, +4, dateadd(week, datediff(week, 0, @date), 0))           else dateadd(day, -3, dateadd(week, datediff(week, 0, @date), 0))       end 

thanks much!!

the script found works fine. need write function.

create function fndatecheck (     @date datetime ) returns datetime     begin          return (select case             when datepart(weekday, @date) > 5                 dateadd(day, + 4, dateadd(week, datediff(week, 0, @date), 0))              else                 dateadd(day, - 3, dateadd(week, datediff(week, 0, @date), 0))         end)     end go 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -