Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select all date that is lesser than today

Tags:

sql-server

I have a table (mytable) that contains:

id, date

and some rows (the Id is for the example):

4235          null
3563   2013-11-27 08:02:53.917
3143   2013-11-27 01:04:51.917
1455          null
5223   2013-11-26 08:02:53.917
2123   2013-11-25 08:02:53.917

I want to select all the rows that their date before today or the date is null.

so in my example, if I run the query on 2013-11-27 I want to get:

4235          null
1455          null
5223   2013-11-26 08:02:53.917
2123   2013-11-25 08:02:53.917

I think to do it with:

select case when
(
(select DATEPART(yyyy,dailybudgetexceeded) from sensplit_commercial_goal where 
commercialGoalId = 'dbe20d71-e304-4989-8524-5feef61d32a7') >= YEAR(GETDATE()) 
or...

but maybe there is a shorter way.

any help appreciated!

like image 954
Alon Shmiel Avatar asked Nov 27 '13 08:11

Alon Shmiel


People also ask

How do I query today's date in SQL?

To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34 .


Video Answer


3 Answers

select * from mytable
where [date] < Convert(date, getdate())  or [date] is null

SQLFiddle Demo

like image 131
Raj Avatar answered Sep 28 '22 06:09

Raj


select * 
from sensplit_commercial_goal
where commercialGoalId = 'dbe20d71-e304-4989-8524-5feef61d32a7'
and (dailybudgetexceeded < getdate() or dailybudgetexceeded is null)
like image 21
juergen d Avatar answered Sep 28 '22 07:09

juergen d


SELECT *
FROM sensplit_commercial_goal
WHERE dailybudgetexceeded  < CONVERT(date, GETDATE()) OR dailybudgetexceeded  IS NULL
like image 43
kingkong0924 Avatar answered Sep 28 '22 08:09

kingkong0924