Solved

FILTER BY DATE FIELD sql 2005

Posted on 2009-04-06
6
483 Views
Last Modified: 2012-05-06
Hello Guys,

I am having problem to filter through datetime field

I was doing this:

Select * from Table
where convert(char(10),datefield,112)='20090403'

But doing this I will have my index off

The dates that I have recorded is in this format:

2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00
2008-04-21 20:00:00

I have a time together

How can I filter the rows ?
I tried this but it didn't work

select * from table
where datefield='2009-04-21'

None rows

Thanks
0
Comment
Question by:hidrau
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24076103
this should do:
Select * from Table
where datefield >= convert(datetime,'20090403', 112)
  and datefield < dateadd(day,1,convert(datetime,'20090403', 112))

Open in new window

0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 24076113
This wont work

select * from table
where datefield='2009-04-21'

as the value '2009-04-21' will be converted into datetime value of '2009-04-21 00:00:00.000' which is not equal to your date value.

Using a scalar function convert will not use the Index on that column.
In order to use your index effectively your query should be something like this:

select * from table
where datefield between cast('2009-04-21' as datetime) and DATEADD (DD, 1, DATEADD(MS, -2, CAST('2009-04-21' as datetime)))
0
 
LVL 12

Expert Comment

by:udayakumarlm
ID: 24076128
use
select * from table
where datediff(dd,datefield,'2008-04-01') = 0
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24076149
udayakumarlm,
   FYI, Even your query wont use the Index on the datefield column.
0
 
LVL 12

Expert Comment

by:udayakumarlm
ID: 24076254
small correction
select datefield from table
where datediff(dd,datefield,'2008-04-01') = 0 will use index
not
select * from table
where datediff(dd,datefield,'2008-04-01') = 0
hidrau, if it is performace that you are looking at then don't use *, if the fields you specify instead of * are all indexed then SQL will use index scan else it will use clustered index scan.
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 31566965
thanks
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stored procedure query with if 27 36
Record open by another user 6 50
Problem with SqlConnection 4 160
sql query help 2 45
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now