MariaDB regexp_replace backreference not working as expected - mariadb

I have this simple regex in a user defined function
set mm = regexp_replace(in_str, '^M([0-9]+(\.[0-9]+)?).*$', '\\1');
when passing in 'M56-5' in MariaDB 10.1 I would get "56" as expected but in MariaDB 10.4 I get an error "Data truncated for column 'mm' at row 1". When I pass in 'M56.5' I get "56.5" in both versions as expected. Did something change?

I figured it out. I mistakenly was using a single backslash to escape the "." whereas it is required to use 2 backslashes when escaping special characters, one for the MariaDB parser and one for the regex library. https://mariadb.com/kb/en/regular-expressions-overview/#escaping

mysql> SELECT regexp_replace('M56-5 foo', '^M([0-9]+(\.[0-9]+)?).*$', '$1');
+---------------------------------------------------------------+
| regexp_replace('M56-5 foo', '^M([0-9]+(\.[0-9]+)?).*$', '$1') |
+---------------------------------------------------------------+
| 56-5 |
+---------------------------------------------------------------+
(That is using MySQL 8.0.31 -- There has been some inconsistencies lately in the syntax of regexps for MySQL and MariaDB versions.)

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

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.

Pervasive database: Problem with spaces in column names

We use pervasive Database with Zen Contol-Center And want to perform the following SQl-Statenemt:
insert into MWlog
(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,`Messwert neu`,Zugriff,`Messwert alt`,Aenderungsgrund)
values
('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5','5.62','Neuer Import')
We get the following error-message:
*<<<<<<<<<<<<<<<<<<<<<<<<
insert into MWlog(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,`Messwert neu`,Zugriff,`Messwert alt`,Aenderungsgrund) values ('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5','5.62','Neuer Import')
[Zen][SQL Engine]
Syntax Error: insert into MWlog(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,<< ??? >>`Messwert neu`,Zugriff,`Messwert alt`,Aenderungsgrund) values ('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5',
>>>>>>>>>>>>>>>>>>>>>>>>*
What is wrong in the SQL Statement?
(We found out that the problem are the spaces in the field-Names)
We are using
Zen Control Center
Zen Install Version 14.10.035.
Java Version 1.8.0_222.
Gord Thompson is correct. You need to use double quotes around field names with spaces or field names that are reserved key words. You statements should be:
insert into MWlog
(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,"Messwert neu",Zugriff,"Messwert alt",Aenderungsgrund)
values
('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5','5.62','Neuer Import')

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.

Autosys Job depends regular expression

I am using the Jobdepends command to get a list of jobs stating with tax_check_d_%
I need files like tax_check_d_job
but it returns
tax_check_djob
tax_check_d_job
tax_check_djob_job
how can i select only tax_check_d_job?
When used in a job filter, the _ (underscore) character is a wildcard that matches exactly one character. This explains why you are seeing the unwanted results coming back -tax_check_d_job_% is essentially the same as tax_check_d_job%.
Unfortunately I don't think there's a way around this - according to the Autosys 4.0 reference guide, the SQL ESCAPE option is not supported for wildcards.
I would suggest adding one more character onto your search criteria to filter out the unwanted results, running the command multiple times if necessary. E.g. run it with tax_check_d_job_a%, tax_check_d_job_b%, ... as necessary.
You could also use some custom code to strip out the results you don't want to see. Depending on your needs, this could be a simple as
job_depends -d -J tax_check_d_job_% | grep "tax_check_d_job_.*"

Resources