Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server and date + 1 result

In case of the SQL Server. Say, if I have a table with [myDate] column that is of the type datetime, what would adding 1 to it mean in the following context?

SELECT * 
  FROM [myTable] 
 WHERE [myDate] + 1 > @someDate
like image 482
ahmd0 Avatar asked Jul 16 '11 19:07

ahmd0


People also ask

How do I add 1 date in SQL?

SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.

How do I get just the first result in SQL?

To return only the first row that matches your SELECT query, you need to add the LIMIT clause to your SELECT statement. The LIMIT clause is used to control the number of rows returned by your query. When you add LIMIT 1 to the SELECT statement, then only one row will be returned.

What do 1 and 0 mean in SQL?

Learn MySQL from scratch for Data Science and AnalyticsThe condition 1=0 can be used to stop the query from returning any rows. It returns empty set.

What does order by 1 do in SQL?

it simply means sorting the view or table by 1st column of query's result.


2 Answers

It adds one day (exactly 24 hours)

However, your query is more correct this way. The function or processing on the column usually invalidates index usage if there is one of that column

WHERE [myDate] > @someDate - 1

OR

WHERE [myDate] > DATEADD(day, -1, @someDate)
like image 161
gbn Avatar answered Sep 20 '22 20:09

gbn


Use the DATEADD function (see MSDN docs for DATEADD for details):

SELECT * 
  FROM dbo.myTable
 WHERE DATEADD(D, 1, myDate) > @someDate

DATEADD allows you to add any of the usual date parts (day, month, year) and also time parts (hours, minutes, seconds) etc.

like image 42
marc_s Avatar answered Sep 24 '22 20:09

marc_s