there was a question coming up to my mind. Is there any possibility to use the current timestamp instead of a selected date in the Where Clause?
SELECT this, that
FROM here
WHERE start>='2010-07-01'
I thought it would be sth. like: start='now()' or curdate() or curtime().
Anything I found was that they're used in the Select Clause, but I need it in Where.
Any help is much appreciated.
Flora
SELECT this, that
FROM here
WHERE start >= NOW()
You can use any of the following three functions as per your requirements:
SELECT NOW(),CURDATE(),CURTIME()
The output of this query is -
NOW() | CURDATE() | CURTIME()
---------------------+----------------+----------
2008-11-11 12:45:34 | 2008-11-11 | 12:45:34
Edited:
you can use these functions in Where clause as instructed.
Sure you can:
WHERE start >= CURDATE()
You can use any expression in the WHERE clause, using any inbuilt Date-and-Time function.
I'd use
WHERE start >= current_timestamp
Just because this should work in every DBMS. Don't know about NOW() though, maybe that's a standard function?
Update: well now I know NOW() does not work at least in Oracle, so I'd definitely go with current_timestamp, current_date etc, because these are in the standard. I've done a couple of DBMS migrations (DB2 -> MySQL, MySQL -> Oracle etc) and I'm glad we used the standards -compliant SQL where ever possible, which made the migrations relatively painless.
You shouldn't to quote a function name
Use function names like this:
SELECT this, that FROM here WHERE
start >= NOW();
SELECT this, that FROM here WHERE
start >= CURRENT_DATE();
... WHERE v_date=CURRENT_DATE()
Related
I am making a modification to an Oracle Query, where I need to get the information found from the query date and minute up to 30 minutes ago.
For example, I made the query at 16:35, so I need it to show me the information found from 16:05 to 16:35.
I did something like this, but I don't have the result I need.
Also, how can I make it find everything loaded with current date? This is what I have done with no result
AND FV.FEC_CAR = dateadd(minute,-30,getdate()) ORDER BY F.N_FILE DESC
Thank you very much in advance
dateadd and getdate aren't valid in Oracle's SQL dialect. That looks like SQL Server syntax but it probably works in some other database as well.
In Oracle, you'd do
fv.fec_car > sysdate - interval '30' minute
or
fv.fec_car > sysdate - 30/24/60
I find the interval syntax far clearer personally
As far as I can understand and interpret, you need to see the data at a point in the past before applying some modification to your table. This case,
SELECT *
FROM tab AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '30' MINUTE
might be used to see the values of half an hour before modification if undo_retention parameter's value of your database is big enough(without forgetting that it does not guarantee to return a result even if the value is big enough)
Hi guys i have table in mysql
I need filter results where created_at and updated at difference between those timestamps 2 hours or less
I'am trying to use:
SELECT *
FROM `imoniu_r_padaliniai`
WHERE DATEDIFF(hour, created_at, updated_at) <= 2;
But i get syntax errot how achieve this?
Just use direct comparisons:
where updated_at <= created_at + interval 2 hour
In MySQL and MariaDB, datediff() only handles date differences. If you want arbitrary time differences, you need timestampdiff(). However, I recommend using direct comparisons instead.
The three-argument form of datediff() is the syntax in SQL Server (and a handful of other databases), not MySQL.
I'd like to know when I write a query like
SELECT id FROM users WHERE expiry > curdate()
How can I work out what the curdate() is returning? I just switched servers of a site and it appears to be throwing it out, by perhaps returning a different curdate().
Thanks
SELECT CURDATE();
From the documentation, CURDATE():
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
UNIX_TIMESTAMP() isn't an ANSI standard keyword but an addition to the MySQL syntax. However, since I'm supporting multiple DB's, is there an ANSI standard way to write UNIX_TIMESTAMP();
Thanks
As far as I know, no.
Every database handles this differently.
For example, in Oracle, you have to manually generate the timestamp with something like:
SELECT (sysdateColumn - to_date('01-JAN-1970','DD-MON-YYYY')) * (86400) AS alias FROM tableName;
In MSSSQL:
SELECT DATEADD(s, yourDateColumn, '19700101') AS alias FROM tableName
In PGSQL:
SELECT date_part('epoch', timestampColumn) AS alias FROM tableName
Edit: as AlexKuznetsov pointed out, there are two totally different usages of MySQL's UNIX_TIMESTAMP() function. I assumed the latter, UNIX_TIMESTAMP(date) (for native format to epoch conversion) for the above answer.
You failed to define UNIX_TIMESTAMP
If the following is true:
UNIX_TIMESTAMP(date) returns the value of the argument as seconds since '1970-01-01 00:00:00'
then use DATEDIFF function
I need to do a date comparison in Mysql without taking into account the time component i.e. i need to convert '2008-11-05 14:30:00' to '2008-11-05'
Currently i am doing this:
SELECT from_days(to_days(my_date))
Is there a proper way of doing this?
Yes, use the date function:
SELECT date(my_date)
select date(somedate) is the most common.
If you need to accommodate other formats, you can use:
SELECT DATE_FORMAT(your_date, '%Y-%m-%d');
In PostgreSQL you use the TRUNC() function, but I'm not seeing it for MySQL. From my brief Googling, it looks like you'll need to cast your DATETIME value to be just DATE.
date_col = CAST(NOW() AS DATE)
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
Just a simple way of doing it date("d F Y",strtotime($row['date'])) where $row['date'] comes from your query
You could use ToShortDateString();