Just a general date filter question, what are the valid operators for me to filter a specific timeframe as I had this error:
Cannot apply operator: date <= varchar(8)
code:
where utc_date >= '2022-04-01'
data type = date
'2022-04-01' is a varchar literal, you need to convert it to date for example by prefixing it with date:
where utc_date >= date '2022-04-01'
Related
I have this query that I used to be able to run in SQL server but cannot in PostgreSQL. Can you provide a query that will do the same as I cannot use DATEADD for Postgres.
select *
from VTable
where join_date>='1/1/2020' and join_date<'1/1/2021'
and join_date>= DATEADD(day,-30, removed_date)
and lremoved = 1
and countryid = '100010'
order by join_date asc
As documented in the manual you can add an integer to a date value which represents the number of days to add.
Also: ANSI date literals are preferred over locale specific date (or timestamp) values as they are unambiguous.
select *
from VTable
where join_date >= date '2020-01-01'
and join_date < date '2021-01-01'
and join_date >= removed_date - 30
and lremoved = 1
and countryid = '100010'
order by join_date asc
If join_date is a timestamp rather than a date, use an interval:
and join_date >= removed_date - interval '30 days'
The above is all standard ANSI SQL which Postgres happens to honor.
I want to filter the date range from April 1 to April 22, but only between 8:00 PM and 6:00 AM - the rest of the hours that are not in this range should be excluded. Do you have any ideas on how to do that? The only thing I have done so far is the date range but I don't know how to handle it next:
select * from cdm_service_request_logs
where inserted_at >= '20/04/01 20:00:00%'
and inserted_at < '20/04/22 06:00:00%'
ORDER BY ID DESC;
I think you want something like this:
select srl.*
from cdm_service_request_logs srl
where inserted_at >= date '2020-04-01' and
inserted_at < date '2020-04-022' and
extract(hour from inserted_at) not between 6 and 19;
In Oracle, the time comparison would often use strings:
where inserted_at >= date '2020-04-01' and
inserted_at < date '2020-04-022' and
(to_char(inserted_at, 'HH24:MI') >= '20:00' or
to_char(inserted_at, 'HH24:MI') < '06:00'
)
Note that the date comparison uses date constant values. This is correct. The % is not valid for a date, so your comparisons are as strings. I can only speculate that you are confusing like patterns with date constants. If so, that is just misguided.
I would like to convert a date in yyyy-mm-dd (Date format, length 10) to mm-yy format.
I have so far tried "right", "Left" and "convert" functions to no avail.
What's the exact syntax I need to use?
SELECT
LEFT (day,7) as 'YYYYMM'
FROM bi_core.fact_campaign_device_stats_daily
WHERE DAY = '2019-07-01'
SQL Error [3457] [42883]: [Vertica][VJDBC](3457) ERROR: Function LEFT(date, int) does not exist, or permission is denied for LEFT(date, int)
In Vertical, use to_char():
SELECT to_char(day, 'MMYY') as mmyy
FROM bi_core.fact_campaign_device_stats_daily
WHERE DAY = '2019-07-01'
For SQL database:
SELECT FORMAT(day, 'MM-yyyy') AS 'new_format'
FROM bi_core.fact_campaign_device_stats_daily
WHERE DAY = '2019-07-01'
Or also, to get it numeric:
SELECT YEAR("day") * 100 + MONTH("day") AS yearmonth
FROM bi_core.fact_campaign_device_stats_daily
WHERE "day" = '2019-07-01'
Not sure how to use the NOW() function in presto. Seems like it should be straight forward, but i'm getting no luck
SELECT DISTINCT field
FROM table
WHERE field BETWEEN '2019-01-01' and NOW()
field = varchar
You should match data types to avoid implicit conversions:
SELECT DISTINCT field
FROM table
WHERE CAST(field AS DATE) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
SELECT DISTINCT field
FROM table
WHERE DATE(field) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
I'm having trouble with my WHERE clause when trying to retrieve records for a certain month.
WHERE NVL(TO_CHAR(prsl.DEADLINE, 'YYYY-MM-DD'), TO_CHAR(prsl.SUBMIT_DATE, 'YYYY-MM-DD')) >= DATE '2015-11-01'
AND NVL(TO_CHAR(prsl.DEADLINE, 'YYYY-MM-DD'), TO_CHAR(prsl.SUBMIT_DATE, 'YYYY-MM-DD')) < DATE '2015-12-01'
Basically what this does is if DEADLINE is not null, use that to compare to the date range. If it is null, use the SUBMIT_DATE for comparison instead.
Using the above query, I get this error:
ORA-01861: literal does not match format string
You can try with EXTRACT:
EXTRACT(MONTH FROM prsl.DEADLINE) = 11
AND EXTRACT(YEAR FROM prsl.DEADLINE) = 2015
Another solution would be this:
WHERE TRUNC(COALESCE(prsl.DEADLINE, prsl.SUBMIT_DATE), 'MM') = DATE '2015-11-01'