SQL Query Current Date Minus 180 days? - sql

Query
I'm getting a syntax error:
SELECT "LastFilterDate"
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" < DATE_SUB(CURDATE(),INTERVAL 180 DAY)
I want to select the LastFilterDateS that are older than the current date minus 180 days (6 months).
I'm using LibreOffice base.
error in question:
SQL Status: HY000 Error code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or
SQL_TOKEN_LIKE
Sandesh gave a fantastic link on the subject, but I'm still getting a syntax error.
variations I've tried:
SELECT *
FROM Filter Cartridge Updates
WHERE LastFilterDate BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
SELECT *
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )

The problem is that you're using MYSQL syntax for an SQL that is not MYSQL.

You can find some help on the LibreOffice website. I found this: =DATEDIF("1974-04-17";"2012-06-13";"d") which yields the number of days between 2 dates.
So you could change your where clause to be (if this allows substitution - you'll have to test that), to:
Where =DATEDIF(last_filter_date,current_date;"d") > 180
Note: I only put stubs in for the dates, you'll have to format those correctly.

Related

How can I translate this legacy SQL to standard SQL in BigQuery?

I need to translate this to BigQuery. Can anyone help? Thanks
IF DATEDIFF('day',DATEADD('day',7,snapshot_date),TODAY(),'monday')>=1
THEN 1
END
This is the error I'm getting.
The Google BigQuery Standard SQL database encountered an error while
running this query. Query execution failed: - Syntax error: Expected
"(" but got identifier "DATEDIFF" at [2:25]
Try this:
case when DATE_DIFF(DATE_ADD(snapshot_date, INTERVAL 7 DAY), CURRENT_DATE(), WEEK(MONDAY)) >= 1 then 1 end
Try this (docs):
IF(DATE_DIFF(DATE_ADD(snapshot_date, INTERVAL 7 DAY), CURRENT_DATE(), WEEK(MONDAY)) >= 1, 1, NULL)

SQL syntax error on select between two dates

I'm trying to get records which between two dates. Below is my query
select * from my_data
where name = 'customer' AND
(time between (('2021-03-24'::date - '1 month'::interval) AND '2021-03-24'::date))
However I'm getting a syntax error
ERROR: syntax error at or near ")"
LINE 1: ...03-24'::date - '1 month'::interval) AND '2021-03-24'::date))
^
What is the correct way of writing the query to return rows 1 month older than a given date?
You can drop the parentheses:
select *
from my_data
where name = 'customer' AND
time between '2021-03-24'::date - '1 month'::interval AND '2021-03-24'::date;
The specific issue is that the range for between does not accept parens.

query to subtract date from systimestamp in oracle 11g

I want to perform a subtraction operation on the date returned from another query and the system time in oracle SQL. So far I have been able to use the result of another query but when I try to subtract from systimestamp it gives me the following error
ORA-01722: invalid number
'01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
Below is my query
select round(to_number(systimestamp - e.last_time) * 24) as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
I have also tried this
select to_char(sys_extract_utc(systimestamp)-e.last_time,'YYYY-MM-DD HH24:MI:SS') as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
I want the difference between the time intervals to be in hours.
Thank you for any help in advance.
P.S. The datatype of ATTR_VALUE is VARCHAR2(150). A sample result of e.last_time is 2016-09-05 22:43:81796
"its VARCHAR2(150). That means I need to convert that to date"
ATTR_VALUE is a string so yes you need to convert it to the correct type before attempting to compare it with another datatype. Given your sample data the correct type would be timestamp, in which case your subquery should be:
(
select to_timestamp(ATTR_VALUE, 'yyyy-mm-dd hh24:mi:ss.ff5') as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
)
The assumption is that your sample is representative of all the values in your CONFIG table for the given keys. If you have values in different formats your query will break on some other way: that's the danger of using this approach.
So finally after lots of trial and errors I got this one
1. Turns out initially the error was because the data_type of e.last_time was VARCHAR(150).
To find out the datatype of a given column in the table I used
desc <table_name>
which in my case was desc CONFIG
2. To convert VARCHAR to system time I have two options to_timestamp and to_date. If I use to_timestamp like
select round((systimestamp - to_timestamp(e.last_time,'YYYY-MM-DD HH24:MI:SSSSS')) * 24, 2) as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
I get an error that round expects NUMBER and got INTERVAL DAY TO SECONDS since the difference in date comes out to be like +41 13:55:20.663990. To convert that into hour would require a complex logic.
An alternative is to use to_data which I preferred and used it as
select round((sysdate - to_date(e.last_time,'YYYY-MM-DD HH24:MI:SSSSS')) * 24, 2) as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
This returns me the desired result i.e. the difference in hours rounded off to 2 floating digits

Oracle sql: ORA-01830 when date calculation happens

I want to perform following oracle SQL query
SELECT t1.Technology, count(t1.trax_id) as "Current number of items", to_char(to_date(max(round((SYSDATE - t1.time_event) * 24 * 60 * 60)),'sssss'),'hh24:mi:ss') as "max_ages"
from dm_procmon t1
group by t1.Technology;
The problem is the date substration formula.
I susbtract 2 dates from each other. This gives me a decimal value
(like 0,00855605). I want the value back to be a date value. So I
converted this first to a Number (Decimal > Number) and than to a
char (Number > Char) Finally from a char to date (Char > Date).
But when I perform the action I receive
Error report -
SQL Error: ORA-01830: Datumnotatieafbeelding eindigt voordat de gehele
invoerstring is geconverteerd.
01830. 00000 - "date format picture ends before converting entire input string"
What do I do wrong?
you try to convert to_date(%number of seconds%, 'sssss'), that is the problem. just use TO_CHAR(MAX(TO_DATE('20000101','yyyymmdd')+(SYSDATE - t1.time_event)),'hh24:mi:ss'); this will function correctly for intervals < 1day
here is a general solution without any limitations on maximum interval size:
select Technology, cnt as "Current number of items", FLOOR(x) || ':' || TO_CHAR(TO_DATE('20000101','yyyymmdd')+(x - FLOOR(x)),'hh24:mi:ss') as "max_ages"
from
(select t1.Technology, COUNT(t1.trax_id) cnt, MAX(SYSDATE - t1.time_event) x
from dm_procmon t1
group by t1.Technology);
FLOOR(x) returns the days and the rest (x - FLOOR(x)) is added to a constant date and transformed to hours, minutes and seconds
The following will give a DAY TO SECOND INTERVAL for all dates within about 68 years of one another:
SELECT t1.technology, COUNT(t1.trax_id) AS "Current number of items"
, NUMTODSINTERVAL(ROUND((SYSDATE - MIN(t1.time_event) * 24 * 60 * 60), 'SECOND') AS "max_ages"
FROM dm_procmon t1
GROUP BY t1.technology;
Note that rather that using MAX(SYSDATE - time), I used SYSDATE-MIN(time) (logically the same). The advantage of using this instead of TO_CHAR() is that you can then use the value returned in DATE/INTERVAL arithmetic.

How to get one day ahead of a given date?

Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that
For example,
SELECT *
from table
where date = date("2010-07-29")
How to do one day before without changing the string "2010-07-29"?
I searched and get some suggestion from web and I tried
SELECT *
from table
where date = (date("2010-07-29") - 1 Day)
but failed.
MySQL
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
AND '2010-07-29'
Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).
SQL Server
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
AND '2010-07-29'
Oracle
SELECT *
FROM TABLE t
WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
AND TO_DATE('2010-07-29', 'YYYY-MM-DD')
I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.
If you're using Oracle, you can use the + and - operators to add a number of days to a date.
http://psoug.org/reference/date_func.html
Example:
SELECT SYSDATE + 1 FROM dual;
Will yield tomorrow's date.
If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.
Depends of the DateTime Functions available on the RDBMS
For Mysql you can try:
mysql> SELECT DATE_ADD('1997-12-31',
-> INTERVAL 1 DAY);
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:
Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)
Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.
In PL SQL : select sysdate+1 from dual;