Solved

Search datetime field

Posted on 2012-04-05
3
423 Views
Last Modified: 2012-04-05
Hello,

I need to search only the date in this datetime column. Columns are RequestDateTime, Id and NAME FROM REQUEST

My values in the table are

1 Tom 2012-04-05 15:00
2 Bob 2012-04-05 10:00
3 Mark 2012-03-20 11:00
4 Kirk  2012-04-25 09:00

I want to only search by the date values =  2012-04-05.

This will eventionly go into stored procedure as parameter.

Thanks
0
Comment
Question by:lulubell-b
3 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 250 total points
ID: 37814063
That would be something like:


SELECT * FROM REQUEST WHERE RequestDateTime = '2012-04-05';


--or if you use SQL 2008

SELECT * FROM REQUEST WHERE cast(RequestDateTime as date) = cast('2012-04-05' as date);
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 37814081
from a date param I usually create a start and end date parameter:

declare @startdate datetime
declare @enddate datetime

set @startdate = DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), -1)  -- replace getdate with your param
set @enddate =  DATEADD(s, - 1, DATEADD(DAY, DATEDIFF(DAY, - 1, GETDATE()), 0))  --replace getdate with your param

this will give you

4/5/2012 12:00:00 AM
and 4/5/2012 11:59:59 PM

from there you can do

select * from table where RequestDateTime between @startdate and @enddate
0
 

Author Closing Comment

by:lulubell-b
ID: 37814132
These are both great.

Thank you
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

896 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

15 Experts available now in Live!

Get 1:1 Help Now