Solved

Search datetime field

Posted on 2012-04-05
3
440 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help needed in sql query 4 25
I need help using SQL case statements in where clauses 3 23
sql query 5 37
Database-Scoped Permissions 2 10
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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

679 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