1

I'm trying to convert a string into timestamp format in BigQuery.
The source has all dates in the format "10/15 11:59 PM".

Here's what I'm trying as a proof of concept:

SELECT
PARSE_TIMESTAMP(
'%d/%m/%y %I:%M %p',
  CONCAT(SPLIT('10/15 5:00 PM', ' ')[offset(0)] 
    ,'/',FORMAT_DATE('%y',CURRENT_DATE()),' '
    ,SPLIT('10/15 5:00 PM', ' ')[offset(1)],' '
    ,SPLIT('10/15 5:00 PM', ' ')[offset(2)]
  )
)

The string this generates is "10/15/18 5:00 PM" which looks correct to me, but when I run it I get the error:

Query Failed Error: Failed to parse input string "10/15/18 5:00 PM"

Any ideas what else I could try to get this formatted as a timestamp?

Thanks!

2 Answers 2

1

I think you were almost there but your day and month were the wrong way round so it was trying to get the 15th month.

SELECT
PARSE_TIMESTAMP(
'%m/%d/%y %I:%M %p',
  CONCAT(SPLIT('10/15 5:00 PM', ' ')[offset(0)] 
    ,'/',FORMAT_DATE('%y',CURRENT_DATE()),' '
    ,SPLIT('10/15 5:00 PM', ' ')[offset(1)],' '
    ,SPLIT('10/15 5:00 PM', ' ')[offset(2)]
  )
)
Sign up to request clarification or add additional context in comments.

Comments

0

Below is for BigQuery Standard SQL

SELECT
PARSE_TIMESTAMP('%Y/%m/%d %I:%M %p', 
  CONCAT(FORMAT_DATE('%Y/',CURRENT_DATE()),'10/15 5:00 PM')
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.