Solved

SQL Query - Compare Date Part of DateTime Value in Table

Posted on 2013-12-06
12
3,484 Views
Last Modified: 2013-12-13
Good Day Experts!

It feels like my little quandry is feasible, but I have been unable to find a solution for it.

I have SQL table data in a date column:
12/6/2013 11:48:03 AM
12/6/2013 10:05:43 AM
12/6/2013 9:25:07 AM

The User enters a Date for Start and End.  
When I build my query, how do I just compare the date part of the above data?

If the User enters 12/6/2013 for both Start and End dates, I want to return all the above values.

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39701416
This is somewhat dependent on what version of SQL you are using.  SQL 2008 introduced a new DATE datatype that does not have a time component so the fastest way now to strip the time component from a DATETIME is to convert to DATE.

SELECT ...
FROM myTable
WHERE CAST(dateColumn AS DATE) >= @StartDate
   AND CAST(dateColumn AS DATE) <= @EndDate
0
 

Author Comment

by:Jimbo99999
ID: 39701423
Ok, ours is pre-2008.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
ID: 39701463
Another option...

SELECT ...
FROM myTable
WHERE DATEADD(DAY, 0, DATEDIFF(DAY, 0, dateColumn)) >= @StartDate
   AND DATEADD(DAY, 0, DATEDIFF(DAY, 0, dateColumn)) <= @EndDate
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 15

Expert Comment

by:JimFive
ID: 39702212
Pre 2008 you can CAST(CAST(datefield as INT) as datetime) to strip off the time.
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39702218
Casting to INT rounds up to the next day if the time is after 12:00PM

DECLARE @date DATETIME
SET @date = '1/1/2013 13:00'

SELECT CAST(CAST(@date AS INT) AS DATETIME)

returns '1/2/2013'
0
 
LVL 15

Assisted Solution

by:JimFive
JimFive earned 75 total points
ID: 39702221
Based on your question you can do this:

WHERE datefield between @StartDate and DateAdd(day, 1, @EndDate)

Adding 1 to the EndDate works because @EndDate has a time of midnight.  If you are concerned about things happening at midnight then use
datefield > @StartDate and Datefield < DateAdd(day,1,@EndDate)
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39702239
If performance is an issue than manipulating your parameters would make more efficient use of an index on the date field.

SELECT @EndDate =
   CASE
      WHEN @StartDate = @EndDate THEN DATEADD(DAY, 2, @EndDate)
      ELSE DATEADD(DAY, 1, @EndDate)
   END

SELECT ...
FROM myTable
WHERE dateColumn >= @StartDate
   AND dateColumn < @EndDate
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 50 total points
ID: 39703937
hi.. can you try this example

Yes you can still be able to compare them but this is certainly a bad practice because you will need to convert this strings into dates data type to be able to compare between them. If you have indexes define on the column, they will not be used anymore since the column will be converted and it will cuase slow performance on large database.

An example on comparing dates is like this:

SELECT *
FROM   tableName
WHERE  CONVERT(DATETIME, dateSTRColumn, XXX) > GETDATE()

where XXX is the current format of the date stored as string.
0
 

Author Comment

by:Jimbo99999
ID: 39706001
Thanks everyone for your repsonses.  We had other trouble last Friday that had to be put out.  So, I am just gettign back to this little project of mine.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 75 total points
ID: 39706362
You never want to perform a function on a table column unless it's absolutely unavoidable.  Using a function on a column is not a "choice" -- it's an absolute last resort.


DECLARE @Start_Date datetime
DECLARE @End_Date datetime
SET @Start_Date = '<user_entered_value>'
SET @End_Date = '<user_entered_value>'

SELECT
FROM dbo.tablename
WHERE
    date_column >= DATEADD(DAY, DATEDIFF(DAY, 0, @Start_Date), 0) AND
    date_column < DATEADD(DAY, DATEDIFF(DAY, 0, @End_Date) + 1, 0)
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 50 total points
ID: 39713425
Make use of below logic if suits., i have used like this ..

just append the time to the date from the code as like below
'12/9/2013 00:00:00' and '12/9/2013 23:59:59'

Pass this two values to the query as below.

SELECT ...
FROM myTable
WHERE  datecolumn between '12/9/2013 00:00:00' and '12/9/2013 23:59:59' 

Open in new window

0
 

Author Closing Comment

by:Jimbo99999
ID: 39716676
Thanks for all the help everyone. I have learned quite a bit about dates.  I will file this question in my knowledge base for future reference.

Thanks,
jimbo99999
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

828 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