Comparing results with today's date?

2019-01-17 05:29发布

Is there a way to use the Now() function in SQL to select values with today's date?

I was under the impression Now() would contain the time as well as date, but today's date would have the time set to 00:00:00 and therefore this would never match?

9条回答
三岁会撩人
2楼-- · 2019-01-17 05:46

OK, lets do this properly. Select dates matching today, using indexes if available, with all the different date/time types present.

The principle here is the same in each case. We grab rows where the date column is on or after the most recent midnight (today's date with time 00:00:00), and before the next midnight (tomorrow's date with time 00:00:00, but excluding anything with that exact value).

For pure date types, we can do a simple comparison with today's date.

To keep things nice and fast, we're explicitly avoiding doing any manipulation on the dates stored in the DB (the LHS of the where clause in all the examples below). This would potentially trigger a full table scan as the date would have to be computed for every comparison. (This behaviour appears to vary by DBMS, YMMV).

MS SQL Server: (SQL Fiddle | db<>fiddle)

First, using DATE

select * from dates 
where dte = CAST(CURRENT_TIMESTAMP AS DATE)
;

Now with DATETIME:

select * from datetimes 
where dtm >= CAST(CURRENT_TIMESTAMP AS DATE)
and dtm < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
;

Lastly with DATETIME2:

select * from datetimes2
where dtm2 >= CAST(CURRENT_TIMESTAMP AS DATE)
and dtm2 < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
;

MySQL: (SQL Fiddle | db<>fiddle)

Using DATE:

select * from dates 
where dte = cast(now() as date)
;

Using DATETIME:

select * from datetimes 
where dtm >= cast((now()) as date)
and dtm < cast((now() + interval 1 day) as date)
;

PostgreSQL: (SQL Fiddle | db<>fiddle)

Using DATE:

select * from dates 
where dte = current_date
;

Using TIMESTAMP WITHOUT TIME ZONE:

select * from timestamps
where ts >= 'today'
and ts < 'tomorrow'
;

Oracle: (SQL Fiddle)

Using DATE:

select to_char(dte, 'YYYY-MM-DD HH24:MI:SS') dte
from dates 
where dte >= trunc(current_date)
and dte < trunc(current_date) + 1
;

Using TIMESTAMP:

select to_char(ts, 'YYYY-MM-DD HH24:MI:SS') ts
from timestamps
where ts >= trunc(current_date)
and ts < trunc(current_date) + 1
;

SQLite: (SQL Fiddle)

Using date strings:

select * from dates 
where dte = (select date('now'))
;

Using date and time strings:

select dtm from datetimes
where dtm >= datetime(date('now'))
and dtm < datetime(date('now', '+1 day'))
;

Using unix timestamps:

select datetime(dtm, 'unixepoch', 'localtime') from datetimes
where dtm >= strftime('%s', date('now'))
and dtm < strftime('%s', date('now', '+1 day'))
;

Backup of SQL Fiddle code

查看更多
一夜七次
3楼-- · 2019-01-17 05:46

Not sure exactly what you're trying to do, but it sounds like GETDATE() is what you're after. GETDATE() returns a datetime, but if you're not interested in the time component then you can cast to a date.

SELECT  GETDATE()
SELECT  CAST(GETDATE() AS DATE)
查看更多
爷、活的狠高调
4楼-- · 2019-01-17 05:57

You can try this sql code;

   SELECT [column_1], [column_1], ...    
    FROM (your_table)
     where date_format(record_date, '%e%c%Y') = date_format(now(), '%e%c%Y') 
查看更多
乱世女痞
5楼-- · 2019-01-17 05:58

Just zero off the time element of the date. e.g.

select    DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

I've used GetDate as that's an MSSQL function, as you've tagged, but Now() is probably MySQL or you're using the ODBC function call, still should work if you just replace one with the other.

查看更多
疯言疯语
6楼-- · 2019-01-17 06:01

If you have a table with just a stored date (no time) and want to get those by "now", then you can do this:

SELECT * FROM tbl WHERE DATEDIFF(d, yourdate, GETDATE())=0

This results in rows which day difference is 0 (so today).

查看更多
Viruses.
7楼-- · 2019-01-17 06:05

Not sure what your asking!

However

SELECT  GETDATE()

Will get you the current date and time

SELECT  DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

Will get you just the date with time set to 00:00:00

查看更多
登录 后发表回答