sql server - Convert DATETIME to 8 digit INT in format YYYYMMDD SQL -
need join 2 tables. 1 table has 'datekey' column int in format yyyymmdd dimdate table, , other table has 'createdon' column datetime. casting datetime date because way group seems work correctly. keep getting error "operand type clash: date incompatible int" because of "on d.datekey = bbb.datekey" line.. suggestions on easy workaround?
select distinct convert (date,convert(char(8),d.datekey )) date , isnull(bbb.reccount,0) dimaccount dimdate d left outer join( select cast(createdon date) datekey , count(*) reccount dimaccount group cast(createdon date) )bbb on d.datekey = bbb.datekey d.datekey '2017%' group d.datekey ,bbb.reccount having max(datediff(dd, convert(date, cast(d.datekey char(8)), 101), getdate())) > -1 order date desc
something should started
from dimdate dd join dimaccount da on cast(da.createdon date) = cast(cast(dd.datekey char(8)) date) dd.year = 2017 -- or can filter on da.createdon >= '20170101'
in fact, if dimdate table has actual date column wouldn't have cast datekey @ all.
Comments
Post a Comment