BigQuery CAST whole string column to date after LEFT - sql

The data came in datetime format but was upload as a string to BigQuery.I want to extract only the date part but not the time part. I try to use a subquery using code:
SELECT CAST(LEFT(SleepDay,9) AS DATE)
but I kept getting: Invalid datetime string "4/12/2016" as an error. Please help me fix it thank you so much.

try use BIGQuery parse_date() function.
SELECT PARSE_DATE('%m/%d/%Y', LEFT(SleepDay,9))

I would not recommend using the LEFT(date_str, 9) function, since you don't want to parse 12/31/2022 as 12/31/202. Instead, parse the date_string and use the PARSE_DATE() function with proper format strings.
WITH
dataset AS (
SELECT "4/12/2016T12:34:56" as SleepDay
UNION ALL SELECT "4/15/2016T12:34:56" as SleepDay
),
some_how_parse_the_date_string AS (
SELECT SPLIT(SleepDay, 'T')[OFFSET(0)] as SleepDay_date_string
FROM dataset
)
SELECT PARSE_DATE('%m/%d/%Y', SleepDay_date_string) as SleepDay_date
FROM some_how_parse_the_date_string
;

Related

Want to convert timestamp to date format in hive

want to convert this number '20210412070422' to date format '2021-04-12' in hive
I am trying but this returns null value
from_unixtime(unix_timestamp(eap_as_of_dt, 'MM/dd/yyyy'))
The best methoid is to do without unix_timestamp/from_unixtime if possible and in your case it is possible. date() can be removed, string in yyyy-MM-dd format is compatible with date type:
select date(concat_ws('-',substr(ts,1,4),substr(ts,5,2),substr(ts,7,2)))
from
(
select '20210412070422' as ts
)s
Result:
2021-04-12
Another efficient method using regexp_replace:
select regexp_replace(ts,'^(\\d{4})(\\d{2})(\\d{2}).*','$1-$2-$3')
If you prefer using unix_timestamp/from_unixtime
select date(from_unixtime(unix_timestamp(ts, 'yyyyMMddHHmmss')))
from
(
select '20210412070422' as ts
)s
But it is more complex, slower (SimpleDateFormat class is involved) and error prone because will not work if data is not exactly in expected format, for example '202104120700'
Of course you can make it more reliable by taking substring of required length and using yyyyMMdd template:
select date(from_unixtime(unix_timestamp(substr(ts,1,8), 'yyyyMMdd')))
from
(
select '20210412070422' as ts
)s
It makes it even more complex.
Use unix_timestamp/from_unixtime only if simple substr or regexp_replace do not work for data format like '2021Apr12blabla'.

postgres sql to extract year-month

Have a table with a column like this:
first_day_month
01/07/2020
01/07/2020
01/08/2020
01/09/2020
.......
Need to create a column like year-month,
Tried to_char(first_day_month, 'MM/YYYY') but got an error:
Error running query: INVALID_FUNCTION_ARGUMENT: Failed to tokenize string [M] at offset [0]
Tried
concat(extract(year from first_day_month),'-',extract(month from first_day_month) ) as month,
with an error:
Error running query: SYNTAX_ERROR: line 2:1: Unexpected parameters (bigint, varchar(1), bigint) for function concat. Expected: concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar)
Also tried date_parse but didn't get it right, any idea?
Thanks
You need to use TO_DATE first, to convert the column to a proper date. Then use TO_CHAR to format as you want:
SELECT TO_CHAR(TO_DATE(first_day_month, 'DD/MM/YYYY'), 'MM/YYYY') AS my
FROM yourTable;
Note that in this case since the text month year you want is actually just the right substring, you could also directly use RIGHT here:
SELECT RIGHT(first_day_month, 7)
FROM yourTable;
Finally, note that YYYY/MM would generally be a better format to use, as it sorts properly. So perhaps consider using this version:
SELECT TO_CHAR(TO_DATE(first_day_month, 'DD/MM/YYYY'), 'YYYY/MM') AS ym
FROM yourTable;
Your data doesn't seem to be of DATE type, might be string, then need to convert to DATE type first and format display style as desired pattern :
SELECT TO_CHAR(first_day_month::DATE,'MM/YYYY') AS first_day_month
FROM t
Demo

BigQuery: String to Timestamp

I have a timestamp from the source that has been loaded to BQ as a string. I'd like to write a query in BigQuery that will return timestamp in the following format 2020-01-06 11:09:14.000-0600. Here is the current format of the string field: 2020-01-06T11:09:14.000-0600, 2018-10-01T15:45:59.000-0500, etc.
I have tried the following:
SELECT parse_timestamp ("%Y-%m-%dT%H:%M:%S.%E3S", start_timestamp, "America/Chicago"), FROM bqtable
The goal is to perform arithmetic on the timestamp fields.
Any feedback is appreciated. Thank you.
I think the %S and %E3S% are conflicting, as they both are parsing the seconds part of the string.
Try this:
with data as (
select '2020-01-06T11:09:14.000-0600' as ts_string union all select '2018-10-01T15:45:59.000-0500'
)
select ts_string, parse_timestamp ("%Y-%m-%dT%H:%M:%E3S%z", ts_string, "America/Chicago") as ts
from data

Convert Number type to Date in postgres SQL

I have a numeric data in a column 20170930, need help in converting it into Date in PostgreSQL , tried multiple ways but non seems to work
You can convert to a string and then to a date:
select column::text::date
You can also express this using explicit cast() syntax:
select cast(cast(20170930 as text) as date)
Use one of the following :
SELECT cast(yourcol::varchar as date ) as dt1, yourcol::varchar::date as dt2
where dt1 and dt2 values of type date, and yourcol is a numeric value such as 20170930
Demo
The best thing is to change column datatype into Date type,
ALTER TABLE table_name
ADD column_name Date;
As shown above, PostgreSQL supports a full set of SQL date and time types, as shown in the table below. Dates are counted according to the Gregorian calendar. Here, all the types have a resolution of 1 microsecond / 14 digits except date type, whose resolution is day.
Please try below query
SELECT to_date(column::varchar,'YYYYMMDD')
For anybody who fell into my pitfall I tried this but my numeric was like a 'seconds past 01-01-1970 format' rather than YYYYMMDD
This worked
SELECT to_timestamp(yourcol) as numeric_column_now_date
from yourtable
see here
https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT

SQL Convert String to Date

I am trying to find a way of extracting the first part of line of string and separating as a date. The following is an example of some of the data.
17/10/12 lskell Still waiting for one more signature on the
I have tried casting the whole field as a date, and converting to a date, but these fail?
Would anyone have any ideas?
Try this for MySql
DATE_FORMAT(STR_TO_DATE(SUBSTRING_INDEX(columnname,' ',1), '%d/%m/%y'), '%Y-%m-%d')
If you know that
the first "word" in your string will always be a date
the date will always be separated from the rest of the string by a space
the date will have a consistent format
you are working in MySQL
then try this:
SELECT CAST(SUBSTRING_INDEX(myfield, ' ', 1) AS DATE) adate FROM mytable;
Try:
SELECT SUBSTRING('10/17/12 lskell Still waiting for one more signature on the', 1, 8)
(assuming date comes in this format)
-- MYSQL:
SELECT Str_to_Date(Left(yourstring,8),'%d/%m/%y') from yourtable;
-- Oracle
SELECT TO_DATE(left(yourstring,8),'dd/mm/yy')
from your table;
--- sql server
SELECT CONVERT(DATETIME,left(yourstring,8),120) from your table;