SQL syntax error on select between two dates - sql

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.

Related

How to add dynamic interval from the column

In Hive I need to add interval, taken from column:
SELECT CAST('2017-09-22 17:22:38' as timestamp) +
INTERVAL T.a minute
FROM ( SELECT 1 as a) as T;
I got this error:
cannot recognize input near 'interval' 'T' '.' in
expression specification.
All examples of using INTERVAL which I found are using the constant value after INTERVAL, but in my case this is dynamic value from the table.
You just need put the dynamic var inside brackets. Below code will work.
SELECT CAST('2017-09-22 17:22:38' as timestamp) +
INTERVAL (T.a) minute
FROM ( SELECT 1 as a) as T;

What causes error "Strings cannot be added or subtracted in dialect 3"

I have the query:
WITH STAN_IND
AS (
SELECT ro.kod_stanow, ro.ind_wyrob||' - '||ro.LP_OPER INDEKS_OPERACJA, count(*) ILE_POWT
FROM M_REJ_OPERACJI ro
JOIN M_TABST st ON st.SYMBOL = ro.kod_stanow
WHERE (st.KOD_GRST starting with 'F' or (st.KOD_GRST starting with 'T') ) AND ro.DATA_WYKON>'NOW'-100
GROUP BY 1,2)
SELECT S.kod_stanow, count(*) ILE_INDEKS, SUM(ILE_POWT-1) POWTORZEN
from STAN_IND S
GROUP BY S.kod_stanow
ORDER BY ILE_INDEKS
That should be working, but I get an error:
SQL Error [335544606] [42000]: Dynamic SQL Error; expression evaluation not supported; Strings cannot be added or subtracted in dialect 3 [SQLState:42000, ISC error code:335544606]
I tried to cast it into bigger varchar but still no success. What is wrong here? Database is a Firebird 2.1
Your problem is 'NOW'-100. The literal 'NOW' is not a date/timestamp by itself, but a CHAR(3) literal. Only when compared to (or assigned to) a date or timestamp column will it be converted, and here the subtraction happens before that point. And the subtraction fails, because subtraction from a string literal is not defined.
Use CAST('NOW' as TIMESTAMP) - 100 or CURRENT_TIMESTAMP - 100 (or cast to DATE or use CURRENT_DATE if the column DATA_WYKON is a DATE).

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

SQL Query Current Date Minus 180 days?

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.

last_day function not working in DB2

Database: DB2 v9.5.301.436
Requirement: I need to find number of days in a month.
Code:
select day(last_day(created))
from tablename
Error:
[Error Code: -440, SQL State: 42884] DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=LAST_DAY;FUNCTION, DRIVER=3.57.82. 2)
[Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-440;42884;LAST_DAY|FUNCTION, DRIVER=3.57.82
I checked the DB2 documentation, Which shows above function is available.
http://www-01.ibm.com/support/knowledgecenter/api/content/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_bif_lastday.html
the following works fine for me.
select day(last_day(current_timestamp))
from SYSIBM.SYSDUMMY1
are you sure created is a date?
The below code worked for me.
select day(date(date(created))-(day(date(created)) -1) days + 1 month -1 day) as maxdate , created from customer ;
Note: created column is a timestamfild
If created is a Date couln use below query.
select day(date(created)-(day(created) -1) days + 1 month -1 day) as maxdate , created from customer ;
last_day where introduced in 9.7. You can roll your own like:
create function myfun.last_day(d date)
returns date
return (d + 1 month) - day(d + 1 month) days
with t(d) as (
values date('2014-01-11')
union all
select d + 1 month
from t
where d<'2015-01-01'
)
select d, myfun.last_day(d) from t