Snowflake - convert string to datetime - datetime

I am using snowflake and I have date as a string in this format
'2021-04-01 08:00:05.577209+00'
I want to convert it to DateTime. I used the below code to do this (I trim '+00' from each string first). However I think I defined it somehow wrong, so I keep getting errors.
TO_TIMESTAMP_NTZ(left(ts,len(ts)-4),'YYYY-MM-DD HH24:MI:SS.FF'),

Why do you want to trim the +00 off? just do it like this:
select to_timestamp_ntz('2021-04-01 08:00:05.577209+00', 'YYYY-MM-DD HH24:MI:SS.FF+00')

It would be better to use left(ts,len( ts)-3) instead of left(ts,len( ts)-4) to trim last 3 characters.
Can you check your data and be sure it is '2021-04-01 08:00:05.577209+00' cause it works as expected (tested with both):
select ts,
left(ts,len( ts)-3) trimmed,
TO_TIMESTAMP_NTZ(left(ts,len( ts)-3),'YYYY-MM-DD HH24:MI:SS.FF') result
from values ('2021-04-01 08:00:05.577209+00') tmp (ts);
Result:
+-------------------------------+----------------------------+-------------------------+
| TS | TRIMMED | RESULT |
+-------------------------------+----------------------------+-------------------------+
| 2021-04-01 08:00:05.577209+00 | 2021-04-01 08:00:05.577209 | 2021-04-01 08:00:05.577 |
+-------------------------------+----------------------------+-------------------------+

I have found answer on my question. I was reading data from CSV files on Azure Data Lake and I haven't noticed quotes in a columns. When I deleted them everything is working fine.

Related

Mariadb get string in binary format BE

If I am run query:
SELECT HEX(BINARY(CONVERT('ßÁÁÁÁÁȵ$€Łß' USING ucs2)));
I am get:
00DF00C100C100C100C100C1010C00B5002420AC014100DF
and I suppose that sequence is BE, because in txt file in UTF-16 BE is the same sequence.
How to get sequence in UTF-16 LE?
You ask why I want LE? Because the query on MS SQL server:
SELECT CONVERT(varbinary(100), N'ßÁÁÁÁÁȵ$€Łß',0)
return:
0xDF00C100C100C100C100C1000C01B5002400AC204101DF00
Thank
Jaroslav
You need to cast with a little endian character set:
SELECT HEX(BINARY(CONVERT('ßÁÁÁÁÁȵ$€Łß' USING utf16le)));
+----------------------------------------------------------------+
| HEX(BINARY(CONVERT('ßÁÁÁÁÁȵ$€Łß' USING utf16le))) |
+----------------------------------------------------------------+
| DF00C100C100C100C100C1000C01B5002400AC204101DF00 |
+----------------------------------------------------------------+

is there any way to parse a date in jq which contains milliseconds, or is there a way to get the interval between two dates without parsing?

So basically i need to get an interval between 2 dates and i have found how to do that using mktime, then deducting the two numbers.
But from my searching in the strptime c library, there doesnt seem to be a way to parse a date containing milliseconds, so im asking if there is any way around this, or if there is any way to parse a date containing milliseconds.
Edit: if there is any way to round up or down the date, or just remove the milliseconds that would work too
example:
{
"ActiveFrom": "2022-02-13T11:32:01.321345+04:00",
"ActiveTo": "2022-02-13T11:33:13.031743+04:00"
}
You could strip the fractional seconds with a substitution, and then go via strptime (required because of non-UTC timezone) to get a Unix timestamp:
echo '{
"ActiveFrom": "2022-02-13T11:32:01.321345+04:00",
"ActiveTo": "2022-02-13T11:33:13.031743+04:00"
}' \
| jq '.ActiveFrom
| sub("\\.[[:digit:]]+"; "")
| strptime("%Y-%m-%dT%H:%M:%S%z")
| mktime'
resulting in 1644751921 (jq playground).
If your dates are of OBJECT data type, you can easily calculate the interval like this:
var interval = date_later - date_early;

Why Google Bigquery Datetime Format is with a 'T' separator? I want a space separator

I am creating table in BigQuery with schema from json file and giving DATETIME datatype to a column. Then i have simply loaded data from a csv in the datetime format of 2020-01-01 00:00:00 but when it goes into bigquery, i can see it is having a 'T' separator i.e. 2020-01-01T00:00:00.
Its even mention on google bigquery docs - YYYY-[M]M-[D]D[( |T)[H]H:[M]M:[S]S[.DDDDDD]] , ( |T): A space or a T separator. But i dont know how to give a space separator instead of the default T separator. Please help. Thanks.
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
The option is using the Datetime function:
FORMAT_DATETIME(format_string, datetime_expression)
For example:
bq query --nouse_legacy_sql \
"SELECT FORMAT_DATETIME('%Y-%m-%d %H:%M:%S', '2020-01-01 13:45:55') AS datetime;"
+---------------------+
| datetime |
+---------------------+
| 2020-01-01 13:45:55 |
+---------------------+

How do I use getschema for customDimensions?

I've used a query like
requests
| getschema
to get a table containing the names and types of all columns in the requests table. How can I get the same result for requests.customDimensions?
It looks like this can be done using buildschema and then transforming the output into a consumable format (thanks Dmitry Matveev for helping me out with that piece 🙂):
requests
| summarize schema=buildschema(customDimensions)
| mvexpand bagexpansion=array schema
| project name=schema[0], type=schema[1]

Datetime not formatting properly as d:hh:mm

I have a Google spreadsheet where I'm finding the amount of time between two datetime values. The calculation seems to be working properly, because when I output the value as just a number, it looks correct. However, when I try to format it as d:hh:mm, for any values where the two dates are on the same day, it is showing me 30 days instead of 0. Some sample data:
Datetime | Diff as # | Diff as d:hh:mm
8/13/2016 20:24 | |
8/27/2016 00:09 | 13.15625 | 13:03:45 (this line is correct)
8/27/2016 04:43 | 0.190278 | 30:04:34 (this is incorrect, should be 0 days, not 30)
When I try the same thing in Excel, it works as expected. (instead of 30:04:34, I get 00:04:34) So I think it's some difference between the two that I'm not familiar with. Can someone assist?
At present, Google sheets don't have the feature to show the duration in days (via custom format), you can only have it in hours and/or minutes and/or seconds..
Screenshot:
Still there is a workaround to get the desired format, try the following formula in cell C3:
=(datevalue(A3)-datevalue(A2))&text(time(0,0,(A3-A2)*24*60*60),":HH:mm")
Screenshot:
EDIT (after viewing the comment):
Try the following formula:
=rounddown(A3-A2)&" days "&text(time(0,0,(A3-A2)*24*60*60),"HH:mm")

Resources