Solved

Find records with a specific date

Posted on 2008-10-29
7
356 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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