Compare date filed with month and year in Postgres - sql

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

Related

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

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''

How to query checking if a month is between two dates?

I'm looking for query in postgresql to check whether a month is between two dates or not.
I know how to check if a date is between two date or not. Postgres also have a function to do that.
Let's say I have a a_table with rows:
ID | start_date (timestamp) | end_date (timestamp)
1 | 2019-07-20 00:00 | 2020-03-20 00:00
2 | 2019-08-20 00:00 | 2020-08-30 00:00
I have a to return the row that include a month between the start_date and end_date.
Let's say i have a month 2019-08.
So when i count
Select count(*) from a_table
Where [some where clause]
it returns 2 rows, ID 1 and ID 2
AND when i have a month 2020-01 it only return ID 1
You can use date range for this.
It's not clear to me what should happen if the start/end date in the table only covers part of a month.
If you only want to consider the full month, use the "contains" operator
select count(*)
from the_table
where daterange(start_date::date, end_date::date, '[]') #> daterange('2019-08-01'::date, '2019-09-01'::date, '[)');
The <# is the "is contained" operator which tests if the left range (the values from the table) is contained in the right hand range (the month you want to test). The comparison is done with an "open interval", which means '2019-09-01' is excluded from it. The above would not consider rows that do not contain the full August.
If you want to include partial matches as well, use the "overlaps" operator && instead:
select count(*)
from the_table
where daterange(start_date::date, end_date::date, '[]') && daterange('2019-08-01'::date, '2019-09-01'::date, '[)');
You can use to_date() in your where clause. So that your where clause would be like this.
WHERE '2019-08' BETWEEN to_date(start_date, 'YYYY-MM') and to_date(end_date ,'YYYY-MM')
I would recommend writing this as:
WHERE end_date >= TO_DATE('2019-08', 'YYYY-MM') AND
start_date < TO_DATE('2019-08', 'YYYY-MM') + INTERVAL '1 MONTH'
That is, the period includes at least one day of the month, because it starts before the end of the month and ends after the start of the month.
In addition, this has no functions on the columns in the table. So, if an index is available on either column, then it can be used. If you define the start/end as a range, then that provides other opportunities for using indexes.

How to get year, month and day from seconds in PostgreSql?

I'm trying to create three columns based on date in seconds format.
My user.updated_at = 1521533490
I would like to get year, month and day separately and put these formatted values to columns for example:
year -> 2018, month -> 11, day -> 23
Does someone know how can I do that in pgSQL?
I would like to get year, month and day separately and put these formated values to columns
Don't do that.
Use a single column of type date or timestamp, depending on your application. Not every combination of your three columns will be a valid date. But every value in a single column of type date will be a valid date.
If you need the parts of a date separately, use PostgreSQL's date/time functions.
Try this approche to get differents arguments, then you can do whatever you want:
SELECT to_timestamp(1521533490); //2018-03-20T08:11:30.000Z
SELECT to_char(to_timestamp(1521533490), 'HH'); // 08 Hour
SELECT to_char(to_timestamp(1521533490), 'MI'); // 11 Minutes
SELECT to_char(to_timestamp(1521533490), 'SS'); // 30 Seconds
SELECT to_char(to_timestamp(1521533490), 'DD'); // 20 Day
SELECT to_char(to_timestamp(1521533490), 'Mon'); // MAR Month
SELECT to_char(to_timestamp(1521533490), 'YYYY'); // 2018 Year
Use the EXTRACT function.
SELECT to_timestamp(updated_at) "Date",
EXTRACT(YEAR FROM (to_timestamp(updated_at))) "Year",
EXTRACT(MONTH FROM (to_timestamp(updated_at))) "Month",
EXTRACT(DAY FROM (to_timestamp(updated_at))) "Day"
FROM users
Output
Date Year Month Day
2018-03-20T08:11:30Z 2018 3 20
SQL Fiddle: http://sqlfiddle.com/#!15/afe0e/15/0
More information on the EXTRACT function.

Choose active employes per month with dates formatted dd/mm/yyyy

I'm having a hard time explaining this through writing, so please be patient.
I'm making this project in which I have to choose a month and a year to know all the active employees during that month of the year.. but in my database I'm storing the dates when they started and when they finished in dd/mm/yyyy format.
So if I have an employee who worked for 4 months eg. from 01/01/2013 to 01/05/2013 I'll have him in four months. I'd need to make him appear 4 tables(one for every active month) with the other employees that are active during those months. In this case those will be: January, February, March and April of 2013.
The problem is I have no idea how to make a query here or php processing to achieve this.
All I can think is something like (I'd run this query for every month, passing the year and month as argument)
pg_query= "SELECT employee_name FROM employees
WHERE month_and_year between start_date AND finish_date"
But that can't be done, mainly because month_and_year must be a column not a variable.
Ideas anyone?
UPDATE
Yes, I'm very sorry that I forgot to say I was using DATE as data type.
The easiest solution I found was to use EXTRACT
select * from employees where extract (year FROM start_date)>='2013'
AND extract (month FROM start_date)='06' AND extract (month FROM finish_date)<='07'
This gives me all records from june of 2013 you sure can substite the literal variables for any variable of your preference
There is no need to create a range to make an overlap:
select to_char(d, 'YYYY-MM') as "Month", e.name
from
(
select generate_series(
'2013-01-01'::date, '2013-05-01', '1 month'
)::date
) s(d)
inner join
employee e on
date_trunc('month', e.start_date)::date <= s.d
and coalesce(e.finish_date, 'infinity') > s.d
order by 1, 2
SQL Fiddle
If you want the months with no active employees to show then change the inner for a left join
Erwin, about your comment:
the second expression would have to be coalesce(e.finish_date, 'infinity') >= s.d
Notice the requirement:
So if I have an employee who worked for 4 months eg. from 01/01/2013 to 01/05/2013 I'll have him in four months
From that I understand that the last active day is indeed the previous day from finish.
If I use your "fix" I will include employee f in month 05 from my example. He finished in 2013-05-01:
('f', '2013-04-17', '2013-05-01'),
SQL Fiddle with your fix
Assuming that you really are not storing dates as character strings, but are only outputting them that way, then you can do:
SELECT employee_name
FROM employees
WHERE start_date <= <last date of month> and
(finish_date >= <first date of month> or finish_date is null)
If you are storing them in this format, then you can do some fiddling with years and months.
This version turns the "dates" into strings of the form "YYYYMM". Just express the month you want like this and you can do the comparison:
select employee_name
from employees e
where right(start_date, 4)||substr(start_date, 4, 2) <= 'YYYYMM' and
(right(finish_date, 4)||substr(finish_date, 4, 2) >= 'YYYYMM' or finish_date is null)
NOTE: the expression 'YYYYMM' is meant to be the month/year you are looking for.
First, you can generate multiple date intervals easily with generate_series(). To get lower and upper bound add an interval of 1 month to the start:
SELECT g::date AS d_lower
, (g + interval '1 month')::date AS d_upper
FROM generate_series('2013-01-01'::date, '2013-04-01', '1 month') g;
Produces:
d_lower | d_upper
------------+------------
2013-01-01 | 2013-02-01
2013-02-01 | 2013-03-01
2013-03-01 | 2013-04-01
2013-04-01 | 2013-05-01
The upper border of the time range is the first of the next month. This is on purpose, since we are going to use the standard SQL OVERLAPS operator further down. Quoting the manual at said location:
Each time period is considered to represent the half-open interval
start <= time < end [...]
Next, you use a LEFT [OUTER] JOIN to connect employees to these date ranges:
SELECT to_char(m.d_lower, 'YYYY-MM') AS month_and_year, e.*
FROM (
SELECT g::date AS d_lower
, (g + interval '1 month')::date AS d_upper
FROM generate_series('2013-01-01'::date, '2013-04-01', '1 month') g
) m
LEFT JOIN employees e ON (m.d_lower, m.d_upper)
OVERLAPS (e.start_date, COALESCE(e.finish_date, 'infinity'))
ORDER BY 1;
The LEFT JOIN includes date ranges even if no matching employees are found.
Use COALESCE(e.finish_date, 'infinity')) for employees without a finish_date. They are considered to be still employed. Or maybe use current_date in place of infinity.
Use to_char() to get a nicely formatted month_and_year value.
You can easily select any columns you need from employees. In my example I take all columns with e.*.
The 1 in ORDER BY 1 is a positional parameter to simplify the code. Orders by the first column month_and_year.
To make this fast, create an multi-column index on these expressions. Like
CREATE INDEX employees_start_finish_idx
ON employees (start_date, COALESCE(finish_date, 'infinity') DESC);
Note the descending order on the second index-column.
If you should have committed the folly of storing temporal data as string types (text or varchar) with the pattern 'DD/MM/YYYY' instead of date or timestamp or timestamptz, convert the string to date with to_date(). Example:
SELECT to_date('01/03/2013'::text, 'DD/MM/YYYY')
Change the last line of the query to:
...
OVERLAPS (to_date(e.start_date, 'DD/MM/YYYY')
,COALESCE(to_date(e.finish_date, 'DD/MM/YYYY'), 'infinity'))
You can even have a functional index like that. But really, you should use a date or timestamp column.

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.