Solved

Find records with a specific date

Posted on 2008-10-29
7
330 Views
Last Modified: 2012-05-05
Hi,

I'm trying to come up with the best way of finding records that have a datetime field = 1 date.

i.e.
declare @startdate as datetime
set @startdate = '28/10/2008'
select * from myTable Where fldMsgDate = @startdate

This doesnt work because the msgdate has minutes & seconds in it.

declare @startdate as datetime
set @startdate = '28/10/2008'
select * from myTable Where cast(fldMsgDate as int) = cast(@startdate as int)

does, as does the convert

select * from myTable Where convert(varchar(10),fldMsgDate,101) = convert(varchar(10),@startdate,101)

(Howeever they come up with different totals!).

Is there a function that I'm missing or do you really have to use cast or convert?

Whats teh best way? I've been struggling with this for years!  Thanks.


0
Comment
Question by:jmsjms
  • 3
  • 3
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22833439
small change:
declare @startdate as datetime
set @startdate = '28/10/2008'
select * from myTable Where fldMsgDate >= @startdate
 and fldMsgDate < dateadd(day, 1, @startdate)

Open in new window

0
 
LVL 5

Expert Comment

by:jfmador
ID: 22836090
Hello I'm not sure about the @startdate you set it using dd/mm/yyyy format, I don't know how the Sql Server will convert it. What happen if you use '05/11/2008', would it be the nov 5th or may 11th?

Maybe angelll could give us an advice about this.

0
 

Author Comment

by:jmsjms
ID: 22836687
So. from a performance view, Is it better to have two conditions in the where rather than use a convert to remove the minutes/seconds from the datetime field?

Could I be cheeky and also ask how would you handle  the WHERE statement if the @startdate = getdate() ?

Many thanks
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 22836776
>So. from a performance view, Is it better to have two conditions in the where rather than use a convert to remove the minutes/seconds from the datetime field?

better to have the 2 conditions. removing the time from the field in the where condition will just make your query a full-table scan.

>Could I be cheeky and also ask how would you handle  the WHERE statement if the @startdate = getdate() ?

same method... or inline:

select * from myTable Where fldMsgDate >= convert(datetime, convert(varchar(10), getdate(), 120), 120)

 and fldMsgDate < dateadd(day, 1, convert(datetime, convert(varchar(10), getdate(), 120), 120))

Open in new window

0
 

Author Comment

by:jmsjms
ID: 22839264
Thanks, thats very useful with the amount of records I have to handle.

So using >= and < is better than between ... and ... for dates?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22839354
>So using >= and < is better than between ... and ... for dates?
no, that part is the same...
0
 

Author Closing Comment

by:jmsjms
ID: 31511288
Thanks very much!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now