sql server - SQL Date Diff disregarding Year -


i want make select, users birthday(date field) less 30 days.
best way to it? tried datediff, don't know how put year aside.

thanks

you use datepart function dayofyear datepart value.

edit: honestly, there boundary issue in previous answer (many damien): e.g. 2010-12-25 , 2011-01-07 => difference should less 30 days, datepart(dayofyear, @date) - datepart(dayofyear, [birthday]) < 30 condition skip record. added additional contition answer:

datepart(dy, @d) - datepart(dy, [birthday]) < 30 or (    datepart(mm, @d) = 12 ,    datepart(dy, dateadd(m, 1, @d)) - datepart(dy, dateadd(m, 1, [birthday])) < 30 ) 

it adds 1 month each date in case when month part of first date december , compares difference.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -