Oracle concatenate two int columns, make them date and select a date from them - sql

I have column REF_YEAR and column REF_MONTH. Both are int columns.
I want to select a range of dates between these two ranges.
for example
select *
from table
where date between '05/2020' and '12/2021'
Can i do something like that but with similar results for int columns?

If I understood you correctly, you'd
select *
from your_table
where to_date(ref_year || lpad(ref_month, 2, '0'), 'yyyymm')
between date '2020-05-01' and date '2021-12-01'
i.e. you'd have to "convert" (to_date) ref_year concatenated with ref_month (left-padded with zero up to 2 characters in length because May 2021 isn't 20215 but 202105), using appropriate format mask (yyyymm) and compare it to valid date values, e.g. date literals I used.

You should be using a single proper date column here, rather than separate columns for the month and year. That being said, integer month and year columns is still workable. Using your example:
SELECT *
FROM yourTable
WHERE REF_YEAR = 2020 AND REF_MONTH >= 5 OR REF_YEAR = 2021;
Note that the above logic would include the entire 2021 calendar year, including the entire month of December. This is what I imagine you want; if you instead want to exclude December, we would have to change the logic slightly. That being said, it would be much better to maintain a single date column REF_DATE:
SELECT *
FROM yourTable
WHERE REF_DATE >= date '2020-05-01' AND REF_DATE < date '2022-01-01''

Related

SQL filter by two date columns in the same month

In my dataset I have column A and column B, they have different dates, sometimes one column has a date and the other don't, sometimes both have values but different months. I want to filter by the date when they are in the January.
I want both columns to have date of January '2023-01-01'
I started with
SELECT *
FROM table
WHERE DATE_TRUNC(SAFE_CAST(column_a AS DATE), MONTH) = '2023-01-01'
OR DATE_TRUNC(column_b, MONTH) = '2023-01-01'
The problem is that in some cases the column_a has a date prior January due to the or. And both dates need to be in the some month.
How can I solve that?

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);

split the date then divide it into three columns

I have a column for a date in this format 2/21/2020, and 11/2/2020
what I mean if it the month 2 it will be one digit if it is 11 two digits
the result that I need from the above examples should be like 20200221 and 20201102
so I decided to split the date and divide them into three columns be like year:2020 month:02 day:21
then I will combine them
year || month || day
now I know how to split the year
select
regexp_substr(date1, '[^/]+$', 1, 1) as year
from (select '2/11/2020' as date1 from dual)
but if I need the month or the day what I should change in the code
In addition, If the month contains one digit, the 0 should be added beside the month number like 02. do I use case when condition?
Is there any easier way to solve this problem
Try this hope you like it.
declare #TargetDate datetime='2/21/2020'
SELECT REPLACE(CONVERT(varchar,(SELECT #TargetDate),23), '-', '') as Date;
If your column is of date datatype then
To_char(your_date_column, 'yyyymmdd')
If your column is of varchar datatype then
To_char(to_date(your_date_column_in_string,'mm/dd/yyyy'), 'yyyymmdd')
Dont worry about one or two digit in day or month as oracle is smart enough to recognize it.
See this db<>fiddle demo

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 SQL get yyyymm minus 2 months

I am using Presto. I have an integer column (let's call the column 'mnth_nbr') showing year and month as: yyyymm. For instance, 201901. I want to have records showing all dates AFTER 201901 as well as 2 months before the given date. In this example, it would return 201811, 201812, 201901, 201902, 201903, etc. Keep in mind that my data type here is integer.
This is what I have so far (I do a self join):
select ...
from table 1 as first_table
left join table 1 as second_table
on first_table.mnth_nbr = second_table.mnth_nbr
where first_table.mnth_nbr <= second_table.mnth_nbr
I know this gives me all dates AFTER 201901, including 201901. But, I don't know how to add the 2 previous months (201811 and 201812)as explained above.
As far as the documentation, Presto DB date_parse function expects a MySQL-like date format specifier.
So the proper condition for your use case should be :
SELECT ...
FROM mytable t
WHERE
date_parse(cast(t.mnth_nbr as varchar), '%Y%m') >= date '2019-01-01' - interval '2' month
Edit
As commented by Piotr, a more optimized expression (index-friendly) would be :
WHERE
mnth_nbr >= date_format(date '2019-01-01' - interval '2', '%Y%m')
Something like this would help. first parse your int to date
date_parse(cast(first_table.mnth_nbr as varchar), 'yyyymm') > date '2019-01-01' - interval '2' month
please keep in mind that you may encounter with indexing issues with this approach.