Google BigQuery Date(String format) failed to convert/cast as Date - google-bigquery

I am trying to convert/cast the date in the big query into date format. My query is like this:
SELECT CAST(t.date AS date)
FROM `table` t;
But I got an Error code of Invalid date:'20151108'. it gave me different error date when I run the query.
Any thoughts?

try
SELECT PARSE_DATE('%Y%m%d', t.date) FROM table t

Related

Presto SQL + Athena: Cast date fail with error message: INVALID_FUNCTION_ARGUMENT

I have a table with datehourminute column with string datatype and formatted as 202012062302. I want to select the date and cast it to date, so I made a query that looks like this:
select cast(date_parse(created_date, '%Y-%m-%d') as date) created_date,
daily_user
from
(
select concat(substr(datehourminute,1,4),'-',
substr(datehourminute,5,2),'-',substr(datehourminute,7,2))
as created_date, sum(users) as daily_user
from "a-schema".a-table
group by 1
)
order by 1;
However, that query returns this error:
SQL Error [100071] [HY000]: [Simba]AthenaJDBC
An error has been thrown from the AWS Athena client.
INVALID_FUNCTION_ARGUMENT: Invalid format: "(oth-er-)"
Any idea how to fix this? Thanks!

Convert YYYY-MM-DD into YYYYMMDD and insert into Big query partitioned table

I am trying to perform date conversion of a column from YYYY-MM-DD to YYYYMMDD and insert the final value into a big query partitioned table.
I am getting error as "invalid value" after conversion.
I tried the below,
CASE WHEN o_date is not null THEN PARSE_DATE("%Y%m%d",cast(a.o_date as string))
ELSE PARSE_DATE("%Y%m%d",'19000101')
END as o_date
or
CASE WHEN o_date is not null THEN cast(FORMAT_DATE("%Y%m%d",o_date) as DATE)
ELSE PARSE_DATE("%Y%m%d",'19000101')
END as o_date
Could you please help to achieve the same
Cheers!
Instead of using PARSE_DATE you can use FORMAT_DATE. According to the documentation it formats a date_expression according to a format string, as follows:
FORMAT_DATE(format_string, date_expr)
Notice that the format_string must be a STRING and it should be written between double quotes following these formatting elements dictionary and the date_expr must be a DATE.
I ran the query below to exemplify how it should be:
WITH
a AS (
SELECT
"2020-01-31" AS date )
SELECT
FORMAT_DATE("%Y%m%d", CAST(date AS DATE)) AS new_date
FROM
a
I hope it helps.

SQL: How To Select Earliest Date

I have a date column & I am simply trying to know the earliest date. I use the command:
select Min(Install_date) From PocketGemsSchema.pocketgemstable2;
This returns 1-Dec-17
But the minimum date from my sample data is actually 1-Nov-17.
Can anyone help please?
Try this:
If your Install_date contain datatype varchar than
SELECT MIN(CAST(Install_date AS DATE))
FROM PocketGemsSchema.pocketgemstable2
SELECT FORMAT(MIN(CAST(Install_dateAS DATE)), 'dd-MMM-yy ')
FROM PocketGemsSchema.pocketgemstable2
If your Install_date contain datatype date or datetime than your query will work
I think its the data type issue , you can try two approach
convert the field to datatime and your query should work
cast it on the run time like below
Mysql
SELECT Min(Str_to_date(Install_date, '%m/%d/%Y'))
FROM pocketgemsschema.pocketgemstable2;
SQL server
SELECT Min(Cast(Install_date as datetime))
FROM pocketgemsschema.pocketgemstable2;
I would change the column to a date or a datetime type and sort out any bugs that arise.

How to convert Timestamp to Date Data Type in Google Bigquery

I am trying to convert Timestamp data type columns to Date datatype using:
bq query -q --destination_table=NEW_DATE_TABLE --replace "SELECT DATE(CURR_DT) AS CURR_DT from TEST.DATE_TABLE"
The new table shows the column as STRING rather than date. Is there a way to convert timestamp to date data type.
Requested Screenshot
If you use Standard SQL, you can do the following:
SELECT * REPLACE(EXTRACT(DATE FROM curr_dt)) AS curr_dt FROM test.date_table
If curr_dt is repeated field, then the solution will look the following:
SELECT * REPLACE(
ARRAY(
SELECT EXTRACT(DATE FROM curr_dt) FROM t.curr_dt
) AS curr_dt)
FROM test.date_table t
Consider below!
Works in both Legacy and Standard SQL
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT FROM TEST.DATE_TABLE
Added to address comment
Try below - as I mentioned above - it works for both Legacy and Standard
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT
FROM (SELECT CURRENT_TIMESTAMP() AS CURR_DT)
if you are interested in making your case working with Legacy SQL - provide more details about CURR_DT field
Try this
SELECT TIMESTAMP_SECONDS(CAST(CURR_DT AS INT64)) AS CURR_DT FROM TEST.DATE_TABLE

Extracting most recent day data from sql

I have a table with data being inserted into from a procedure, which then I use for a Javascript chart. My problem is, I only want today's data all the time. I tried dateadd and datediff but I was still getting the same result when I did a select statement.
[![Procedure][2]][2]
If your time column is of type datetime then this simple where will work
select * from throughput
where time = cast(cast(GetDate() as Date) as datetime)
If time column isn't of type datetime then you need to cast it to date as well and use the following query
select *,cast(GetDate() as Date) from throughput
where cast(time as date) = cast(GetDate() as Date)
I have created a fiddle to demonstrate both of the scenario
SQL Fiddle Demo