Solved

Find records with a specific date

Posted on 2008-10-29
7
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 143

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 143

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 143

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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