How to convert string to datetime with correct timezone?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 08:33 PM
I have a field stored as a string in the format "12/30/2022 10:30:00 AM"
- If I use the function TO_DATE, I only get the date part... I want the full date and time.
- If I use the function TO_TIMESTAMP, I get the date and time, but it's assumed to be UTC, which isn't correct (it's actually the local timezone where the data was originally hosted)
- I can potentially use MAKE_TIMESTAMP, but I have to do a lot of string manipulation to get all the components
What is the easiest way to convert the string into a timestamp, with the timezone of my choosing?
- Labels:
-
Databricks SQL
-
Spark sql
-
SQL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 10:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2023 11:06 PM
I don't seem to be getting the same result... note no timezone info in my results;
Is there a config setting I'm missing or something?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 04:57 PM
Just to answer my own question, in case anyone else googles for this...
FirstIy, don't know why timezone isn't displaying in my query results using a SQL Warehouse, but it works ok using a compute cluster, so I'm guessing it is a display issue rather than a data issue.
Secondly, from what I can tell, using the TO_TIMESTAMP function will apply the timezone for your session (which might not be the timezone you want). I didn't realise this could be solved pretty easily by simply pasting your timezone to the end of the date string, like this;
TO_TIMESTAMP(MyDateTimeString || ' Timezone', 'M/d/y h:m:s a z)For example;
TO_TIMESTAMP(MyDateTimeString || ' America/Los_Angeles', 'M/d/y h:m:s a z)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2023 06:30 AM
Use to_utc_timestamp(expr,timezone)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 04:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2024 10:18 AM
TO_DATE("12/30/2022 10:30:00 AM", "MM/dd/yyyy HH:mm:ss a") AS tsDateIs this correct if we need only date?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 03:48 AM
use from_utc_timestamp(to_timestam("<string>", <format>),<timezone>)