How to align the date - tidyverse

There is current_dataframe ,how to align the start_day end_day and keep the date_gap not change (Is avaiable package or function for this)? Thanks!
library(tidyverse)
current_dataframe <- data.frame(start_day=c('2022/1/19','2022/2/19','2022/4/19'),
end_day=c('2022/1/25','2022/2/20','2022/4/24'))

Use the lubridate package to work with dates. Use ymd to convert the character string into a date format. Then floor_date can give you the first day of the month (it can do other units too).
library(tidyverse)
library(lubridate)
current_dataframe <- data.frame(start_day=c('2022/1/19','2022/2/19','2022/4/19'),
end_day=c('2022/1/25','2022/2/20','2022/4/24'))
current_dataframe %>%
mutate(across(c(start_day, end_day), ymd),
days_gap = end_day - start_day,
start_day = floor_date(start_day, "month"),
end_day = start_day + days_gap)

Related

How can I convert a string quarter year to a timestamp in Databricks SQL

In Databricks SQL, how can I convert a date in string format "2021Q2" to a timestamp with the last day of that quarter?
select
to_timestamp(
last_day(
to_date(
(left('2021Q4',4)||'-'||int(right('2021Q4',1)*3))||'-'||'1')))
from
my_table
It is a pity that Q is not working in formatting from string to date object (it is working only in reverse) - parsing Q with to_date(date, "YYYY'QQ") sadly will not work.
According to https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html:
Symbols of ‘E’, ‘F’, ‘q’ and ‘Q’ can only be used for datetime formatting, e.g. date_format. They are not allowed used for datetime parsing, e.g. to_timestamp.
Because of that we have to separate quarter and multiple by 4. Than convert it to date object (parse_ and take last_date of the month:
SELECT
last_day(
to_date(
concat(left("2021Q4", 4), int(right("2021Q4", 1))*3),
"yyyyMM")
) as last_day_of_quarter
Simple way :
select to_timestamp(last_day(concat('2021','-',0,4*3,'-01'))) as last_date_queter
Logic :
calculate the last month of quarter by using multiple with 3. example 4th quarter's last month calculated 12 (4*3)
concat (year,'-',-01) so that we can get the first day of respective month 2021-12-01
last_day we can use last date of the given date month.
finally , we can convert the date into timestamp to_timestamp

How to display day first with pd.to_datetime()?

I have a data frame with date columns in the format: day / month / year
They are in string/object format.
I want to convert them to datetime.
Sample date, 5th of January 2016: '05/01/2016'
However pd.to_datetime is confusing the day and month.
Here is what I've tried:
pd.to_datetime('05/01/2016')
Timestamp('2016-05-01 00:00:00')
This has given me Year - Month - Day
I want Day - Month - Year as in: 05-01-2016
What I have tried:
pd.to_datetime('05/01/2016',dayfirst=True)
Timestamp('2016-01-05 00:00:00')
This is correct, but it's not the format I want, which is '05-01-2016'
So I tried this:
pd.to_datetime('05/01/2016',dayfirst=True,format='%d/%m/%Y')
Timestamp('2016-01-05 00:00:00')
There's no difference.
How can I do it? How can I force it to display the datetime as '05-01-2016'
The only way I know is to change the display options:
pd.set_option("display.date_dayfirst", True)
https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html#available-options
but it's not working... Else you convert the datetime type to str:
ts = pd.to_datetime('05/01/2016', format='%d/%m/%Y')
print(ts)
# Timestamp('2016-01-05 00:00:00')
ts = ts.strftime('%d-%m-%Y')
print(ts)
# '05-01-2016'
Or just replace '/' by '-':
print('05/01/2016'.replace('/', '-'))
# '05-01-2016'
You can't change the timestamp format (to my knowledge), but you can convert it to string in the wanted format like so :
>>> import pandas as pd
>>> pd.to_datetime('05/01/2016', dayfirst=True, format='%d/%m/%Y').strftime('%d-%m-%Y')
'05-01-2016'

Transform Julien date and timestamp in correct durations

I have an Oracle DB with this data (extract - there are more columns - totally)
The first is the name, the second the department.
Then there is at CJDate the JDE Julian date - Format.
And cStart and cEnds inludes the timestamp.
I would like to get the Date in the Format "DD.MM.YYYY" and the duration between cStart and cEnd in "D.HH:MM:SS".
But there are some exotics. If there are values in cEnd between 0 and 62000, the cJDate should be -1.
Is it possible to get the correct duration, if cEnd has the value 240000?Or sould it be transformed in 23:59:59 or in cJDate -1 and 00:00:00?
I use already this, to get the correct date:
DATE '1900-01-01' + FLOOR(ZZ."cJDate" / 1000) * INTERVAL'1' YEAR+
(MOD(ZZ."cJDate", 1000) -1) * INTERVAL'1' DAY AS "Date",
Does someone have any idea?
Best regards
Until now I've been using this:
select to_date(to_char(cjDate), 'YYYDDD')
from dual;
Is not julian date it's more like JDE Julian date
Example output:
118316 → 12/11/18

Concatenating and comparing values of fields as date

Can you help me out with this problem.
I have the following fields below with sample values:
STRM = 1171
TERM_BEGIN_DT = 01-SEPT-18
TERM_END_DT = 31-JUL-19
ACAD_YEAR = 2018
*Additional Info:
2018/19 Academic Year
1st August 2018 – 31st July 2019*
What I want to do is, I want to get the STRM within the current ACADEMIC YEAR
The SQL i want is:
SELECT STRM FROM PS_TERM_TBL
WHERE TERM_BEGIN_DT BETWEEN '01-AUG-18' AND '31-JUL-19';
The problem is, given only the values above, I have to hard code '01-AUG-' and '31-JUL-' and concatenate each with the '18' and '18' + 1 of ACAD_YEAR respectively.
Main question is, how do i do this?
How do i get the 18 of 2018 in ACAD_YEAR and then add 1 to it to get 19?
I think i will get a invalid type error with this one, so what do i have to convert to to_date in order for the comparison to be legit?
You can use ADD_MONTHS to translate the epoch date of the start of the current academic year back to the start of the calendar year, TRUNCate the value to the beginning of the year, and then use ADD_MONTHS again to reverse the initial translation:
SELECT STRM
FROM PS_TERM_TBL
WHERE TERM_BEGIN_DT >= ADD_MONTHS( TRUNC( ADD_MONTHS( SYSDATE, -7 ), 'YYYY' ), 7 )
AND TERM_BEGIN_DT < ADD_MONTHS( TRUNC( ADD_MONTHS( SYSDATE, -7 ), 'YYYY' ), 19 )
As an aside, '01-AUG-18' is not a date - it is a string literal that Oracle is implicitly converting to a date using the NLS_DATE_FORMAT session parameter (which is something that can change depending on the user's territory and users can also change their own session's settings so should not be relied upon to be give a consistent format model). If you want to specify dates then you should use either:
a date literal DATE '2018-08-01'; or
an explicit conversion from a string literal TO_DATE( '01-AUG-18', 'DD-MON-RR', 'NLS_DATE_LANGUAGE = American' )

Is there a way to convert ISO 8601 to date format in BigQuery?

I have input from Amazon Alexa in the format of ISO 8601 and was wandering if I needed to do a whole bunch of string substrings & transforms to make it into a BigQuery Timestamp format, or is there some function that does it?
I also understand that it is hard to turn 2015-W49 into a date, but thought I would ask.
references:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/built-in-intent-ref/slot-type-reference#date
https://en.wikipedia.org/wiki/ISO_8601#Dates
https://code.google.com/p/google-bigquery/issues/detail?id=208
This can be done via the function PARSE_DATE as detailed here: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_date
In BigQuery Standard SQL:
SELECT DATE_ADD(PARSE_DATE('%Y', SUBSTR('2015-W49',0,4)), INTERVAL CAST(SUBSTR('2015-W49',7,2) as INT64)-1 WEEK) as parsed;
I would expect below result of such conversion, which is first day of the respective week, which should be respectively :
Week Date in ISO 8601 First Day of the week
2015-W01 2014-12-28
2015-W02 2015-01-04
2015-W49 2015-11-29
You can verify above at http://www.enpicbcmed.eu/calendar/2015-W01 for example
I think below returns correct result
#standardSQL
WITH data AS (
SELECT '2015-W01' AS dt UNION ALL
SELECT '2015-W02' AS dt UNION ALL
SELECT '2015-W49' AS dt
)
SELECT
dt,
DATE_ADD(PARSE_DATE("%Y-W%W", dt),
INTERVAL 7 * CAST(SUBSTR(dt,-2,2) AS INT64) - 6 -
EXTRACT (DAYOFWEEK FROM PARSE_DATE("%Y-W%W", dt)) DAY
) as d
FROM data
ORDER BY dt
This worked for me:
DATE_ADD(
DATE_TRUNC(PARSE_DATE('%Y-%m-%d', CAST(date_close AS STRING)), ISOYEAR),
INTERVAL EXTRACT(WEEK FROM date_close) - 1 WEEK
)