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.
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)
I tried with below query in Enterprise postgres which is working perfectly fine there but failling in open source(Community) version of postgres.
select (trunc(now())::date - trunc(now())::date) > '728 days' limit 1
I also did some modification in the query to make it compatible like
select (date(now())::date - date(now())::date) > '728 days' limit 1
but i got the error
ERROR: invalid input syntax for integer: "728 days"
what needs to be changed here?
In "stock" Postgres you need to use date_trunc() because trunc() only works on numbers. But given your expression using date_trunc() is essentially useless, because the cast ::date will achieve the same thing.
If you subtract two DATE values the result is an integer, not an interval, so you probably want:
select some_timestamp::date - current_date > 728
(assuming your now() expressions are simply placeholders for something different, because as shown, that expression doesn't make sense as the result will always be 0)
Or if you do want to work with timestamps (and an interval) you can use:
select date_trunc('day', some_timestamp) - now() > interval '728 day'
I am using SQLite, now how can I Select a query if I have a timestamp column that has this value for example 07:00 06/03/13 but I want only to select where timestamp 06/03/13.
This is my example of my query..
select
timestamp, pname
from
attendance
where
timestamp between `06/01/13` and `06/10/13`
Use date
select timestamp, pname
from attendance
where date(timestamp) = '2013-03-06'
When filtering on a timestamp column, you want this logic.
where YourField >= TheStartOfYourRange
and YourField < TheDayAfterTheEndOfYourRange
This is equivalent to the two answers that use the date() function, but will generally perform faster. Using functions in the where clause usually slows down production, especially on indexed fields.
SQLite support function date, which can be used to get the date part of a timestamp. For example:
SELECT * FROM mytable WHERE date(timestamp_col) = '2013-05-20'
See SQLite date and time functions documentation for more info.
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()
Is there a way to write a query equivalent to
select * from log_table where dt >= 'nov-27-2009' and dt < 'nov-28-2009';
but where you could specify only 1 date and say you want the results for that entire day until the next one.
I'm just making this up, but something of the form:
select * from log_table where dt = 'nov-27-2009':+1;
I do not believe there is one method that is portable to all RDBMSes.
A check in one of my references (SQL Cookbook) shows that no one RDBMS solves the problem quite the same way. I would recommend checking out Chapter 8 of that book, which covers all of the different methods for DB2, Oracle, PostgreSQL, MySQL.
I've had to deal with this issue in SQLite, though, and SQL Cookbook doesn't address that RDBMS, so I'll mention a bit about it here. SQLite doesn't have a date/time data type; you have to create your own by storing all date/time data as TEXT and ensure that your application enforces its formatting. SQLite does have a set of date/time conversion functions that allow you to add nominal date/times while maintaining the data as strings. If you need to add two time durations (HH:MM:SS) to each other, though, based upon data that you've stored in text columns that you are treating as date/time data, you'll have to write your own functions (search for "Defining SQLite User Functions") and attach them to the database at runtime via a call to sqlite3_create_function(). If you want an example of some user functions that add time values, let me know.
For MS SQL Server, check out DATEPART.
/* dy = Day of Year */
select * from log_table where datepart(dy, dt) = datepart(dy, '2009-nov-27');
With SQL Server, you could
Select * From table
Where dt >= DateAdd(day, DateDiff(day, 0, #ParamDate), 0)
And dt < DateAdd(day, DateDiff(day, 0, #ParamDate), 1)
As long as you are dealing with the date data type for the respective data type, the following will work:
t.date_column + 1
...will add one day to the given date. But I have yet to find a db that allows for implicit data type conversion into a date.
SELECT '12-10-2009' + 1
...will fail on SQL Server because SQL Server only performs the implicit conversion when comparing to a datetime data type column. So you need to use:
SELECT CONVERT(DATETIME, '12-10-2009') + 1
For Oracle, you'd have to use the TO_DATE function; MySQL would use something like STR_TO_DATE, etc.
Have a column that just has the date part (time is 00:00:00.000) and then you can add a where clause: WHERE dt = '2009-11-27'