With Mysql - Return the output of AES_ENCRYPT as string "0x8DBADD..." - mysql

In Mysql the function AES_ENCRYPT() returns a hex like:
0x8DBADD32FD0FAB62232104DEB9D56246
How can I return it as string:
"0x8DBADD32FD0FAB62232104DEB9D56246"
so for example I can right() to remove the first two characters: "0x"
I tried CAST but that converts the HEX to string. What I need is the string literal of the Hex

You can use HEX() to make the conversion and CONCAT() to add the prefix:
SELECT CONCAT('0x', HEX(0x8DBADD32FD0FAB62232104DEB9D56246)) AS hex_dump;

Related

How to have a whole string as a normal string in PostgreSQL's regular match?

In my sql code, I receive a input string as a regular match's filter, I want to have the whole string as a normal string, even it includes some special characters.
Just look below:
do $$ declare
jdata jsonb='[{"name":"Dog 3*240+1*120"}]'::jsonb;
vfilter1 text='dog';
vfilter2 text='3*240+1*120';
vexists bool=false;
begin
select jdata #? concat('$[*] ? (#.name like_regex "',vfilter1,'" flag "i")')::jsonpath into vexists;
raise notice 'exists:%',vexists; --the result is true
select jdata #? concat('$[*] ? (#.name like_regex "',vfilter2,'" flag "i")')::jsonpath into vexists;
raise notice 'exists:%',vexists;-- the result is false
end;
$$ language plpgsql;
the string 3*240+1*120 include + and * characters, perhaps this causes the regular match have them as special character. In my code, I just want to have the whole vfilter string includes all special characters together as a normal string for the regular match.
What should I do?
You should read the documentation for the feature you are using.
The optional flag string may include one or more of the characters i
for case-insensitive match, m to allow ^ and $ to match at newlines, s
to allow . to match a newline, and q to quote the whole pattern
(reducing the behavior to a simple substring match).

Use regexp in presto to remove the last slash only when it's preceded by a character

Is there a way in Presto or SQL to remove the last backslash only when it is preceded by a character and keep it otherwise?
I am using regexp_replace in presto. So for example if x = '/' The expression should return '/'
and if x = 'beta/alpha/' it should return 'beta/alpha'
I am using select regexp_replace ([expression], '[\/]$', '').
This returns an empty string when there is only a backslash and removes the backslash from the end of the string if the expression has some characters before the backslash.
You can use
regexp_replace([expression], '([^/])/$', '$1')
-- or
regexp_replace([expression], '(?<=[^/])/$', '')
See the regex demo.
Details
([^/])/$ - matches and captures any char but / into Group 1 (with the ([^/]) pattern, the $1 in the replacement pattern is a replacement backreference that refers to the Group 1 value), then matches a / at the end of string ($)
(?<=[^/])/$ matches a / at the end of a string only when the char immediately on the left is not a / char (and not the start of a string).

Compare two strings using MYSQL

I have two strings in comma separated.
String1='ABC,DEF,PQR,MNO,XYZ'
String2='PQR,FGH'
String3='GHI,JKL,TUV'
Now I want that, compare string1 and string2, it will return true because String2 has value 'PQR' that matches with String1. But If I compare String1 and String3, it will return false because none of the string from string 3 match with String1.
Note : Both Strings I got using Group_concat function.
I got the solution using FIND_IN_SET() function.
Thank you for your efforts.

Argument data type numeric is invalid for argument 1 of substring function

I get the following message with this code
case when substring(New_Limit,11,1)=' ' then '0'+substring(New_Limit,1,10)
The 'then' bit is meant to concat the 0 and substring.
Any help?
This means that your New_Limit variable is a numeric value. You may want to put a CAST to (n)varchar around it.
You try to cast it to a string type (varchar) first:
SUBSTRING(CAST(New_Limit AS varchar(38)), 11, 1)

Regarding Extract Function

i have query related to extract function, will Extract Function accept ASCII Value?
The $EXTRACT function when given a string of ASCII characters, will return a substring of that string.
for example $EXTRACT("Hello World",1,6) is "Hello"
If you want to extract the ASCII value of a character in a string, use the function $ASCII.
If you want to create a string given the ASCII values, use the function $CHAR.
For example $EXTRACT("ABC",2) is "B" and $ASCII("ABC",2) is the number 66
and the function $CHAR(67,65,66) is the string "CAB".