2

I'm having a hard time doing this query. I want to compare dates in my query, dates from my DB are in this format:
(MM/DD/YYYY HH:MM:SS AM)
I want to compare this date with tomorrow's day, today plus one.
My questions are:

How do I declare tomorrow's date in sql server?
How would you compare these two dates?

Thank you!! =D

EDIT : DATES in DB are VarChar =S

gbn
  • 408,740
  • 77
  • 567
  • 659
hyeomans
  • 4,402
  • 5
  • 22
  • 29

4 Answers4

5

declare tomorrow's date : DATEADD(dd,1,getdate())

compare dates :

WHERE column >= CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 102))
    AND column < CONVERT(datetime, CONVERT(varchar, DATEADD(day, 2, GETDATE()), 102))
anishMarokey
  • 11,041
  • 2
  • 32
  • 46
5

Assumes datetime datatype for columns

WHERE
   MyCol >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)
   AND
   MyCol < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 2)

This (yyyy-mm-dd) removes the time component to test of MyCol is tomorrow

2009-10-06 00:00:00 <= MyCol < 2009-10-07 00:00:00

You don't strip time from MyCol: this will affect performance and disallow index usage etc

Efficiency of remove time from datetime question, which is why I used this format and avoid varchar conversions...

Edit:

Avoiding implicit conversions and string matching

10/06/2009 <= MyCol < 10/07/2009

WHERE
   MyCol >= CONVERT(char(10), DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1), 101)
   AND
   MyCol < CONVERT(char(10), DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 2), 101)

Of course, it'll fail at the end of December...

Community
  • 1
  • 1
gbn
  • 408,740
  • 77
  • 567
  • 659
1

I would think your dates are most likely to be in SQL Server's datetime datatype, and that the format you give is just the default string representation.

Typically, I use something like:

SELECT *
FROM tbl
WHERE datecol = CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 101))

However, if your datetimes include a time piece, you need to use something like this:

SELECT *
FROM tbl
WHERE datecol >= CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 101))
    AND datecol < CONVERT(datetime, CONVERT(varchar, DATEADD(day, 2, GETDATE()), 101)) 

There are other date arithmetic tricks you can use. There are plenty here on SO if you look for SQL dates

Cade Roux
  • 85,870
  • 40
  • 177
  • 264
0

SQL Server allows you to declare variables

DECLARE @TomorrowsDate DateTime

And you can set it to the current date and time

SET @TomorrowsDate = DATEADD (Day, 0, GETDATE())

For tomorrow's date (without time)

SET @TomorrowsDate = DATEADD (Day, 1, CONVERT (VARCHAR, GETDATE(), 101))

To use it in a query with a column without declaring a variable

SELECT Column1, Column2
FROM YourTableName
WHERE DateColumn BETWEEN DATEADD (Day, 0, CONVERT (VARCHAR, GETDATE(), 101)) 
    AND DATEADD (Day, 1, CONVERT (VARCHAR, GETDATE(), 101))
Raj More
  • 45,769
  • 31
  • 127
  • 192