Solved

Search datetime field

Posted on 2012-04-05
3
451 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
[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 Comments
 
LVL 40

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

623 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