how to get data of current week only in SQL server? -


i want records table stores current date when record inserted in current week only.

i have tried:

select pid  ,workdate  ,hours           ,empid             acb        empid=@empid , workdate between dateadd(day, -7, getdate()) , getdate() 

do this:

set datefirst 1 -- define beginning of week monday select [...] , workdate >= dateadd(day, 1-datepart(dw, getdate()), convert(date,getdate()))  , workdate <  dateadd(day, 8-datepart(dw, getdate()), convert(date,getdate())) 

explanation:

  • datepart(dw, getdate()) return number of day in current week, 1 7, starting whatever specified using set datefirst.
  • dateadd(day, 1-datepart(dw, getdate()), getdate()) subtracts necessary number of days reach beginning of current week
  • convert(date,getdate()) used remove time portion of getdate(), because want data beginning @ midnight.

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -