sql add year statement - sql

I have sysdate in one column and I want to add two years in another. I would not like to use sysdate + 365
Is there any other way?

select add_months(sysdate, 12) from dual

SELECT your_date_column + interval '2' year
FROM dual

Related

How to select whether or not a specific date is before X amount of years? - Presto SQL

I'm trying to select whether or not the date value in one of my columns takes place before a certain amount of years.
The code I'm trying out:
SELECT CASE
WHEN CAST(my_date AS DATE) + INTERVAL '10' YEAR < CURRENT_DATE THEN 'OVER 10 YEARS AGO'
ELSE NULL AS date_check
My expected output should be NULL for the dates I'm using (e.g. 2019-09-30) as they fall within the last 10 years, but for some reason, I'm getting OVER 10 YEARS AGO for everything.
When I test with values actually over 10 years ago (e.g. 2010-07-17), I'm also getting OVER 10 YEARS AGO as expected, however.
How is it possible to achieve what I'm trying to do? What appears to be wrong with my code?
WITH cte (my_date) AS (
SELECT current_date - INTERVAL '11' YEAR UNION
SELECT '2010-07-17' UNION
SELECT '2019-09-30'
)
SELECT CASE
WHEN CAST(my_date AS DATE) + INTERVAL '10' YEAR < CURRENT_DATE
THEN 'OVER 10 YEARS AGO'
ELSE NULL
END AS date_check
FROM cte
;
Having said that, it's best to perform your calculation against the literal, not your column:
WITH cte (my_date) AS (
SELECT current_date - INTERVAL '11' YEAR UNION
SELECT '2010-07-17' UNION
SELECT '2019-09-30'
)
SELECT CASE
WHEN CAST(my_date AS DATE) < CURRENT_DATE - INTERVAL '10' YEAR
THEN 'OVER 10 YEARS AGO'
ELSE NULL
END AS date_check
FROM cte
;
and, if possible, use the correct type without CAST.
Full test case

SQL Oracle How to convert String Week into date

I have dates stored like String in database.
The format is 'yyyy-ww' (example: '2015-43').
I need to get the first day of the week.
I tried to convert this string into date but there is no 'ww' option for the function "to_date".
Do you have an idea to perform this convertion?
EDIT
Test results based on the answers -
Thanks for your anwsers, but I have many problems to apply your solutions to my context:
select
TRUNC ( 2015 + ((43 - 1) * 7), 'IW' )
from dual
==> Error : ORA-01722: invalid number
select
TRUNC(to_date('2015','YYYY')+ to_number('01') *7, 'IW')
from dual
==> 2015-02-02 00:00:00
I waited for a date in january
select
trunc(to_date(regexp_substr('2015-01', '\d+',1,2), 'YYYY') + regexp_substr('2015-01', '\d+') * 7, 'IW') dt2
from dual
==> 0039-09-14 00:00:00
select
regexp_substr('2015-01', '\d+',1,2) as res1,
regexp_substr('2015-01', '\d+') * 7 as res2
from dual
==> res1 = 01
==> res2 = 14105
try to use by truncate
with t as (
select '16-2010' dt from dual
)
--
--
select dt,
trunc(to_date(regexp_substr(dt, '\d+',1,2), 'YYYY') + regexp_substr(dt, '\d+') * 7, 'IW') dt2
from t
I have dates stored like String in database.
You should never do that. It is a bad design. you should store date as DATE and not as a string. For all kinds of requirements for date manipulations Oracle provides the required DATE functions and format models. As and when needed, you could extract/display the way you want.
I need to get the first day of the week.
TRUNC (dt, 'IW') returns the Monday on or before the given date.
Anyway, in your case, you have the literal as YYYY-WW format. You could first extract the year and week number and combine them together to get the date using TRUNC.
TRUNC ( year + ((week_number - 1) * 7)
, 'IW
)
So, the above should give you the Monday of the week number passed for that year.
SQL> WITH DATA AS
2 ( SELECT '2015-43' str FROM dual
3 )
4 SELECT TRUNC(to_date(SUBSTR(str, 1, 4),'YYYY')+ to_number(SUBSTR(str, instr(str, '-',1)+1))*7, 'IW')
5 FROM DATA
6 /
TRUNC(TO_
---------
23-NOV-15
SQL>
Similar to Lalit's, however, I think I've corrected the math (his seemed to be off a bit when I tested .. )
with w_data as (
select sysdate + level +200 d from dual connect by level <= 10
),
w_weeks as (
select d, to_char(d,'yyyy-iw') c
from w_data
)
SELECT d, c, trunc(d,'iw'),
TRUNC(
to_date(SUBSTR(c, 1, 4)||'0101','yyyymmdd')-8+to_char(to_date(SUBSTR(c, 1, 4)||'0101','yyyymmdd'),'d')
+to_number(SUBSTR(c, instr(c, '-',1)+1)-1)*7 ,'IW')
FROM w_weeks;
The extra columns help show the dates before, and after.
I would do the following:
WITH d1 AS (
SELECT '2015-43' AS mydate FROM dual
)
SELECT TRUNC(TRUNC(TO_DATE(REGEXP_SUBSTR(mydate, '^\d{4}'), 'YYYY'), 'YEAR') + (COALESCE(TO_NUMBER(REGEXP_SUBSTR(mydate, '\d+$')), 0)-1) * 7, 'IW')
FROM d1
The first thing the above query does is get the first four digits of the string 2015-43 and truncates that to the closest year (if you convert convert 2015 using TO_DATE() it returns a date within the current month; that is SELECT TO_DATE('2015', 'YYYY') FROM dual returns 01-FEB-2015; we need to truncate this value to the YEAR in order to get 01-JAN-2015). I then add the number of weeks minus one times seven and truncate the whole thing by IW. This returns a date of 01-OCT-2015 (see SQL Fiddle here).
According ISO the 4th of January is always in week 1, so your query should look like
Select
TRUNC(TO_DATE(REGEXP_SUBSTR(your_column, '^\d{4}')||'-01-04', 'YYYY-MM-DD')
+ 7*(REGEXP_SUBSTR(your_column, '\d$')-1), 'IW')
from your_table;
However, there is a problem. ISO year used for Week number can be different than actual year. For example, 1st Jan 2008 was in ISO week number 53 of 2007.
I think a proper working solution you get only when you generate ISO weeks from date value.
WITH w AS
(SELECT TO_CHAR(DATE '2010-01-04' + LEVEL * INTERVAL '7' DAY, 'IYYY-IW') AS week_number,
TRUNC(DATE '2010-01-04' + LEVEL * INTERVAL '7' DAY, 'IW') AS first_day
FROM dual
CONNECT BY DATE '2010-01-04' + LEVEL * INTERVAL '7' DAY < SYSDATE)
SELECT your_Column, first_day
FROM w your_table
JOIN w ON week_number = your_Column;
Your date range must bigger than 2010-01-04 and not bigger than current day.
This is what I used:
select
to_date(substr('2017/01',1,4)||'/'||to_char(to_number(substr('2017/01',6,2)*7)-5),'yyyy/ddd') from dual;

Add days Oracle SQL

SELECT ORDER_NUM, CUSTOMER_NUM, CUSTOMER_NAME, ADD_DAYS (ORDER_DATE, 20)
FROM CUSTOMER, ORDERS;
Oracle Express says ADD_DAYS invalid? Any ideas what Am I doing wrong?
If you want to add N days to your days. You can use the plus operator as follows -
SELECT ( SYSDATE + N ) FROM DUAL;
You can use the plus operator to add days to a date.
order_date + 20
In a more general way you can use "INTERVAL". Here some examples:
1) add a day
select sysdate + INTERVAL '1' DAY from dual;
2) add 20 days
select sysdate + INTERVAL '20' DAY from dual;
2) add some minutes
select sysdate + INTERVAL '15' MINUTE from dual;
Some disadvantage of "INTERVAL '1' DAY" is that bind variables cannot be used for the number of days added. Instead, numtodsinterval can be used, like in this small example:
select trunc(sysdate) + numtodsinterval(:x, 'day') tag
from dual
See also: NUMTODSINTERVAL in Oracle Database Online Documentation
It's Simple.You can use
select (sysdate+2) as new_date from dual;
This will add two days from current date.
One thing about
select (sysdate+3) from dual
is that the sysdate is interpreted as date.
But if you want to use a custom date, not local, you need to make sure it is interpreted as date, not string. Like so (adding 3 days):
select (to_date('01/01/2020')+3) from dual

Oracle SQL: Get the last eight months of data (with YEAR and MONTH columns)

I have a table that has columns YEAR and MONTH, which are varchars, which have a format like MONTH = '02', YEAR = '2011'.
What query can I use to get the last eight months of data, excluding the current month?
Try
where to_date(year || month, 'YYYYMM')
between add_months(trunc(sysdate, 'MM'), -8) and trunc(sysdate)
Try this:
SELECT *
FROM myTable
WHERE (year, month) IN
(
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -level), 'RRRR') AS year,
TO_CHAR(ADD_MONTHS(SYSDATE, -level), 'MM') AS month
FROM dual
CONNECT BY LEVEL < 8
)
Another version:
SELECT *
FROM myTable
WHERE year||month IN
(
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -level), 'RRRRMM') AS yearmonth
FROM dual
CONNECT BY LEVEL < 8
)
Best I can think of is this:
SELECT * FROM table WHERE (MONTH || YEAR) IN ('012011','122010', et cetera)
It is a brute force method, so it is not feasible if you want your query to generalize.

Date range in sql

I want to write a query where it displays data between 01-April-2010 to lastday-april-2011.
But I don't want to hard code the year. It should take from system date so it only displays records from this year april to next year april.
Thanks in advance for the help.
select add_months( trunc(sysdate,'YYYY'), -12 )
+ interval '4' month
+ level
- 1
from dual
connect by level <= ( (trunc(sysdate,'YYYY')
+ interval '4' month) -
(add_months( trunc( sysdate, 'YYYY' ), -12 )
+ interval '4' month) )
should work. You could probably simplify the expression that computes the number of rows but I'd have to think for a few minutes about leap years.
If the date column has no time component:
select ...
from mytable
where datecol between add_months(trunc(sysdate,'YYYY'),3)
and add_months(trunc(sysdate,'YYYY'),16)-1
If the date column has a time component:
select ...
from mytable
where datecol >= add_months(trunc(sysdate,'YYYY'),3)
and datecol < add_months(trunc(sysdate,'YYYY'),16)
Your query should be something like this
SELECT *
FROM youtable
WHERE datecolumn BETWEEN to_char(sysdate,'yyyy')+'0401' AND to_char(sysdate,'yyyy')+'0430'