Are UTC_TIMESTAMP () and CONCAT (UTC_DATE (), '', UTC_TIME ()) functionally identical?
EDIT:
When I tested it, UTC_TIMESTAMP() does not return a UNIX TIMESTAMP. (with MariaDB 10.x)
No. UTC_TIMESTAMP returns a string or number, the CONCAT returns a string.
UTC_TIMESTAMP, UTC_TIMESTAMP([fsp])
Returns the current UTC date and time as a value in 'YYYY-MM-DD
HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function
is used in a string or numeric context.
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-timestamp
https://mariadb.com/kb/en/library/utc_timestamp/
CONCAT(str1,str2,...)
Returns the string that results from concatenating the arguments.
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
https://mariadb.com/kb/en/library/concat/
create table test ( real_ts timestamp, fake_ts varchar(40));
✓
insert into test (real_ts, fake_ts)
values (utc_timestamp()+6, CONCAT (UTC_DATE (), '', UTC_TIME ()));
✓
select
*
from test;
real_ts | fake_ts
:------------------ | :-----------------
2017-11-09 06:57:15 | 2017-11-0906:57:09
insert into test (real_ts)
values (utc_timestamp()+6)
;
✓
insert into test (real_ts)
values ( CONCAT (UTC_DATE (), '', UTC_TIME ())+6)
;
Truncated incorrect DOUBLE value: '2017-11-0906:57:09'
dbfiddle here
I would not trust the CONCAT (even if it would work). What if the implementation called UTC_DATE() just before midnight, and called UTC_TIME() just after? Oops, the combined string is off by a whole day!
Related
I tried a lot of ways to use user defined variables in MariaDB version 10.3.22. After failing to use it in my application, I wanted to try with a simple example:
DECLARE #EmpName1 NVARCHAR(50)
SET #EmpName1 = 'Ali'
PRINT #EmpName1
gives Unrecognized statement type. (near "DECLARE" at position 0)
After some digging around I tried using it between delimiters and as a created function:
DELIMITER //
CREATE FUNCTION test
DECLARE #EmpName1 VARCHAR(50)
SET #EmpName1 = 'Ali'
PRINT #EmpName1
END //
DELIMITER;
This gives
Unrecognized data type. (near ")" at position 54)
A "RETURNS" keyword was expected. (near "END" at position 110)
I cannot figure out where the issue might be coming from, as the MariaDB documentation has the same syntax as far as I can see.
Can anyone help solving this issue? My final goal would be to assign the single result of a query to a variable as a string.
A few syntax matters:
Need a () set after the function name, even if no parameters are used:
CREATE FUNCTION test()
A function's return data type must be specified after that: (I used the same type/size as your variable. Can be some other type, of course, depending upon what is being returned)
CREATE FUNCTION test() returns varchar(50)
The use of # with the variables not needed, also missing ; at the end of each line, plus PRINT is invalid:
DECLARE EmpName1 VARCHAR(50);
SET EmpName1 = 'Ali';
-- PRINT EmpName1; see item 4
Functions are expected to return a value:
RETURN EmpName1; -- I simply replaced the PRINT with RETURN here.
Putting that all together, the complete definition becomes:
DELIMITER //
CREATE FUNCTION test() RETURNS VARCHAR(50)
BEGIN
DECLARE EmpName1 VARCHAR(50) DEFAULT '';
SET EmpName1 = 'Ali';
RETURN EmpName1;
END //
DELIMITER ;
Then after that is created, use the function:
SELECT test();
Example interaction:
root#localhost(test) DELIMITER //
-> CREATE FUNCTION test() RETURNS VARCHAR(50)
-> BEGIN
-> DECLARE EmpName1 VARCHAR(50);
-> SET EmpName1 = 'Ali';
-> RETURN EmpName1;
-> END //
Query OK, 0 rows affected (0.07 sec)
root#localhost(test)
root#localhost(test) DELIMITER ;
root#localhost(test) select test();
+--------+
| test() |
+--------+
| Ali |
+--------+
1 row in set (0.09 sec)
Though the website does not use DELIMITER you can also see this in action at this DB fiddle.
I have a TERADATA dataset that resembles the below :
'Project: Hercules IssueType: Improvement Components: core AffectsVersions: 2.4.1 Priority: Minor Time: 15:25:23 04/06/2020'
I want to extract tag value from the above based on the key.
Ex:
with comm as
(
select 'Project: Hercules IssueType: Improvement Components: core AffectsVersions: 2.4.1 Priority: Minor' as text
)
select regexp_substr(comm.text,'[^: ]+',1,4)
from comm where regexp_substr(comm.text,'[^: ]+',1,3) = 'IssueType';
Is there a way to query without having to change the position arguments for every tag.
Also I am finding the last field a little tricky with date & time fields.
Any help is appreciated.
Thank you.
There's the NVP function to access Name/Value-pair data, but to split into multiple rows you need either strtok_split_to_table or regexp_split_to_table. The tricky part in your case are the delimiters, would be easier if they were unique instead of ' 'and ':':
WITH comm AS
(
SELECT 1 as keycol, -- should be a key column in your table, either numeric or varchar
'Project: Hercules IssueType: Improvement Components: core AffectsVersions: 2.4.1 Priority: Minor Time: 15:25:23 04/06/2020' AS text
)
SELECT id, tokennum, token,
-- get the key
StrTok(token,':', 1) AS "Key",
-- get the value (can't use StrTok because of ':' delimiter)
Substring(token From Position(': ' IN token)+2) AS "Value"
FROM TABLE
( RegExp_Split_To_Table(comm.keycol
,comm.text
,'( )(?=[^ ]+: )' -- assuming names don't contain spaces: split at the last space before ': '
, 'c')
RETURNS (id INT , tokennum INTEGER, token VARCHAR(1000) CHARACTER SET Latin)) AS dt
Is there an ESQL function to convert TIMESTAMP to UTC milliseconds?
This is what I did:
DECLARE eventTimeInteger INTEGER CAST ((eventTimeStamp - epochTimeStamp) SECOND as INTEGER);
But I keep getting this error. So I suspect the "-" is an issue but I'm not sure how else to go about this.
BIP2420E: (.Event_SeparateMessages.Main, 142.60) : Invalid or incompatible data types for '-' operator.
Either the data types of the operands of the operator were not valid for the operator, or the datatypes were incompatible.
Correct the syntax of your ESQL expression in node '.Event_SeparateMessages.Main', around line and column '142.60', then redeploy the message flow: Ensure that the data types of the operands are valid and compatible with each other.
This below is what I've tried but it won't even deploy.
--Converting time in string to timestamp
DECLARE source CHARACTER eventTime ;
DECLARE eventTimeStamp CHARACTER;
DECLARE pattern CHARACTER 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z';
SET eventTimeStamp = CAST(source AS TIMESTAMP FORMAT pattern);
DECLARE epochTimeStamp TIMESTAMP '1970-01-01 00:00:00';
--Casting time from timestamp to Integer
DECLARE eventTimeInteger INTEGER CAST ((eventTimeStamp - epochTimeStamp) SECOND as INTEGER);
I need to have "eventTimeInteger" give me the timestamp in seconds.
The problem, if I'm reading your code right, is that you are trying to subtract a TIMESTAMP from a CHARACTER set.
edit: Noticed the change SET for eventTimeStamp, however the date math is still going to given an INTERVAL output, not an INTEGER
DECLARE EpocTimeStamp TIMESTAMP;
DECLARE eventTimeStamp INTERVAL;
SET EpocTimeStamp = TIMESTAMP '1970-01-01 00:00:00';
SET eventTimeStamp = (CURRENT_TIMESTAMP - EpocTimeStamp) SECOND * 1000;
CAST(eventTimeStamp AS INTEGER);
how do I code this properly to work in Oracle SQL :
update table_name
set field_name =
replace(field_name, x'BF', x'00')
where condition expression ;
Not sure how to code the replace all occurrence of hex 'BF' with null value hex'00' contained in data field field_name.
You can use the unistr() function to provide a Unicode character. e.g.:
update table_name
set field_name = replace(field_name, unistr('\00bf'))
where condition expression ;
which would remove the ¿ character completely; or to replace it with a null character:
set field_name = replace(field_name, unistr('\00bf'), unistr('\0000'))
though I suspect sticking a null in there will confuse things even more later, when some other system tries to read that text and stops at the null.
Quick demo:
with t (str) as (
select 'A ¿ char' from dual
)
select str,
replace(str, unistr('\00bf')) as removed,
replace(str, unistr('\00bf'), unistr('\0000')) as replaced,
dump(replace(str, unistr('\00bf')), 16) as removed_hex,
dump(replace(str, unistr('\00bf'), unistr('\0000')), 16) as replaced_hex
from t;
STR REMOVED REPLACED REMOVED_HEX REPLACED_HEX
--------- --------- --------- ----------------------------------- -----------------------------------
A ¿ char A char A char Typ=1 Len=7: 41,20,20,63,68,61,72 Typ=1 Len=8: 41,20,0,20,63,68,61,72
(Just as an example of the problems you'll have - because of the null I couldn't copy and paste that from SQL Developer, and had to switch to SQL*Plus...)
The first dump shows the two spaces (hex 20) next to each other; the second shows a null character between them.
I have this Filenet query:
SELECT
[This], [Ente], [IDAtto], [Numero], [Tipologia], [DataEmissione]
FROM
[AttoNormativo]
WHERE
([DataEmissione] > 20160405T215959Z AND [DataEmissione] < 20160408T220001Z)
ORDER BY
[DataEmissione] desc, [Tipologia], [Numero], [Ente]
OPTIONS (TIMELIMIT 180)
The problem is that [Numero] property is string type, so it does not order properly. There is some cast function that I can use to convert it numeric?
Thank you very much.
No, there is not. According to the docs the orderby is a property_spec followed optionally by ASC or DESC.
<orderby> ::= <property_spec> [ ASC | DESC ]
The only function allowed in the ORDER BY is COALESCE() which can be used to provide a default sorting value when the data is null.
As per the documentation, properties of type Boolean, DateTime, Float64, ID, Integer32, and Object may appear in an ORDER BY clause, along with short String properties. Neither Binary nor long String properties may be used to order a query.
You can define a custom string property to store in either a short or long database column by setting the UsesLongColumn property when the property is created.
Now - if you are worried about the null values, then you may consider using the COALESCE function.
<orderby> ::= [ COALESCE '(' <property_spec>, <literal> ')' || <property_spec> ] [ ASC | DESC ]
You can find more about Relational Queries - here.