Converting String to Timestamp in BigQuery - google-bigquery

I am trying to convert this string '1/17/2022, 11:59 PM' into timestamp (Like "01/17/2022 11:59:00 PM") in BigQuery.
Edit: timestamp 2022/01/17 23:59:00 UTC
I have tried with PARSE_DATE function but it did not allow me to convert string. I'd appreciate your help.

Consider to use below query.
SELECT PARSE_TIMESTAMP('%m/%d/%Y, %I:%M %p', '1/17/2022, 11:59 PM') ts

Related

Conversion of datetime format

I have column name requestdatetime with data type string.
Value for requestdatetime is in format 15/Aug/2022:01:54:41 +0000
I need to convert 15/Aug/2022:01:54:41 +0000 into 'yyyy-MM-dd HH:mm:ss' format.
I have tried date_parse(requestdatetime,'%d/%b/%Y'':''HH:mm:ss'' ''+SSS') but it not working out.
You need to convert string to date then date to string to get expected result.
select date_format(parse_datetime('15/Aug/2022:01:54:41 +0000','dd/MMM/yyyy:HH:mm:ss Z'), '%Y/%m/%d %T')
result:
2022/08/15 01:54:41
date_parse accepts MySQL date format, try parse_datetime which accepts Java format (do not forget to add part for timezone offset - Z):
SELECT parse_datetime('15/Aug/2022:01:54:41 +0000', 'dd/MMM/yyyy:HH:mm:ss Z');
Output:
_col0
2022-08-15 01:54:41.000 UTC

How to convert VARCHAR (AM/PM) to TIMESTAMP (24 h) in SQL (Teradata v17)

I've tried multiple solutions, but I keep getting errors. I need to create a new column casting VARCHAR to TIMESTAMP that includes AM, PM or -ideally- changes it to 24 hrs format.
VARCHAR format (Start_Date column): 8/3/2022 4:58:49 PM
I found the below solution is some other post, but I'm getting error: 'Format code appears twice'
SELECT itab.*,
TO_TIMESTAMP(Start_Date, 'MM/DD/YYYY HH:MM:SS AM') AS start_TS
FROM db.info_table itab
Please advise.
You have two problems.
MI is the format for minutes, MM is for months (you have it twice, this is why you are getting that error).
Your date/time string has single digit values for month, day, etc. You can use a pretty simple regex for that.
select to_timestamp(regexp_replace('8/3/2022 4:58:49 PM', '\b([0-9])\b', '0\1'), 'MM/DD/YYYY HH:mi:SS AM')
TO_TIMESTAMP returns a TIMESTAMP(6). If you don't want microseconds you can specify the precision using
CAST(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS timestamp(0) FORMAT 'MM/DD/YYYYbHH:Mi:SSbT')
All you need is pad day and month in Teradata (as opposed to Oracle etc). m/d/y format has not been implemented.
select '8/3/2022 4:58:49 PM' date1,
to_timestamp(
lpad(strtok(date1,'/',1),2,'0')||'/'||lpad(strtok(date1,'/',2),2,'0')||'/'||strtok(date1,'/',3)
,'mm/dd/yyyy hh24:mi:ss AM'
);

Impala convert timestamp AM/PM

I have table in hive with string in 12 hrs format
in which data is appended every day. I don't have control over hive table. Need to expose as view in impala after conversion.
to_timestamp() doesn't support AM/PM format.
Do we have a better way to convert string in 12 hrs format to timestamp.
came across this function which supports AM/PM.which support ISO SQL:2016 standard patterns.
CAST(expression AS type FORMAT pattern)
impala conversion functions
This function is not supported by the impala version I am using.
The other ways I came across:
To add 12hrs to hh:mm if it is PM.
Convert it to 24hrs format in Hive to other table or view using unix_timestamp and have it through impala(require to expose table as view in impala)
can you use below function?
This will give you from string with am/pm to a date time format data.
from_unixtime(unix_timestamp(string_with_ampm, 'MM/dd/yyyy hh:mm:ss a'))
Example
select from_unixtime(unix_timestamp('01/25/2021 09:58:37 AM', 'MM/dd/yyyy hh:mm:ss a')) c
union
select from_unixtime(unix_timestamp('01/25/2021 07:58:37 PM', 'MM/dd/yyyy hh:mm:ss a'))
Result

How to cast 12/01/2019 12:00:00 a.m. to DATETIME in BigQuery

I'm trying to convert a String (12/01/2019 12:00:00 a.m.) to DATETIME.
I have tried with:
PARSE_DATE('%e/%m/%Y %k:%M:%S %P', Fecha_Desc)
no result ... any help?
Two things. First, you need parse_datetime(). Second a.m. is not recognized. So, remove the spaces:
SELECT PARSE_DATETIME('%e/%m/%Y %k:%M:%S %p', replace('12/01/2019 12:00:00 a.m.', '.', ''))
If you really want a date, convert to a date after converting to a datetime -- or just convert the first 10 characters.

How to cast varchar to datetime in sql

I need to cast a string to a datetime so i can compare it later.
The varchar that i have is like this format:
29/11/2013 12:00:00 a.m.
And i need it to cast to:
2013-11-29 00:00:00.000
Im using MSSQL Server 2012
Thx for all
Please, have a look a CAST and CONVERT for more information.
Here are some examples:
-- converting from DD-MM-YYYY
select CONVERT(datetime, '29/11/2013 12:00:00 AM', 103)
-- converting from MM-DD-YYYY
select CONVERT(datetime, '11/29/2013 12:00:00 AM', 101)
IF Your date is of type varchar in the database then if you need to retrieve
then these cases arises::
case1:if your data is in the format of "dd-MM-yyyy"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,103) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'
case2:if your data is in the format of "dd-MM-yyyy HH:mm:ss"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,105) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'