Combine separate month and year in Redshift - sql

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.

Related

Compare date filed with month and year in Postgres

I have a date field in one of my tables and the column name is from_dt. Now I have to compare a month and year combination against this from_dt field and check whether the month has already passed. The current database function uses separate conditions for the month and the year, but this is wrong as it will compare month and year separately. The current code is like this
SELECT bill_rate, currency FROM table_name WHERE
emp_id = employee_id_param
AND EXTRACT(MONTH FROM from_dt) <= month_param
AND EXTRACT(YEAR FROM from_dt) <= year_param
Now the fromt_dt field has value 2021-10-11. If I give month_param as 01 and year_param as 2022, this condition will not work as the month 10 is greater than 1, which I have given. Basically, I need to check whether 01-2022 (Jan 2022) is greater than r equal to 2021-10-01(October 1st, 2021). It would be very much helpful if someone can shed some light here.
If you just want to check whether one date is >= then another:
# select '2022-01-01'::date >= '2021-10-11'::date;
?column?
----------
t
If you want to restrict to year/month then:
select date_trunc('month','2022-01-01'::date) >= date_trunc('month', '2021-10-11'::date);
?column?
----------
t
Where the date_trunc components are:
select date_trunc('month','2022-01-01'::date) ;
date_trunc
------------------------
2022-01-01 00:00:00-08
select date_trunc('month','2021-10-11'::date) ;
date_trunc
------------------------
2021-10-01 00:00:00-07
See Postgres date_trunc for more information.
Assuming the given year_param and month_param are integers you can use the make_date function to create the first of the year_month and date_trunc to get the first on the month from the table. Just compare those values. (See date functions) So:
select bill_rate, currency
from table_name
where emp_id = employee_id_param
and date_trunc('month',from_dt) =
make_date( year_param, month_param, 01);

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)

Create date from integers in separate fields in athena aws

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)

Oracle SQL: selecting date field without day (Only Month and Year)

I need to select values from a Database where I have a complete Date. Now I have to get this Date without the Day because I have to group and count them per Month.
I did it like this, but this will get me the Month like for January with 1 and I need 01...
(extract(YEAR,Month from ak.date ) || '.' ||extract(Month from ak.date) ) as Datum
Use the TO_CHAR function for this:
TO_CHAR(ak.date, 'YYYY.MM') as Datum
Another way:
TRUNC(ak.date, 'MM')
Advantage of this is that date sorting and date arithmetic still work.

How to convert month number to full month name in oracle?

I have a month column in my table. The month numbers are stored in this month column like 1 for january, 2 for feb and so on.
How do I convert the numbers into month names such as january, february, march etc.
SELECT TO_CHAR(TO_DATE(7, 'MM'), 'MONTH') AS monthname FROM DUAL;
outputs
monthname
---------
JULY
If you really want the month names in lower case or capitalised, you can also use:
TO_CHAR(TO_DATE(7, 'MM'), 'month')
TO_CHAR(TO_DATE(7, 'MM'), 'Month')
if you want to specify for your language, you can use like this;
SELECT to_char(TO_DATE(6, 'MM'),'MONTH','NLS_DATE_LANGUAGE=Turkish') from dual;
it returns a Turkish month name : Haziran
If you want to show a value after the formatted month like Year. Month tacks on a whole bunch of spaces which makes the value look weird. To fix this you have to trim the Month string prior to appending a follow up string value.
select trim(TO_CHAR(TO_DATE(7, 'MM'), 'MONTH')) || ' 2020' month_yr from dual;