?
Solved

Find records with a specific date

Posted on 2008-10-29
7
Medium Priority
?
392 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

752 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