Presto - how is there an alternative to to_char like postgresql? - sql

I need to build a query in presto that could look back the trailing 70 days, the table I am working with is storing the dates in the format of 'YYYYMMDD'.
in postgresql, I can just simply write the where clause as
where date >= to_char(current_date - 70, 'YYYYMMDD')
and it'll pull in the date 70 days ago in a YYYYMMDD format.
However, in PrestoSQL, that function doesn't seem to exist, is there an alternative to this?

You can do this with date_format():
where date >= date_format(current_date - interval '70' day, '%Y%m%d')
Note that storing dates as strings is not a good practice at all - you should be using the proper date-like datatype - and then you won't need to do conversions at all.

You would just use date arithmetic:
where date >= current_date - interval '70' day
I'm not sure why you would want to involve strings in comparisons that are strictly date related.

Related

Date Format syntax in where clause in Teradata

I have a table with date column in which date is updated in this format - 11/21/2022.
How can I get the results for the last 15 days using this date column in Teradata? Looks like need to change the date format in where clause.
I was using below query which does not work with this format
select *
from table_A
WHERE date BETWEEN (CURRENT_DATE - INTERVAL '30' DAY)
AND CURRENT_DATE
This does not give any results.
Assuming your date column be text, then you must first convert it to a bona fide date using the TO_DATE() function:
SELECT *
FROM table_A
WHERE TO_DATE("date", 'MM/DD/YYYY') BETWEEN (CURRENT_DATE - INTERVAL '30' DAY) AND CURRENT_DATE;
Note that ideally you should be storing your dates in actual date columns, not as text. This would avoid the need to call the clunky TO_DATE() function.

HIVE select where date between, but its a string YYYYMMDD, not numeric date format?

so I'm trying to learn how to deal with this.
the key partition column is "date" as a string (YYYYMMDD).
so its a character string, not a numeric date format.
I'm having trouble understanding how I can WHERE filter off this by normal date ranges like
where date > CURRENT_DATE -30
or
WHERE date BETWEEN '20210512' AND '20210517'
How does this work? Should the date column be converted first?
This is SQL in a HIVE database.
The best way is to convert the comparison date to a string. Many databases support to_char():
where date > to_char(current_date - 30, 'YYYYMMDD')
Those that do not would have some other function to convert the date to a string.
Note that this formulation allows the use of indexes and table partitions to optimize the query.

How to transform Oracle DateTime to Date

For instance, I have a datetime like this '2016-04-02 00:00:00' and another like this '2016-04-02 15:10:00'. I don't care about the time-part, I want them to match just by the date-part.
I have tried with date(), to_date, datepart, nothing works.
Do it like this:
where yourField >= the start of your date range
and yourField < the day after the end of your date range
Edit starts here:
While you could use trunc, as suggested by others, bear in mind that filtering on function results tends to be slow.
Truncating the date to day should do the trick. Documentation here:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions201.htm
For example
SELECT TRUNC(SYSDATE, 'DAY') FROM DUAL;
As others have said - there is no separate "date" data type in Oracle. A pure "date" is stored as a date with the time portion set to 00:00:00 (midnight at the beginning of the day), and TRUNC(date) will take any date and truncate the time to 00:00:00, so if you need to compare two dates you can write
where trunc(date_1) = trunc(date_2)
If your tables are very large and you need to do these comparisons often, this is not ideal, because wrapping column values within function calls (like date_1 within a TRUNC) prevents the use of an index you may have on the date_1 column. If you need to compare dates in two columns you may not have much of a choice, but if you compare to a fixed date (or something like SYSDATE) you may be better off with something like
where date_1 >= trunc(sysdate) and date_1 < trunc(sysdate) + 1
Here you are not using trunc on the column value, so if there's an index on the column, Oracle is free to use it - and trunc(sysdate) is computed only once, not for every single row. "+1" by the way means "add one day".
TO_DATE converts a string to a date; if you apply TO_DATE to a value that is already a legitimate date, you will get unexpected results because Oracle will first convert your true date to a string and then back to date again, and since these conversions require a date FORMAT for strings, and the formats Oracle assumes for conversion from date to string and from string to date may not match, .... you get the idea. As far as I know, DATE() (a FUNCTION) and DATEPART do not exist in Oracle; when you use a new language, keep Google close by and use it often.
If you input a date with no time component, for example TO_DATE('04-apr-2016, 'dd-mon-yyyy'), then the implicit time is 00:00:00 so you don't need to apply TRUNC() to it.
Good luck!

issue with add_month date using PostgreSQL

I am trying to use the add_month function but getting an error. I want to get the number of visits between [CAL_DATE - 13 months] and [CAL_DATE]. The format of the dates are as following: 2007-14, 2010-05, 2009-04 and etc. this is the error I am getting
"Bad time stamp external representation '2009-11"
and here is the code I am using. I can't seem to figure out the issue.
CAL_DATE BETWEEN add_months(CAL_DATE,-13) AND CAL_DATE.
I am using netezza database.
Presumably add_months expects a date as its first argument and returns a date. You don't have dates, you have YYYY-MM strings so you have two problems:
add_months won't know what to do with a YYYY-MM string.
BETWEEN won't know what to do with a date and a YYYY-MM string.
If you want to use add_months then you'll have to give it a date and convert the date it gives you to one of your YYYY-MM strings with something like this:
to_char(add_months((cal_date || '-01')::date, -13), 'yyyy-mm')
Appending -01 to your strings should give you a string representation of the first of that month and you should be able to cast that to a date with ::date. Then a to_char to convert the result of add_months back to your YYYY-MM format.
Alternatively, since add_months isn't really doing anything useful for you here, just use a PostgreSQL interval for the month adjustment:
to_char((cal_date || '-01')::date - interval '13 months', 'yyyy-mm')

current_date usage

I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Can some one give me a basic example Please.
You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....
In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.