Skip to content
Advertisement

Snowflake/javascript datetime format changes during iteration

JavaScript

Javascript converts above date into something like this ‘Tue Dec 07 2021 14:52:12 GMT-0800 (Pacific Standard Time)’

Here is the javascript code

JavaScript

Above code between ** … ** is modifying the timestamp

however i need to get same datetime stamp(2021-12-07 15:17:04.673 -0800) and

NOT ‘Tue Dec 07 2021 14:52:12 GMT-0800 (Pacific Standard Time)’

How to format the javascript iteration code to return datetime/time stamp. Thx

Advertisement

Answer

I have tested, if you force the timestamp column in your Snowflake query to be string, then it will avoid your issue.

See test case below.

  1. Prepare the data (note that the timestamp column needs to be TIMESTAMP_TZ type, otherwise it will NOT preserve the original string as it gets converted to either local timezone or no timezone, depending on your setting):
JavaScript
  1. prepare the SP:
JavaScript
  1. call the SP using timestamp value (it will show your issue):
JavaScript
  1. call the SP by casting timestamp value to STRING (what you wanted):
JavaScript

So you need to make sure you use timestamp_tz data type and cast to string, it should help to resolve your issue.

UPDATE

We can use getColumnValueAsString() instead of getColumnValue() of the ResultSet object to cast the value from TIMESTAMP to STRING inside the JS, rather than at the SQL level.

https://docs.snowflake.com/en/sql-reference/stored-procedures-api.html#getColumnValueAsString

So updated the SP is below:

JavaScript

And then we can just run SELECT *:

JavaScript

I do not know what controls the number of digits in the milliseconds part, as the TIMESTAMP_%_OUTPUT_FORMAT parameters can’t seem to control it.

You might need to manually get rid of the trailing zeros if you have to.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement