Solved

Search datetime field

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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