Bigquery - Extract month as 2 digit - google-cloud-platform

How do I extract month as 2 digits from a date field?
My current query:
EXTRACT(MONTH FROM TransactionDate) as CalendarPeriod,
Example:
2021-07-13 returns 7
Required Output
2021-07-13 should return 07

Use below instead
cast(TransactionDate as string format('MM')) CalendarPeriod

Related

Pandas transform date string from format mm/d/yyyy and mm/dd/yyyy to dd.mm.yyyy

I have a Pandas data frame which contains a column with dates. The dates are represented in by a string in the format mm/dd/yyyy.
But I have a problem with the format of the day: Dates until the 9th day of a month are in the format mm/d/yyyy. For example the first december 2008 is displayed as 12/1/2008. The 10th day until the end of a month are displayed as mm/dd/yyyy. For example the 17th december 2008 is represented by 12/17/2008.
My target is to transform the all dates into the form mm.dd.yyyy. The could would represent the above expamles as: 12.01.2008 and 12.17.2008
My idea is to just write the day, month and year into seperate columns and then connect the strings in the format mm.dd.yyyy
So far I have tried to withdraw the year and the month just by their position in the string (see code and example below). But this does not work with the days as there are two cases: the day has either one or two digits.
My idea is to just use a regular expression It is basically the case backslash one or two digits and a backslash. But I do not know how I can express this as a regular expression.
Or is there totally different approach which is much simpler?
Thank you for the help in advance! I am sure that there is a way to do that with regular expressions. But I am also grateful for totally different approaches.
import pandas as pd
# example data frame with dates in the format mm/d/yyyy and mm/dd/yyyy
df = pd.DataFrame({'date' : ['12/1/2008','12/5/2008','12/10/2008','12/17/2008']})
# withdraw month
df['month'] = df['date'].str[:2]
# withdraw year
df['year'] = df['date'].str[-4:]
# withdraw day - this is my problem
df[day] = df['day'] = df['date'].str.extract(r'[\]\d*')
# generate string with dates in the format mm/dd/yyyy
df['date_new'] = df['month'] + '.' df['day'] + '.' + df['year']
From the code of df['day'] I get the following error: error: unterminated character set at position 0
I think you are looking for this:
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].dt.strftime('%m.%d.%Y')
Output:
date
0 12.01.2008
1 12.05.2008
2 12.10.2008
3 12.17.2008
Another thing to bring to your attention if you want to extract days, months, years or so, pandas has a special dt functionality for datetime types, hence, you need to convert your column first into that type.
You can access days and months like this:
df['date'] = pd.to_datetime(df['date'])
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
df['year'] = df['date'].dt.year
You will get something like:
date month day year
0 2008-12-01 12 1 2008
1 2008-12-05 12 5 2008
2 2008-12-10 12 10 2008
3 2008-12-17 12 17 2008

The last day of month with a given time flow in SAS

I am bothered with a simple question.
I need to get the last business day of the month and the date in my dataset only includes the business day.
For example:
ID Date
1 20180301
1 20180302
1 20180305
...
1 20180329
1 20180330
1 20180402
...
2 20180301
2 20180302
2 20180305
And I need the output like this:
ID Date Enddate
1 20180301 20180330 (The last business of March)
1 20180302 20180330
1 20180305 20180330
...
1 20180329 20180330
1 20180330 20180330
1 20180402 20180430 (The last business of March)
...
2 20180301 20180330 (Same for other IDs)
2 20180302 20180330
2 20180305 20180330
I tried to use this command:
enddt=intnx('month',date,0,'E');
However, it will output 20180331 instead of 20180330.
So I was wondering if there is a method to extract directly the last day of given month instead of the calendar month.
Thank you very much for your kind help.
You can do this in a data step:
1) sort on date from latest to earliest (reverse sort)
2) create a variable based on the yearmonth = int(date/100)
3) do a datastep by yearmonth and retain enddate
4) if first.yearmonth then enddate = date;
5) drop vars you don't want
6) sort back to original order

In sas application Set date parameter and put it in the contacted column to retrieve data in a certain period

The date in the table is not one set,
Days in the days column and months in the month column and years in the year column
I have concatenated the columns and then put these concatenation in where clause and put the parameter I have made but I got no result
I assume you are querying a date dimension table, and you want to extract the record that matches a certain date.
Solution:
I created a dates table to match with,
data dates;
input key day month year ;
datalines;
1 19 2 2018
2 20 2 2018
3 21 2 2018
4 22 2 2018
;;;
run;
Output:
In the where clause I parse the date '20feb2018'd using day, month & year functions: in SAS you have to quote the dates in [''d]
proc sql;
select * from dates
/*if you want to match todays' date: replace '20feb2018'd with today()*/
where day('20feb2018'd)=day and month('20feb2018'd)=month and year('20feb2018'd)=year;
quit;
Output:
if you compare date from day month and year, then use mdy function in where clause as shown below. it is not totally clear what you are looking for.
proc sql;
select * from dates
where mdy(month,day, year) between '19feb2018'd and '21feb2018'd ;

Match range of numbers like 10..15A..20

I have values in my db as follows
1
2
2A
.......
10
10A
.......
250A
......
300
and i need to find the records that are within range 1-10,11-20,....291-300
can't figure out how to do this as there are characters after number
I'm using postgres db so i can apply regex query on db
You can use:
SUBSTRING(mycolumn FROM '^[0-9]+')
in order to extract the number that is at the start of your column.
To perform the filtering you want, you can use:
SELECT *
FROM mytable
WHERE CAST(SUBSTRING(mycolumn FROM '^[0-9]+') AS INT) BETWEEN 1 AND 10
Demo here

django date filter %z (day of the year) with 3 digits

I'm working on a django template, and I want to format a date with the following format 'YYDDD' (sometimes known as julian date format) where:
YY is the last 2 digits of the year
DDD is the day of the year with 3 digit format
I tried to achive this with the date filter:
{{ value|date:"yz" }}
but "z" doesn't keep the output with 3 digits, it uses the minimum possible amount. If the day number is 3 (3rd January) it will print 3 istead 003.
For example, for the date 7th February of 2012 i would expect 12038 but instead I get 1238 (no zero padding).
How can I get this right?
You can write your django custom filter or use jcroft's snippet:
{{ value|date:"y" }}{{ value|date:"z"|leading_zeros:"3" }}