How to convert string to timestamp in looker

Hi team,

We have date string as 20210131120101(YYYYMMDDhhmmss) in table. We want to interpret this string into timestamp so looker can treat it as time stamp.

Then we want to extract date parts from 20210131120101:

2021: year

01: month

31: day

12:hour

01: minute

01:second

I have tried parse_timestamp function because my source database is bigquery but this is the error I am getting:

b71f06a1-3e1f-4e1c-9754-b8ce26f680bf.png

Thanks in advance

Thanks,

Rohit

Solved Solved
0 13 5,970
1 ACCEPTED SOLUTION

This function parsed the example you provided:

parse_timestamp('%Y%m%d%H%M%S',cast(20210131120101 as string))

To apply it to your column, you should use this and replace “your_column” with the column name that contains your timestamp:

parse_timestamp('%Y%m%d%H%M%S',cast(your_column as string))

The error you’re getting on that message refers to the second argument of the function (the column containing the timestamp) not being of string datatype, but float datatype. You should cast your float to a string first, just like in the example.

I hope this helps.

Warmly, 

View solution in original post

13 REPLIES 13

Dawid
Participant V

I can’t believe there’s no better way to do it in BigQuery O_o but this is the only thing I came up with:

SAFE.PARSE_DATETIME('%F %T', 
CONCAT(
SUBSTR('20210131120101',1, 4),
'-',
SUBSTR('20210131120101', 5, 2),
'-',
SUBSTR('20210131120101', 7, 2),
' ',
SUBSTR('20210131120101', 9, 2),
':',
SUBSTR('20210131120101', 11, 2),
':',
SUBSTR('20210131120101', 13, 2)
)
)

I would suggest wrapping it in a function

Dawid,

This is the data in my field:

7a4a80b9-a768-42fa-bbfd-b895fac72431.png

I tried that substr function 

c4f2f4bf-bde4-47d8-bab6-3aec85113cd5.png

but this small expression is failing.

4adf3fe0-9986-4dba-ac85-593c7cad81e6.png

Can you help me to fix this error:

Thanks,

Rohit

This function parsed the example you provided:

parse_timestamp('%Y%m%d%H%M%S',cast(20210131120101 as string))

To apply it to your column, you should use this and replace “your_column” with the column name that contains your timestamp:

parse_timestamp('%Y%m%d%H%M%S',cast(your_column as string))

The error you’re getting on that message refers to the second argument of the function (the column containing the timestamp) not being of string datatype, but float datatype. You should cast your float to a string first, just like in the example.

I hope this helps.

Warmly, 

I tried but this is

156f6922-8985-45cf-885f-4f2eec9446c6.png

the new error:

c4dc6e88-7b07-43f1-a24d-992d228f4534.png

Dawid
Participant V

This looks like your data isn’t always the same, meaning this error shows a value without the seconds, so do you have them all in the same format? if you have multiple formats you may need to use CASE with SAFE.PARSE_TIMESTAMP IS NULL 

I have used:

571a384c-3707-4dee-9dc6-f1aed18444c9.png

This is the result:

f7f2ae27-8c57-486c-80dc-286df737ccae.png

Is this conversion fine?

I am surprised to see small letter m and d for month and day, capital M for minute. it should be YYYYMMDDhhmmss. 

Dawid, 

One last question on this thread and I am not sure it is relevant or not

In Qlik Sense, date or timestamp stores in numeric form in backend as 4993.32344 if timestamp and 4993 as date and we convert this number to any date as required format.

In Looker, how does raw or backend format of date stored? for example if I converted the string to timestamp then how actually it is stored?

20210101120101(YYYYMMDDhhmm) or in some number?

https://cloud.google.com/bigquery/docs/reference/standard-sql/format-elements#format_elements_date_t...

Here’s the reference for the format elements (link above). You might have to adjust based on this, but as Dawid said, it looks like not all of the data is in the same format. Using the prefix “safe.”  before the parse_timestamp function should return nulls whenever the function fails to parse the string because it is in a different format.

safe.parse_timestamp('%Y%m%d%H%M%S',cast(your_column as string))

Anyway, it looks like you got it working! 

Regarding your storing question… If I’m not mistaken, Looker does not store any values in their backend. They simply run queries against your database based on the LookML you’ve written (LookML generates SQL, it doesn’t actually store stuff) and return the results, so your database dialect will determine how things are stored and parsed. In this case, dates or timestamps are stored precisely as date or timestamp datatypes. BigQuery has a lot of great documentation, so you should be able to clear any doubts using that.

Thanks @jbendinger-1633  for such great information. Actually my two tables have common field a timestamp which has format: %Y%m%d%H%M.

I want to apply a join between those such tables. What is the best way here to apply the join? should I apply the join on raw value whose format is %Y%m%d%H%M%S or convert it to another best format where join never fails?

If both your tables are storing the information in the same datatype and format, I’d recommend just joining on the raw field. The reason for it is that the query will take a bit longer to run if you apply the parsing, because it will have to parse every record in each table and then compare those two records and join, whereas without parsing it would just evaluate if two numbers are equal without adding the extra step of parsing (I think you mentioned you’re storing them in float64 datatype). 

Anyway, not a critical difference. However, if your columns are NOT storing the values exactly the same, I would recommend casting them to the same standard and then joining on them. I’d also say give both approaches a try and then compare results. But again, all this is very dependent on your data and the uniformity of it.

Just as a quick example, if you had value_table_1 = 20210101000000 (Jan 1st 2021 at 00:00:00) and value_table_2 = 20210101000000 (same as before), joining on those two would evaluate to true because both numbers are exactly the same, therefore producing a good join. However, if you had something like value_table_1 = 202111000000 (Jan 1st 2021 but dropping the 0 from the month and day) and then value_table_2 = 20210101000000 (same as previous example, where months and days do have zeroes), you might have to cast them both to the same timestamp format using the format elements to produce 2 timestamps that look the same, because value_table_1 (202111000000) is different than value_table_2 (20210101000000). I hope what I’m saying makes sense!

Warmly,

@jbendinger-1633 you are right most of the time. 

My use case says, I could have two kinds of time stamp value of one instant or I can say one hour.

My DB stores data up to hourly level, for example:

202101011100(YYYYMMDDhhmm) this is 1st Jan 2021, 11 AM and 0minutes.

Another value could be 202101011130(YYYYMMDDhhmm) this is 1st Jan 2021, 11 AM and 30 minutes.

Earlier we have noticed that this 30th minute cause problem in joining.

I want to round my field to 1/24, or I can say hour as:

=timestamp(round('2022-03-29 23:30:00',1/24)) is equal to 

06bc86c7-40b0-4153-a5f5-e559d1dbbb95.png

This will always round the timestamp at hour level and join always work for both tables.

I am struglling how to do it in Looker+BQ.

I know how to do it other tool like Qlik but I am new here, need your help. 

Can you please tell me how to round the timestamp by 1/24. 1/24 is the step. When I say step, it means, read below example:

Round(2,6 )

Returns 0. Rounded down because 2 is less than half of the step interval of 6.

In this example, the size of the step is 6 and the base of the step interval is 0.

The intervals are ...0 <= x <6, 6 <= x <12, 12<= x <18...

Hi,
I have got a similar requirement but the parse_date function is not giving me the desired output.

I have a field in the table which is of data type string and I need to convert to date. The values are as 202203,202204 and so on. (YYYYMM). I need to convert this value into Month/Mon - Year.

So the desired output is  :  March 2022, April 2022. Any suggestions, please?

 
Top Labels in this Space
Top Solution Authors