sql - Find dates that are not on the first or last day of the month -
i have table named locations
has column named effective_date
many dates many years, , want retrieve not on first day of month or last day of month.
if effective_date
columns of type date
, sql query return rows non-null effective_date
value other 1st or last day of month:
select t.effective_date , count(*) dbo.foo t 1 = 1 -- clarity -- after 1st day of month , t.effective_date > dateadd(day , 1-day( t.effective_date ) , t.effective_date ) -- , prior last day of month , t.effective_date < dateadd( day , -day( dateadd(month,1,t.effective_date) ) , dateadd(month,1,t.effective_date) )
if column carries time component it, is, of:
datetime
smalldatetime
datetime2
datetimeoffset
you'll want cover bases , modify query, like
select * dbo.foo t 1=1 -- added clarity -- effective date on or after 2nd of month , t.effective_date >= convert(date, dateadd(day , 2-day( t.effective_date ) , t.effective_date ) ) -- , prior last day of month , t.effective_date < convert(date, dateadd(day, -day( dateadd(month,1,t.effective_date) ) , dateadd(month,1,t.effective_date) ) )
Comments
Post a Comment