Link to home
Start Free TrialLog in
Avatar of arthurh88
arthurh88

asked on

sql select where date is equal to or less than today

this isn't quite working

where t2.actionDate <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)

I want to select where t2.actiondate is less than or equal to today, ignoring hours, minutes and seconds on both sides of the comparison
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arthurh88
arthurh88

ASKER

< DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)

that was it exactly.  thank you!
first one:
>>DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
returns you starting of tomorrow so it checks dates less than or equal to today.

second one: converts dates to yyyymmdd format on both sides and checks.
Avatar of PortletPaul
where t2.actionDate < cast(getdate() as date)

either as a datetime or date, the field t2.actionDate can be directly compared to date

select
  'Using convert'                            as [Function used]
,                  convert(date, getdate())  as [Today cut to Whole Date No Time]
, convert(datetime,convert(date, getdate())) as [Today cut to Whole Date but With Time Also]

union all

select
  'Using cast'                               as [Function used]
,      cast(getdate() as date)               as [Today cut to Whole Date No Time]
, cast(cast(getdate() as date) as datetime)  as [Today cut to Whole Date but With Time]

Open in new window

 Function used     Today cut to Whole Date No Time     Today cut to Whole Date but With Time Also    
 ----------------  ----------------------------------  --------------------------------------------- 
 Using convert     2013-02-13                          2/13/2013 12:00:00 AM                         
 Using cast        2013-02-13                          2/13/2013 12:00:00 AM                         

Open in new window