sql server - Query all items for the week in SQL -


i have query selects items given week , sets date start day of week. in cases query sets incorrect date value given item , have traced problem in either datediff or dateadd functions.

the query

select dateadd(week, datediff(week, 0, datetimevalue), 0) newdatetimevalue 

let's take date march 27 2016 example. datediff returns value of 6065 , dateadd value returns march 28 2016.

for date march 26 2016, datediff returns 6064 , dateadd march 21, 2016.

to me sounds firstdayofweek issue , sql server thinks sunday first day of week , giving different values sunday , saturday (march 27, 2016 sunday).

i tried set first day of week by

set datefirst 1 

but didn't make difference , sql returned same results.

so causes sql function behave , ideas how fix it?

i found out datediff doesn't respect datefirst setting , assumes week starts on sunday. reason why sample case works works. question has suggested workaround: is possible set start of week t-sql datediff function?


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -