Convert STRING with date text and numbers in BigQuery into DATETIME - sql

I Want to parse or cast the following column which is the format of this example STRING:
18 Apr 2016 10:17:50
into one new column as a DATETIME in this format:
YYYY-MM-DDTHH:MM:SS
So the above would read as an example of one of the thousands of rows:
2016-04-18T10:17:50
I'm not sure what's throwing this one off but I either get a column full of nulls or a message saying:
Mismatch between format character ':' and string character ' '
I've tried parse_Datetime and safe.parsed_datetime with the correct syntax and format elements as well as casting. I'm obviously doing something wrong.

use PARSE_DATETIME you find there also more parameters
SELECT PARSE_DATETIME("%d %b %Y %T", "18 Apr 2016 10:17:50") as dt

Related

Convert a non-standard format string to a date

In SQL Server, I queried a column called DelDate which contains all the date as character string like
"Thu 1/9/2022"
"Wed 12/9/2022"
It impossible to compare manipulate this column with other columns contain standard format dates. I tried to use the command like
Convert(DATE, DelDate)
but got the error message:
Conversion failed when converting date and/or time from character string.
Any suggestions?
Thanks
I would do something like this:
SELECT CONVERT(Date, RIGHT('Wed 12/9/2022', LEN('Wed 12/9/2022') - 4), 103) DelDate2

Bigquery- Parse_date for string column with 'April 9,2013' 'December 13,2014' and more

I am working with a column containing multiple dates in string format with April 9,2013, December 13,2014 and so on. I did find a answer which would be good for single row of data but hasnt worked for this one.
I also tried
SELECT PARSE_DATE('%B %d,%Y,'ColumnName')
but it returned Error: Failed to parse input string "SaleDate"(ColumnName)
I speculate that your table has one or more records which does not match the date format %B %d,%Y. You may flush out offending records using REGEXP_CONTAINS:
SELECT *
FROM yourTable
WHERE NOT REGEXP_CONTAINS(date_col, r'^(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{1,2},[0-9]{4}$')

PARSE_DATETIME Function returing space where it shouldnt

I am trying to convert April 2, 2012, 12:00 AM into a DATETIME/DATE/TIMESTAMP type I dont really mind just not a string.
The problem here however is that within the PARSE_DATETIME function the format string, specifically the %e element string which is for the single digit day is always preceded by a space and in my string I need a comma. The docs read The day of month as a decimal number (1-31); single digits are preceded by a space..
My parse function is as follows PARSE_DATETIME('%B %e, %Y, %I:%M %p','Timestamp) AS 'Date' but I get back the error No matching signature for function PARSE_DATETIME for argument types: STRING, DATETIME. Supported signature: PARSE_DATETIME(STRING, STRING) at [4:5]
Any help would be much appreciated.
You are likely seeing the error message because the timestamp field you are using is already a datetime. Your format string functions as expected with sample data:
with sample_data as (
select 'April 2, 2012, 12:00 AM' as input_string
)
select PARSE_DATETIME('%B %e, %Y, %I:%M %p', input_string) from sample_data
Which results in:
I would make sure your input data is as expected

Is there an R function for changing a strange character date to POSIXct?

My date type looks like 7-Feb-20 (character) data, and I need to convert this into a data/time, so eventually I can use "As.Date() or as.POSIXct()" to move it into the SQL server as datetime. SQL server uses datetime as default when moving from R to SQL.
The as.Date() function will convert character data into a date format.
You can specify the input format by using the format = argument.
In this case, it looks like your format is %d-%b-%y
%d will give you day
%b will give you abbreviated month name (compare with %B, which gives full month name)
%y will give you two-digit year.
You will also need to include the hyphen in your format.

String to DateTime BigQuery

I have a column with strings as datetimes. I want to convert the whole column to datetime format.
Example
"19.03.2020 08:14:13"
"30.04.2020 08:57:45"
I tried this:
select *,
PARSE_DATETIME("%d%m%y %H:%M:%S", Example) as Date_example
from xx.yy.zz
but i got an error
Failed to parse input string "19.03.2020 08:14:13"
Can anyone help me and tell me why this is not working? Is it because day,month and year are separated by the dot instead of slash?
Consider:
PARSE_DATETIME('%d.%m.%Y %H:%M:%S', example) as Date_example
Rationale:
your original format specifier is missing the dot separator between the date components
you want %Y rather than %y for the year component (the former is a 4 digits year, while the later is 2 digits)
We can shorten the query a little with %T:
PARSE_DATETIME('%d.%m.%Y %T', example) as Date_example