Create date from integers in separate fields in athena aws - sql

I'm super new to athena, so bear with me. I have data stored as integers in three separate columns for year, month and day, as such:
year month day
2020 7 10
2020 7 11
2020 7 12
I'd like to turn these three fields into one date. How do I do that?
Thanks in advance!

One method is:
select date_parse(cast(year * 10000 + month * 100 + day as varchar(255)), '%Y%m%d')
This should also work:
select date(year || '-' || month || '-' || day)

You have to use the concat() function. You can see the documentation here.
Depending of the format that you want to use, this can change.
concat(year, '-' , month , '-', day)

Related

Combine with month and year column kept in different columns on Oracle

I keep the month and year information in different columns as numbers.
I want to go back 12 months on sysdate using these columns.
The table I used below as an example ,
Since we are in the 5th month now, I want to get the data up to the 6th month of last year.
versiyon table :
So as a result of the query ,
the following result should return.
First of all, I want to query by combining the year and month columns and going back one year from the current month as a date.
Convert the values to strings and concatenate and then use ADD_MONTHS(SYSDATE, -12) to get last year's date (which will get the correct date regardless of whether it is a leap year or not):
SELECT *
FROM versiyon
WHERE TO_CHAR(year, 'fm0000') || TO_CHAR(month, 'fm00')
>= TO_CHAR(ADD_MONTHS(SYSDATE, -12), 'YYYYMM')
db<>fiddle here
select *
from versiyon
where lpad(year, 4, '0') || lpad(month, 2, '0') >= to_number(to_char(sysdate - 365, 'yyyymm' ))
Here is a demo
As MT0 say it will not work for leap years... so you can check if it is leap year or not with case when then end clause and using mod(year, 4) to check. In this demo I have created a situation as it is 2020, to be exact like it is 29th of February 2020. where you can see what I am suggesting in action:
DEMO2
You can convert your year and month into an integer form of YYYYMM and compare:
SELECT *
FROM versiyon_table
WHERE (versiyon_table.year * 100) + versiyon_table.month > (EXTRACT(YEAR FROM SYSDATE) * 100) + EXTRACT(MONTH FROM SYSDATE)

Combine separate month and year in Redshift

base_table
month year
5 2021
10 2020
I want to combine the "month" and "year" column into a proper date column.
month_year
2021-05-01
2020-10-01
This seems to be a duplicate question for this:
Concatenate in PostgreSQL
I tried both:
to_date(CONCAT(year::text, '/'::text, month::text), 'YYYY/MM') as month_year
and
to_date(CONCAT(year, '/', month), 'YYYY/MM') as month_year
but maybe the solution does not work in Redshift.
Try:
(year::text || '-' || month::text || '-01')::date
This will use ISO format (2021-11-19) for the date.

Compare between two weeks of the year sqlite

I want to filter values from a table, between two weeks, like this:
select * from SalesWeekly
where SalesWeek BETWEEN '50' and '02'
Problem is, i have no idea how to specify week 50 is from year 2019,
and week 02 is from year 2020.
Assuming that you are storing the year in your table, say in table SalesYear, you could concatenate it with the week number and do string comparisons:
select *
from SalesWeekly
where SalesYear || '-' || SalesWeek BETWEEN '2019-50' and '2020-02'
For this to work, SalesWeek must be a 2-characters long string, left padded with 0 (so the 1st week should be '01', not '1').

Presto how to find start date given week

I want to find start date from given ISO week (which can range from 1-53, Monday as starting day) and year using Presto SQL query.
i.e. year - 2020 and week - 2 should return 06/01/2020
Is there any inbuilt function for this ?
Table structure:
select year, week from table1; // returns year and week from table table 1
There's no direct way for constructing a date from a year + week (there is an issue for this: https://github.com/trinodb/trino/issues/2287), but you can achieve what you want with the date_parse function.
For example:
WITH data (year, week) AS (VALUES (2020, 2))
SELECT CAST(date_parse(CAST(year AS varchar) || ':' || CAST(week AS varchar), '%x:%v') AS date)
FROM data
produces:
_col0
------------
2020-01-06
(1 row)
Using DATE_ADD and MAKEDATE you can achieve the result...
select DATE_ADD(MAKEDATE(year, 1), INTERVAL (week-1) WEEK) as start_date from <table_name>;
Martin's answer is almost there. Instead of using year, you should use year_of_week. Though with this change you'll have to make sure to not have a bug with weeks that bleed into the following or previous year i.e. last days of the previous year or first days of the next year.
year_of_week returns the year of the ISO week.
Here's an example:
WITH data (year, week) AS (VALUES (2020, 2))
SELECT CAST(date_parse(CAST(year_of_week AS varchar) || ':' || CAST(week AS varchar), '%x:%v') AS date)
FROM data
References:
https://prestodb.io/docs/current/functions/datetime.html#year_of_week
https://en.wikipedia.org/wiki/ISO_week_date
I think for partial weeks (when first days in a new year is still counted as week 53) this does not work:
Query failed (#20210624_142222_02859_zf75v): Cannot parse "2021:53": Value 53 for weekOfWeekyear must be in the range [1,52]
tested by this formula:
The date was on Jan 2,2021 which is still treated as week53 but in 2021...
CAST(date_parse(CAST(year(date(service_order_creation_date)) AS varchar) || ':' || CAST(week(date(service_order_creation_date)) AS varchar), '%x:%v') AS date)

Oracle SQL | Making a date field from two separate fields

How would I make a single date field using these two fields?
So if I had a table with this:
MONTH YEAR
2 2013
4 2012
2 2012
How would I combine the month and the year to make a field in that looks like this...
DATE
2013-02
2012-04
2012-02
In Oracle, I would just use concatenation and lpad():
select (year || '-' || lpad(month, 2, '0') ) as date