Query works in Enterprise postgres version but not in Community Edition - sql

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'

Related

How to make H2 db display a number instead of date/interval

Is it possible to make H2 db display a number instead of date/interval to get the same short answer as oracle/mysql?
Running this sql in oracle /H2 console:
select trunc(sysdate+5) - trunc(sysdate) noOfDays from dual;
Response in H2:
NOOFDAYS
INTERVAL '5 00:00:00' DAY TO SECOND
Response in oracle:
NOOFDAYS
5
I´m looking to get the short 5 from H2 as I got form Oracle.
We have followed http://www.h2database.com/html/features.html#compatibility
with oracle compability mode set, but does not get the described "DATE data type is treated like TIMESTAMP(0) data type."
To_Char won't work as the interval must work round months. We have tried changing the sql , different dialects but no success.

Get information between two different times Oracle

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)

SQL where DATEDIFF

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.

is there an easy way to generate a table_query based off of a timezone difference in BigQuery?

Legacy SQL
I'm using GBQ's legacy SQL to query tables dynamically using the TABLE_QUERY function. I dynamically generate the table name to query based on CURRENT_TIMESTAMP. For example, I select devices from the past 14 days of hit data in tables that are partitioned by quarter (ie. mydataset.hit_data_[1-4]).
Standard SQL
I need to convert the timezones to PST. GBQ Standard SQL has TIME ZONE conversions. Switching to Standard SQL, I am able to convert timezones using the GBQ Standard SQL. But if I now try to use a TABLE_QUERY in the same query, to do what I was doing in the Legacy SQL version, I get:
Error: Table-valued functions are not supported
Using both
Is there a way to have the best of both worlds? I would like to query mydataset.hit_data_3 and mydataset.hit_data_4 based on the current timestamp in Q4, if the previous 14 days overlap into Q3.
SELECT
device
FROM
TABLE_QUERY(mydataset, 'table_id = CONCAT(\"hit_data_\", STRING(QUARTER(TIMESTAMP(CURRENT_TIMESTAMP, "America/Los_Angeles")))) OR table_id = CONCAT(\"hit_data_\", STRING(QUARTER(DATE_ADD(TIMESTAMP(CURRENT_TIMESTAMP, "America/Los_Angeles"), INTERVAL -14 DAY)))) ')
WHERE
DATE(date_time) BETWEEN DATE(DATE_ADD(TIMESTAMP(CURRENT_TIMESTAMP, 'America/Los_Angeles'), INTERVAL -14 DAY))
AND DATE(CURRENT_DATE())
;
It looks ugly, but in GBQ it should be valid.
Standard SQL doesn't support TABLE_QUERY or TABLE_DATE_RANGE functions. Instead it supports wildcard tables with a special pseudo column _TABLE_SUFFIX:
You should be able to rewrite your query with a WHERE clause on _TABLE_SUFFIX pseudo column
https://cloud.google.com/bigquery/docs/querying-wildcard-tables
with BigQuery Standard SQL you should use _TABLE_SUFFIX pseudo column that allows you to chose table(s) to query from
Below is direction to go
SELECT *
FROM `mydataset.hit_data_*`
WHERE (_TABLE_SUFFIX = STRING(QUARTER(TIMESTAMP(CURRENT_TIMESTAMP, "America/Los_Angeles")))
OR _TABLE_SUFFIX = STRING(QUARTER(DATE_ADD(TIMESTAMP(CURRENT_TIMESTAMP, "America/Los_Angeles"), INTERVAL -14 DAY)))
)
AND DATE(date_time) BETWEEN
DATE(DATE_ADD(TIMESTAMP(CURRENT_TIMESTAMP, 'America/Los_Angeles'), INTERVAL -14 DAY))
AND DATE(CURRENT_DATE())
Note: you need to make sure you are using functions supported by Standard SQL
For example instead of
QUARTER(TIMESTAMP(...))
you should use
EXTRACT(QUARTER FROM TIMESTAMP(...))

finding time difference in DB2

How can I write a query in DB2 for following thing:
The difference between current timestamp and a timestamp field in dB should be >=4 hours AND <= 24 hours
Someone suggested this but it's not working.
select * from tableName where
date <= DATEADD([hour], -4, CURRENT_TIME) and
date date >= DATEADD([hour], -24, CURRENT_TIME)
But it's not working. It's giving following error.
SQL0104N An unexpected token "[hour]" was found following "ortdate <=
DATEADD(". Expected tokens may include: "<func_arg_list>". SQLSTATE=42601
select *
from table t
where t.tscolumn between current timestamp - 24 hours
and current timestamp - 4 hours
Use just Hour instead of [hour]
select * from tableName where
date <= DATEADD(Hour, -4, CURRENT_TIME) and
date date >= DATEADD(Hour, -24, CURRENT_TIME)
DB2 doesn't like square brackets around name - that is a MS SQL Server mannerism.
The only reference to DATEADD() in the DB2 9.7 Info Centre (oh, beg its pardon: Center - one day, American's will learn to spell correctly) is in 'All of the following expressions are in the package com.ibm.alphablox.bloxbuilder.lib.expression', which is puzzling. I suspect the search is erroneous - though going to the SQL Manual and finding the functions listed there, DATEADD is conspicuously absent, so maybe it isn't.
So, you are going to have to manual bash for the DB2 syntax. But, if anything is going to work, it is likely to involve:
DATEADD(HOUR, -4, CURRENT_TIME)
rather than any square brackets. However, a somewhat more extensive search, including the RedBook on DB2 and Oracle Compatibility, does not show DATEADD as a function that is supported by DB2. So, the DATEADD route is doomed to ... have problems.
Since DB2 (still) doesn't have a proper (SQL standard) INTERVAL type, you are into investigating 'durations'. See DevX for an explanation - but beware the number of cookies the site '.qnsr.com' wants to set. And read the manuals at the DB2 Info Centre.