Mariadb get string in binary format BE - mariadb

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 |
+----------------------------------------------------------------+

Related

Kusto - extract key value from the Kusto table result

How do I extract a set of key value from Kusto Table result. I have a Data field (column in Kusto table) that has log details (15 lines with time stamp). Out of these 15 lines, the last 3 lines has a key value pair which I will need to use in the Query to filter and display results.
Do I use the below method can you give some examples
Extract values on column with strings sharing the same format or pattern -
Example Values from column are (last 3 lines from the Data section)
2021-09-05T06:42:19.2287304Z VMtype - C4
2021-09-05T06:42:19.2287304Z patchsizeMB - 2533```
I am trying with the below Query is this the right way of doing
```| parse Data with * "Virtual machine =" Virtual machine
| parse Data with * "VMtype=" VMtype
| parse Data with * "patchsizeMB=" patchsizeMB```
The way that you implemented it is fine, easiest to understand, and maintain.
Here is a full example, where the results are returned in one row for each timestamp:
datatable(Data:string)["2021-09-05T06:42:19.2287304Z VMtype - C4",
"2021-09-05T06:42:19.2287304Z patchsizeMB - 2533",
"2021-09-05T06:42:19.2287304Z Virtual machine - VM_Name"]
| parse Data with Timestemp:datetime " " *
| parse Data with * "Virtual machine -" VirtualMachine
| parse Data with * "VMtype -" VMtype
| parse Data with * "patchsizeMB -" patchsizeMB
| summarize take_any(VirtualMachine), take_any(VMtype), take_any(patchsizeMB) by bin(Timestemp, 1d)
Results:
Timestemp
VirtualMachine
VMtype
patchsizeMB
2021-09-05
VM_Name
C4
2533
You can also implement it by splitting the string into an array and working on the array to populate the appliable values but it is much more code and probably more fragile.

Snowflake - convert string to 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.

What could be the reason why sqlite's pattern match does not work?

I have a database file with log data. The database contains a table LOG and the table contains a column MSG. There are 30 rows in the table, where the MSG column contains the string "down" at the end of the line:
$ sqlite3 log.db "select msg from log" | grep down$ | wc -l
30
But when I try to find them with LIKE, I get no match:
$ sqlite3 log.db "select msg from log where msg like '%down'" | grep down$ | wc -l
0
What could be the reason for this?
Update: MCVE
CREATE TABLE LOG (MSG VARCHAR(6291456) NOT NULL);
INSERT INTO LOG VALUES (X'666163696c6974793d6461656d6f6e3b636f6d706f6e656e743d6e616d65643b746578743d7368757474696e6720646f776e');
SELECT MSG FROM LOG; -- returns the row
SELECT MSG FROM LOG WHERE MSG LIKE '%down'; -- returns nothing
SELECT MSG FROM LOG WHERE MSG LIKE '%down%'; -- returns nothing
SELECT MSG FROM LOG WHERE CAST(MSG AS VARCHAR) LIKE '%down'; -- returns the row
I have no idea why a cast from VARCHAR to VARCHAR makes a difference.
Update: Another MCVE
CREATE TABLE LOG (MSG VARCHAR(6291456) NOT NULL);
INSERT INTO LOG VALUES (X'666163696c6974793d6461656d6f6e3b636f6d706f6e656e743d6e616d65643b746578743d7368757474696e6720646f776e');
INSERT INTO LOG VALUES ('facility=daemon;component=named;text=shutting down');
SELECT ROWID,MSG FROM LOG; -- returns both rows
SELECT ROWID,MSG FROM LOG WHERE MSG LIKE '%down'; -- returns just the second
SELECT ROWID,MSG FROM LOG WHERE MSG LIKE '%down%'; -- returns just the second
SELECT ROWID,MSG FROM LOG WHERE CAST(MSG AS VARCHAR) LIKE '%down'; -- returns both rows
SELECT HEX(MSG) FROM LOG;
Given your sample data and the results you're seeing on different versions of sqlite, here's what I'm sure is happening.
First, you're inserting blobs into your table, not strings. These blobs are stored unchanged, instead of being converted to strings the way numeric values are for a column with TEXT affinity like you're using. See the documentation for details about column affinity and implicit datatype conversions.
Second, the sqlite3 instance that's not matching those blobs was built with the SQLITE_LIKE_DOESNT_MATCH_BLOBS configuration option turned on, and the one that is matching them was built with it turned off (The default setting).
This compile-time option causes the LIKE operator to always return False if either operand is a BLOB. The default behavior of LIKE is that BLOB operands are cast to TEXT before the comparison is done.
If you check PRAGMA compile_options output you should be able to verify that it's being used.
I would paste this into the comments, but the formatting would look ugly. It works for me:
$ sqlite3 log.db
SQLite version 3.24.0 2018-06-04 14:10:15
Enter ".help" for usage hints.
sqlite> create table log (id int, msg text);
sqlite> insert into log values (1,'database is down');
sqlite> insert into log values (2,'database is down');
sqlite> insert into log values (3,'database is down');
sqlite> ^D
$ sqlite3 log.db "select msg from log" | grep down$ | wc -l
3
$ sqlite3 log.db "select msg from log where msg like '%down'" | grep down$ | wc -l
3
Can you share the contents of your log table, so I can try to reproduce your problem?

Why does end-of-line operator ($) not work in MariaDB unless I include a trailing null?

This is the field of interest:
something VARCHAR(8) NULL,
It can contain non-digits, but I need to select for rows that contain only digits in this field. I have tried to use this regular expression:
something RLIKE '^\\d+$'
This fails to match. However, if I add a Kleene closure for null bytes at the end, it matches. This is the expression that matches:
something RLIKE '^\\d+\x00*$'
My question is: am I doing something wrong? Is there a flag I should have set? I don't see this mentioned in the documentation anywhere. MariaDB's documentation does not mention padding VARCHARs with null bytes. In fact, a section in their documentation leaves me with the impression that my first regular expression should have worked. Here is a quote from the documentation:
This example checks if the string consists of "word" characters only:
SELECT 'abc' RLIKE '^\\w+$';
-> 1
You don't give version or code samples, but the following works in MariaDB 10.3.9 with default settings:
CREATE TABLE r (x VARCHAR(8) NULL);
INSERT INTO r VALUES ('abc'),('123'),('1d3');
SELECT x RLIKE '^\\d+$' FROM r;
+------------------+
| x RLIKE '^\\d+$' |
+------------------+
| 0 |
| 1 |
| 0 |
+------------------+
https://mariadb.com/kb/en/library/server-system-variables/#default_regex_flags has some behavior flags, but I don't know if this is relevant to your case.

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]

Resources