Link to home
Start Free TrialLog in
Avatar of alexking
alexkingFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I Compare Dates without the time component in an SQL qeury?

I have a datatype datetime that stores a ledger date. It does also store the time of the transaction, so
15-10-2008 13:45 etc

I need to get all transactions between 2 dates ignoring the time, so

      SELECT  [LedgerDate]
                                    ,[Quantity]
                                    ,[Description]
                     FROM [MyDb].[dbo].[vw_Ledger]
      WHERE [ledgerdate]>= @FromDate AND
                                  [ledgerdate]<= @ToDate

Where @FromDate are passed in format 01-10-2008 and 30-10-2008, no times
How do I make it ignore the time in the [Ledgerdate]?
SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada 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
ASKER CERTIFIED SOLUTION
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
SOLUTION
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
if you are concerned abt the performance, dont go for casting, try my solution
Avatar of alexking

ASKER

In the cast what is the difference between 101 and 102? I am in the UK and use the format dd-mm-yy
alexking,

did u try my answer?

aneeshattingal
Alex, if you need to ignore the time part, you may have to use a cast or convert function with the datetime filed.  you may try the following code:

SELECT  [LedgerDate]
    ,[Quantity]
    ,[Description]
FROM [MyDb].[dbo].[vw_Ledger]
WHERE CONVERT(VARCHAR,[ledgerdate],112) >= CONVERT(VARCHAR,@FromDate,112)
AND CONVERT(VARCHAR,[ledgerdate],112) < CONVERT(VARCHAR,@ToDate+1,112)
Can someone tell me if it will make any difference if I use Convert with 101 or 102 for the purpose of this comparison if I use the format dd-mm-yyyy (UK format)?
is the same...
select convert(datetime, getdate(),101)
select convert(datetime, getdate(),102)

The more important is the format you have configured in your SQL..
Thankyou you all gave me the answer and all solutions seem to work - most important I have also learnt more about it so thankyou all.