I need to filter an Oracle SQL query on persons who turned age 21 during the calendar year of 2019 - oracle

I need to filter an Oracle SQL query on persons who turned age 13 during the calendar year of 2019 - in other words, persons who had their 13th birthday sometime in 2019. Anyone know the code for that? Thanks in advance.

Assuming your table has a date_of_birth column this would work (2006 being 2019-13):
select *
from your_table
where to_number(extract(year from date_of_birth)) = 2006;
I notice your question title has a different year value from its body, so perhaps we need to hand over the troublesome maths to SQL:
select *
from your_table
where date '2019-01-01' = trunc(date_of_birth, 'yyyy') + interval '21' year
/
Truncating the DOB with a format mask converts all the dates to the first of January for that year. We then add an interval of 21 years. Comparing that value to the first of January 2019 will give you everybody who turned 21 last year.

I would use between clause with date_of_birth + 21 year to let oracle use index on date_of_birth, if any as following:
select *
from your_table
where date_of_birth + interval '21' year between date '2019-01-01' and date '2019-12-31';
Cheers!!

Related

Next week in Oracle

I'm using oracle dbms and I have in Employe table a column Birthdate. I want to write a query that shows the employees who has a birthday next week.
Is this correct ?
select name
from employe
where to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');
That is not the correct usage of next_day(): that function returns the date of the the next instance of a day. For example, to find the date of next Friday:
select next_day(sysdate, 'FRIDAY') from dual;
To find employees whose birthday is seven days from now, you need to just tweak your query a bit:
select name
from employe
where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');
The correct solution would be
SELECT name
FROM employe
WHERE to_char(birthdate
/* "move" the birthdate to the current year
to get a reliable week number */
+ CAST((EXTRACT(year FROM current_date)
- EXTRACT(year FROM birthdate)) || '-0'
AS INTERVAL YEAR TO MONTH),
'IW')
= to_char(current_date + 7, 'IW');
The IW format returns the ISO week containing the date, which is probably what you are looking for. If you start your week on Sunday, add one to both dates.

Oracle sql how to get the date of a week

I have the following query that gets the week of a date:
SELECT pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww') semana,
SUM (rta.kms_acumulados) kms,
COUNT
(DISTINCT (CASE
WHEN v.secuencia BETWEEN rta.sec_origen AND rta.sec_destino
THEN v.cod_inc
ELSE '0'
END
)
)
- 1 numincidencias
FROM (SELECT ms.tren, ms.fecha_origen_tren, ms.secuencia, ri.cod_inc
FROM r_incidencias ri, mer_sitra ms
WHERE ri.cod_serv = ms.tren
AND ri.fecha_origen_tren = ms.fecha_origen_tren
AND ri.cod_tipoin IN (SELECT cod_tipo_iincidencia
FROM v_tipos_incidencias
WHERE grupo = '45')
AND ri.punto_desde = ms.cod_estacion) v,
r_trenes_asignar rta,
r_maquinas rm,
planificador.pl_dh_material pdm
WHERE rta.fecha BETWEEN TO_DATE ('21/09/2018', 'dd/mm/yyyy') AND TO_DATE ('21/09/2018',
'dd/mm/yyyy'
)
AND rta.serie >= 4000
AND rta.matricula_ant IS NOT NULL
AND rm.matricula_maq = rta.matricula_ant
AND rm.cod_serie = pdm.id_material
AND rta.grafico BETWEEN pdm.desde AND pdm.hasta
AND v.tren(+) = rta.tren
AND v.fecha_origen_tren(+) = rta.fecha
GROUP BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
ORDER BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
For example week 1
I want to display
week 1 : 1 january - 7 january
How can I get this?
Oracle offers the TRUNC(datestamp, format) function to manipulate dates this way. You may use a variety of format strings to get the first day of a quarter, year, or even the top of the hour.
Given a particular datestamp value, Oracle returns midnight on the first day of the present week with this expression:
TRUNC(datestamp,'DY')
You can add days to a datestamp. Therefore this expression gives you midnight on the last day of the week
TRUNC(datestamp,'DY') + 6
A WHERE-clause selector for all rows in the present week might be this.
WHERE datestamp >= TRUNC(SYSDATE,'DY')
AND datestamp < TRUNC(SYSDATE,'DY') + 7
Notice that the end of the range is just before (<) midnight on the first day of the next week. You need that because you may have datestamps after midnight on the last day of the week. (Beware using BETWEEN for datestamp ranges.)
And,
SELECT TO_CHAR(TRUNC(SYSDATE,'DY'),'YYYY-MM-DD'),
TO_CHAR(TRUNC(SYSDATE,'DY')+6,'YYYY-MM-DD')
FROM DUAL;
displays the first and last dates of the present week in ISO-like format.
Date arithmetic is cool. It's worth your trouble to study the date-arithmetic functions in your DBMS at least once a year.

extract week number from date postgres

I would like to extract the week number as:
2015-52
from a date formatted as:
2015-12-27
How can I perform this in postgres?
my weeks are calculated from monday to sunday.
To get the year and the week in a single character value, use to_char()
select to_char(current_date, 'IYYY-IW');
IW returns the year and the week number as defined in the ISO standard and IYYY returns the corresponding year (which might be the previous year).
If you need the year and the week number as numbers, use extract
select extract('isoyear' from current_date) as year,
extract('week' from current_date) as week;
I have done like this
extract(week from cast(current_date as date))
extract(year from cast(current_date as date))
As same with #a_horse_with_no_name, but be careful there is a little difference between extract('isoyear' from current_date) and extract('year' from current_date) if current_date is within week0 in new year(the last week(53) of last year), especially when you extract week as well.
For example:
The follow output is 2020 not 2021
select EXTRACT('isoyear' from to_timestamp('2021-01-02 17:37:27', 'YYYY-MM-DD hh24:mn:ss')) as year
The follow output is 2021
select EXTRACT('year' from to_timestamp('2021-01-02 17:37:27', 'YYYY-MM-DD hh24:mn:ss')) as year

Oracle SQL group by week /month

How can we group reocrds weekly/monthly in oracle between from date and to date so that one record can be returned for one week/month.
e.g If we have 5 records for Ist weeek and 3 records for second week between a from date and to date then it should return total 2 records(one for Ist week and one for second week).
Thanks in advance.
You can group the results using GROUP BY: http://www.techonthenet.com/oracle/group_by.php
To select only those between from date and to date use WHERE
EDIT
For selecting the begining of the week or month, you can use TRUNC(the_date_field, ): http://www.techonthenet.com/oracle/functions/trunc_date.php
For example this groups by week:
SELECT TRUNC(datecol_name, 'WW') FROM table_name GROUP BY TRUNC(datecol_name, 'WW');

Calculate the week ending date in oracle using Saturday as the week end date

Given a field in Oracle that contains dates, how would you calculate what the week ending date is using Sun thru Sat as your week. For example, if the date is 1/26/2015 (which is a Monday), the query should return 1/31/2015 (which is a Saturday. If the date is 1/31/2015, then the query should return 1/31/2015.
Given any particular date / time value, this expression will return midnight of the preceding Sunday.
TRUNC(whatever_time,'DAY')
So, you can do stuff like this:
SELECT TRUNC(whatever_time,'DAY') week_starting,
TRUNC(whatever_time,'DAY') + 6 week_ending,
SUM(sales)
FROM table
GROUP BY TRUNC(whatever_time,'DAY')
and you'll get what you need.
Notice that TRUNC(whatever_time,'DAY') honors the Oracle session initialization parameter called “NLS_TERRITORY”. For example, in Europe Monday is considered the first business day of the week. Try this.
ALTER SESSION SET NLS_TERRITORY=GERMANY;
SELECT TRUNC( DATE '2013-12-31', 'DAY'),
TRUNC( DATE '2014-01-03', 'DAY')
FROM DUAL;
A complete writeup of this is here: http://www.plumislandmedia.net/sql-time-processing/using-sql-report-time-intervals-oracle/

Resources