Solved

Find records with a specific date

Posted on 2008-10-29
7
379 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL: How to append a column for serialized JSON data? 2 45
SQL Lag Function DateDiff 2 23
Powershell error using sql agent job 24 31
TDE for SQL Web Edition 1 27
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

740 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