I have this DATE column with Date (20150601) + Hour (02) and so on till the next year. I need to split this one column in two corresponding columns (Date and Hour) in R. As I understand I need stringr package.
What I have
DATE
2015060102
2015060104
2015060106
2015060109
2015060110
If you are just after a column, each of them a string, you can use substr for this task:
input_vals <- "2015060102"
date_col <- substr(input_vals, 1, 8)
hour_col <- substr(input_vals, 9, 10)
Related
I have a column with interval year to month datatype. With Extract function I get only month value from interval. But how to get full month (year * 12 + month)?
I can use Extract for year, multiply it by 12 and add it to the extracted month. But how to do it simply, using 1 function?
I am trying to add a day with a specific date using add_months in oracle database.
I wrote this line:
SELECT ADD_MONTHS('01-JAN-2018', MONTHS_BETWEEN('02-JAN-2018', '01-JAN-2018')) FROM DUAL;
this returns:
01-JAN-18
Why doesn't it return 02-JAN-18?? Can I add one day to the date using this function?
Why doesn't it return 02-JAN-18??
According to MONTHS_BETWEEN documentation,
The MONTHS_BETWEEN function calculates the number of months between
two dates. When the two dates have the same day component or are both
the last day of the month, then the return value is a whole number.
Otherwise, the return value includes a fraction that considers the
difference in the days based on a 31-day month
So,
select MONTHS_BETWEEN('02-JAN-2018', '01-JAN-2018') FROM DUAL ;
yields
.0322580645161290322580645161290322580645
ADD_MONTHS returns the date date plus integer months.
So, .0322.. is considered as integer 0 and your query is equivalent to
SELECT ADD_MONTHS('01-JAN-2018', 0) FROM DUAL;
In order to add 1 months, simply take the difference of two dates.
SELECT ADD_MONTHS(DATE '2018-01-01', DATE '2018-01-02' - DATE '2018-01-01') FROM DUAL;
Or better, add an INTERVAL of 1 month
SELECT DATE '2018-01-01' + INTERVAL '1' MONTH FROM DUAL;
To answer your question, add 1 day, simply use
SELECT DATE '2018-01-01' + 1 FROM DUAL;
Can someone explain me what does the below oracle query do and what is it's output?
select unique trunc(sysdate-370 + level, 'IW') AS datetime from dual
connect by level <= 360 order by datetime;
select sysdate-370 + level AS datetime
from dual
connect by level <= 360;
Will generate 360 rows starting with the current date/time minus 370 days plus one day per row. So rows between 369 and 10 days before the current date/time.
TRUNC( datetime, 'IW' ) will truncate the date to the start of the ISO week (midnight on Monday of that week - irrespective of the NLS settings for date language and/or territory that affect some other options for truncating dates). So you will end up with duplicate rows for each generated row that is in the same week.
The UNIQUE keyword will get rid of those duplicate rows.
The order by datetime will order the results in ascending date order - however, the rows are generated in ascending order so this clause is unnecessary.
So the output will be 52 or 53 rows (depending on what the current day of the week is) starting with Monday midnight of each week containing the date 369 days before the current day up until the week containing 10 days before the current date.
The output (when run on 13th September 2017) is 52 rows (I skipped a few):
05-SEP-2016
12-SEP-2016
19-SEP-2016
26-SEP-2016
03-OCT-2016
...
31-JUL-2017
07-AUG-2017
14-AUG-2017
21-AUG-2017
28-AUG-2017
According to documentation trunc(dateval, 'IW') truncates to:
Same day of the week as the first day of the calendar week as defined by the ISO 8601 standard, which is Monday
connect by level <= N is a trick for producing a set of N rows with level values from 1 to N.
Specify the day, month, and year as three INTEGER values with no separators between them, using these rules:
The day and month components must have two digits. When the day or month is less than 10, it must be preceded by a zero.
For any year, the year component can have four digits (for example, 1997). For years in the range 1950 to 2049, the year component can, alternatively, have two digits (50 represents 1950, and so on).
You cannot use any separators between the date components.
Examples: '240497' or '04241997'
I'm not sure what's the question but you can format a date with to_char
select to_char(sysdate,'ddmmyyyy') as d
from dual;
OUPUT
D
--------
07092016
If you want to insert into a table
insert into t1 (field1)
values (to_char(sysdate,'ddmmyyyy'));
I have the following values in 1 column in Oracle:
20123103
20113112
20103006
The data type is varchar.
I need to go back 1 year and find the dates in the same format in Oracle.
So, the output should be:
20123103 -> 20110104
20113112 -> 20110101
20103006 -> 20090107
Please advise.
You could convert it to a date, subtract a year, and then format it back to a string:
SELECT TO_CHAR(TO_DATE(date_column, 'YYYYDDMM')
- INTERVAL '1' YEAR
+ INTERVAL '1' DAY,
'YYYYDDMM')
FROM my_table