How to get the 1st day of the current week if week starts from Monday in BigQuery? - google-bigquery

SELECT TIMESTAMP_TRUNC(CURRENT_DATE(), WEEK(MONDAY)) AS FirstDayOfWeek
I am a beginner in Big query. This above query is giving me an error, please help me in correcting this.
Error message:
No matching signature for function TIMESTAMP_TRUNC for argument types: DATE, DATE_TIME_PART. Supported signature: TIMESTAMP_TRUNC(TIMESTAMP, DATE_TIME_PART, [STRING]) at [1:8]

Use DATE_TRUNC instead of TIMESTAMP_TRUNC
SELECT DATE_TRUNC(CURRENT_DATE(), WEEK(MONDAY)) AS FirstDayOfWeek

Related

Convert week of the year to date in oracle sql

How can I get a date using the week of the year in Oracle SQL?
I need to search for entries created after the beginning of the current week. Currently, I get the week of the year by doing select to_char(sysdate,'WW') from dual;, but then I can't do
select * from table where date > to_date(to_char(sysdate,'WW'), 'WW') because I get
ORA-01820: format code cannot appear in date input format
01820. 00000 - "format code cannot appear in date input format"
*Cause:
*Action:
You don't need to convert to a string and back, you can use truncate:
select * from table where date > trunc(sysdate,'WW')
Read more about the trunc() function and how the format model is applied.
Notice that WW gives you the same day as the first day of the year, so right now that would give 2020-09-02, which is a Wednesday - possibly not what you'd expect. It depends on your requirements of course, but you might want to work with IW which always starts from Monday, and would give 2020-09-07. If you have a different start day you can add or subtract a day, e.g. if your week starts on Sunday.
According to ORA-doc:
ORA-01820: format code cannot appear in date input format
Cause: A date specification contained an invalid format code. Only the following may > be specified when entering a date: year, month, day, hours, minutes, seconds, Julian day, > A.M./P.M. and B.C./A.D.
Action: Remove the invalid format code from the date specification.
You can't pass the weeknum to to_date() function. What you can do is e.g., the following
select * from table where date > (next_day(trunc(sysdate), 'SUNDAY') - 7)
Basically, next_day returns first date that meets specified weekday. Let's assume it's Monday 2020-09-07, next_day will return you the closest SUNDAY in the future, that is 2020-09-13, so you need to substract 7 to get date of the current week beginning. You can read more about it here

Date_Trunc not function working as expected

I am trying to use the Date_Trunc for MONTH function in a SQL statement but somehow it is not working for me. I am trying to pull entries which happen after April 1st, 2019. The raw date format from the Redshift database is this format which I am trying to group into month/year buckets: 2019-04-08T00:13:20.000Z
Input
SELECT
client_id as user_id,
session_utc as job_date --(format:2019-04-08T00:13:20.000Z)
FROM table1 as hits
WHERE job_date >= DATE_TRUNC('month', 2019-04-01)
group by 1,2;
Output
"ERROR: function date_trunc("unknown", integer) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts."
What am I doing wrong?
The DATE_TRUNC Function - Amazon Redshift takes timestamp as input and provides a timestamp as output:
DATE_TRUNC('datepart', timestamp)
For example:
SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)
2019-05-01 00:00:00
Therefore, your line should read:
WHERE job_date >= DATE_TRUNC('month', '2019-04-01'::timestamp)
If you wish to have the output as a date, append ::date:
SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)::date
2019-05-01
Also, note that the date converts into a timestamp as at midnight. This can cause a difference for some comparisons. For example:
'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp
will evaluate as True because it is comparing to midnight at the start of the day. This is different to comparing two dates (without timestamps).
The syntax for the function is DATE_TRUNC('datepart', timestamp), seems you need to use as DATE_TRUNC('month', session_utc)(this already truncates to the first date of April 2019 i.e. 2019-04-01 )
Assuming you are using Postgres, you need quotes around your date constant and can convert to the right types:
WHERE job_date >= DATE_TRUNC('month'::text, '2019-04-01'::date)
Here is a db<>fiddle.

Bigquery standardSQL: Current date minus a previous date with a result in number of days?

I would like to the current date minus a previous begin date with the result with the result being the number days there is a difference of the two?
I have attempted the following: date_sub(Begindt, INTERVAL current_date)
Also, will I have to cast things differently?
Below is for BigQuery Standard SQL
DATE_DIFF(CURRENT_DATE(), Begindt, DAY)
See more for DATE_DIFF()
Above assumes the Begindt field is of DATE type
If not, you should cast to DATE type via CAST or PARSE_DATE functions
Are you finding something like below
DATE_DIFF(Begindt, CURRENT_DATE, day)

Hive start of week on Sunday?

I'm trying to get the start of the week (Sundays, as a date) for a given date. This works except on Sundays since the day of the week origin begins on Monday:
SELECT DATE_SUB(FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_DATE(), 'yyyy-MM-dd')), CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_DATE(), 'yyyy-MM-dd'), 'u') AS INT))
The function above would return '2018-04-15' for a supplied date of '2018-04-22' whereas I want '2018-04-22'. Is the only recourse to write an case statement to offset for Sundays? I was hoping there was a nice parameter to FROM_UNIXTIME() that would have the weeks start on Sundays. I didn't find them in these docs:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You could get the result with a simple trick without case statements.
Calculate the modulus value of the weekday with 7 and you should get your result.
SELECT DATE_SUB(CURRENT_DATE(), CAST(DATE_FORMAT(CURRENT_DATE(),'u')%7 AS INT));

What is the correct way to use DATE_TRUNC in BigQuery?

I would like to get first day of the year based on the current date. The following query works.
#standardSQL
SELECT DATE_TRUNC(DATE '2017-01-20', YEAR) as First_day_of_year
But, I get syntax error using CURRENT_DATE() in place of the date value.
#standardSQL
SELECT DATE_TRUNC(DATE CURRENT_DATE(), YEAR) as YEAR
Error: Syntax error: Expected ")" but got identifier "CURRENT_DATE"
How can I correct this?
CURRENT_DATE() is already of DATE type so you should use as below
#standardSQL
SELECT DATE_TRUNC(CURRENT_DATE(), YEAR) as YEAR