[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3943
  • Last Modified:

SQL Query - Compare Date Part of DateTime Value in Table

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
Jimbo99999
Asked:
Jimbo99999
  • 4
  • 3
  • 2
  • +3
5 Solutions
 
Brian CroweCommented:
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
 
Jimbo99999Author Commented:
Ok, ours is pre-2008.
0
 
Brian CroweCommented:
Another option...

SELECT ...
FROM myTable
WHERE DATEADD(DAY, 0, DATEDIFF(DAY, 0, dateColumn)) >= @StartDate
   AND DATEADD(DAY, 0, DATEDIFF(DAY, 0, dateColumn)) <= @EndDate
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
JimFiveCommented:
Pre 2008 you can CAST(CAST(datefield as INT) as datetime) to strip off the time.
0
 
Brian CroweCommented:
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
 
JimFiveCommented:
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
 
Brian CroweCommented:
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
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
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
 
Jimbo99999Author Commented:
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
 
Scott PletcherSenior DBACommented:
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
 
Monica PCommented:
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
 
Jimbo99999Author Commented:
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now