Solved

Search datetime field

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Managing SQL log files, SQL Server 2014 6 55
SQL Server memory Issue 7 74
Row-Level Security 2 19
BULK INSERT most recent CSV 19 19
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

705 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

12 Experts available now in Live!

Get 1:1 Help Now