sql - WHy doesn't my stored procedure work for same dates? -
why doesn't stored procedure work when pass same dates (i.e. , dates same) although have records these dates doesn't. why ?
select audits.pk_audits_auditid, audits.fk_webusers_audits_userid, web_users.name username, districts.pk_districts_districtid, districts.districtname, orgs.pk_orgs_orgid, orgs.orgname, audits.auditsuseractivity, audits.auditsuseripaddress, audits.auditsusersystemmacaddress, convert(varchar, audits.auditsystementrydatetime, 113) auditsystementrydatetime audits inner join web_users on web_users.userid= audits.fk_webusers_audits_userid inner join orgs on orgs.pk_orgs_orgid= web_users.fk_orgs_orgid_webusers inner join districts on districts.pk_districts_districtid= orgs.fk_districts_orgs_districtid convert(varchar, dateadd(day, -1, audits.auditsystementrydatetime)) >= @fromdate , convert(varchar, audits.auditsystementrydatetime) <= @todate
it doesn't work because auditsystementrydatetime
presumably has time. suspect want logic as:
convert(date, dateadd(day, -1, audits.auditsystementrydatetime)) >= @fromdate , convert(date, audits.auditsystementrydatetime) <= @todate
in general, don't want convert datetime
characters purpose other output.
but, better way write logic is:
where audits.auditsystementrydatetime >= dateadd(day, 1, @fromdate) , audits.auditsystementrydatetime < dateadd(day, 1, @todate)
by keeping functions on constants, sql server can use index where
clause. hmmm, adding of 1 day explain why no results. perhaps want:
where audits.auditsystementrydatetime >= @fromdate , audits.auditsystementrydatetime < dateadd(day, 1, @todate)
this makes sense.
Comments
Post a Comment