I have some JSON-objects and I need to fetch property values for them.
My situation is based on this query:
DO $$
DECLARE
v_whatever character varying := 'a';
v_res character varying;
BEGIN
SELECT params->>v_whatever FROM user_info1 WHERE uid = 9 INTO v_res;
RAISE NOTICE 'v_res: %', v_res ;
END; $$
In table, value for column params(type json) is: {"a":"b","b":"c","c":"d"} ,
Query returns v_res: b
Because I don't have specific property names, code looks like this:
DECLARE
v_temp char varying;
v_obj char varying[];
comp char varying[];
BEGIN
SELECT json_object_keys(params) FROM user_info1 WHERE uid = p_uid into v_temp;
SELECT params->>v_temp FROM user_info1 WHERE uid = 9 INTO v_obj;
A basic plpgsql FOR loop:
DO
$$
DECLARE
v_whatever text := 'a';
v_res text;
BEGIN
FOR v_res IN
SELECT params->>v_whatever
FROM user_info1
WHERE uid = 9
LOOP
RAISE NOTICE 'v_res: %', v_res;
END LOOP;
END;
$$
Note that a set-based approach is often more efficient than looping. Look to unnest() ..
Related
I have a field text, In it there is information about such
sch hcbhsc hscbshcbc xxxxxxxx sgxfag jdhajdh;
dchbdbc bdcbdh bchdbd xx/xx-xxxx/xx svdhs sbjbsc
bdchbdc jncjdnc jbcjb xx/xx-xxxxx/xx gcvsgc jcbjsb
dchjbd bhjcbdcb bdcbcd xx-xxxx/xx shchscv hscbhsc
dhcbhd jdcbjdb jdcnjdcn xx-xxxxx/xx shcvsch jbscjc
Place x is only a digit, I need to write select and only those numbers are taken
Use SUBSTRING and PATINDEX string functions IN SQL server :
SELECT SUBSTRING(Your_FieldName, PATINDEX('%[0-9]%', Your_FieldName),
LEN(Your_FieldName))
For MYSQL refer below URL :
Query to get only numbers from a string
string
There is no formal PATINDEX() function in MySQL that achieves both the regex pattern lookup with returned character index, define User-Defined function that loops through each character in the length of a string and checks a REGEXP pattern on the character. Once created, use such a function in-line of a query.
DROP FUNCTION IF EXISTS PatIndex;
DELIMITER $$
CREATE FUNCTION PatIndex(pattern VARCHAR(255), tblString VARCHAR(255)) RETURNS INTEGER
DETERMINISTIC
BEGIN
DECLARE i INTEGER;
SET i = 1;
myloop: WHILE (i <= LENGTH(tblString)) DO
IF SUBSTRING(tblString, i, 1) REGEXP pattern THEN
RETURN(i);
LEAVE myloop;
END IF;
SET i = i + 1;
END WHILE;
RETURN(0);
END
Here is a MySQL function (routine) that will do just that. It is an improved version from the solution given here: how-to-get-only-digits-from-string-in-mysql
This improved version can handle much larger numbers. The old solution was limited by the INTEGER value, so if you had phone numbers for example (or string containing many digits), it would fail with out of range for column.
DELIMITER $$
CREATE FUNCTION ExtractNumber (in_string VARCHAR(50))
RETURNS varchar(50)
NO SQL
BEGIN
DECLARE ctrNumber VARCHAR(50);
DECLARE finNumber VARCHAR(50) DEFAULT '';
DECLARE sChar VARCHAR(1);
DECLARE inti VARCHAR(50) DEFAULT 1;
IF LENGTH(in_string) > 0 THEN
WHILE(inti <= LENGTH(in_string)) DO
SET sChar = SUBSTRING(in_string, inti, 1);
SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9');
IF ctrNumber > 0 THEN
SET finNumber = CONCAT(finNumber, sChar);
END IF;
SET inti = inti + 1;
END WHILE;
RETURN CAST(finNumber AS UNSIGNED);
ELSE
RETURN 0;
END IF;
END$$
DELIMITER ;
Now you can do this:
SELECT ExtractNumber(my_field)
FROM my_table;
I use the example below :
select '10+20+30'
and generate value :
'++'
In other words, strip any digit and leave only signs.
Function:
DROP FUNCTION IF EXISTS getMOps;
DELIMITER $$
CREATE FUNCTION getMOps
( s varchar(100)
)
RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
DECLARE sOut VARCHAR(100);
DECLARE theLen,iLooper INT;
SET iLooper=1;
SET theLen=LENGTH(s);
SET sOut='';
WHILE iLooper<theLen DO
IF SUBSTR(s,iLooper,1) IN ('+','-','*','/','^') THEN
SET sOut=CONCAT(sOut,SUBSTR(s,iLooper,1));
END IF;
SET iLooper=iLooper+1;
END WHILE;
return (sOut);
END$$
DELIMITER ;
Test:
select getMOps('fish'); -- blank string
select getMOps('1+2+7-1'); -- '++-'
Modify the IN clause to suit your tastes. I am sure there are better ways.
CREATE FUNCTION FC_IDKRITERIA()
RETURNS CHAR(3)
AS
BEGIN
DECLARE #MAX INT , #KODEBARU CHAR(3)
SELECT #MAX = MAX (RIGHT(IDKRITERIA,2)) FROM KRITERIA
IF #MAX IS NULL
SET #MAX = 0
SET #KODEBARU = 'K' + RIGHT('0'+CONVERT(VARCHAR(3),#MAX+ 1 ) ,2)
RETURN #KODEBARU
END
Every statement in a procedure must end with ;. To keep this from ending the function definition, use the DELIMITER command to change the command delimiter to something else.
And when doing a variable assignment in a SELECT clause, you have to use :=.
There's no AS at the beginning of a function definition.
You don't declare variables that begin with #.
You need THEN and END IF in an IF statement.
To concatenate strings, use CONCAT(), not +.
You have the arguments to CONVERT() in the wrong order, and VARCHAR(3) is not a valid type argument, it should be CHAR(3).
In a function, you can't use a SELECT statement at the top-level, because that means to return the result set, and functions can only return single values. So you have to assign #MAX from a (SELECT ...) expression.
DELIMITER $$
CREATE FUNCTION FC_IDKRITERIA() RETURNS CHAR(3)
BEGIN
SET #MAX = (SELECT MAX (RIGHT(IDKRITERIA,2)) FROM KRITERIA);
IF #MAX IS NULL
THEN SET #MAX = 0;
END IF;
SET #KODEBARU = CONCAT('K', RIGHT('0'+CONVERT(#MAX+ 1, CHAR(3)) ,2));
RETURN #KODEBARU;
END;
$$
DELIMITER ;
Is there a way to replace multiple substrings in a single mysql column field?
In my result, there are fields like:
'&DY, &Q3'
In this case, DY and Q3 are match codes which I have to replace with a defined Word.
I tried to work with this regex_replace function
CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000)) RETURNS varchar(1000)
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;
SET i = 1;
SET temp = '';
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,1);
IF NOT ch REGEXP pattern THEN
SET temp = CONCAT(temp,ch);
ELSE
SET temp = CONCAT(temp,replacement);
END IF;
SET i=i+1;
END LOOP;
ELSE
SET temp = original;
END IF;
RETURN temp;
END
My SQL Query:
SELECT REGEX_REPLACE(fm.column,'(\&[\w]{2})*','My Word') FROM `table` fm WHERE id = '123'
didn't work. Maybe one problem is, that my substrings begin with "&" which is an operator in regex!?
try this, but i have use MariaDB they have REGEX_REPLACE internal
https://mariadb.com/kb/en/mariadb/regexp_replace/
SELECT REGEXP_REPLACE('&DY, &Q3','\(\&[A-Z0-9]+\),*','My Word');
Result:
My Word My Word
I have this:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name = 'whatever'
but what I need is something like this:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_data = 'whatever'
So, in words, I have a value and I have no idea where it's stored. Is there a way to literally check the entire database and return the table, column?
aaaand yes, I know, db admins wouldn't be happy!
This might get you going in the right direction.
1. Create find_column stored procedure
DROP PROCEDURE IF EXISTS `find_column`;
DELIMITER $$
CREATE PROCEDURE `find_column`(IN i_value varchar(200),
OUT o_columns varchar(2000),
OUT o_message varchar(500))
MAIN_BLOCK : BEGIN
DECLARE is_numeric boolean;
CHECK_NUMERIC : BEGIN
set is_numeric = i_value REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
END CHECK_NUMERIC;
FIND_IT : BEGIN
DECLARE bNoMoreRows BOOLEAN DEFAULT FALSE;
DECLARE v_schema varchar(64);
DECLARE v_table varchar(64);
DECLARE v_column varchar(64);
DECLARE v_data_type varchar(64);
DECLARE v_count int;
-- all schemas, tables and columns in DB
DECLARE columns CURSOR FOR
select table_schema,table_name,column_name,data_type from information_schema.columns;
DECLARE EXIT HANDLER for SQLEXCEPTION set o_message := concat('Unexpected error while trying to find schema, table and column for value : ',i_value);
declare continue handler for not found set bNoMoreRows := true;
open columns;
set o_columns = "";
COLUMN_LOOP: loop
fetch columns
into v_schema,v_table,v_column,v_data_type;
if (
(v_data_type in ('int','bigint','tinyint','decimal','smallint','mediumint') and is_numeric=1)
or (v_data_type not in ('int','bigint','tinyint','decimal','smallint','mediumint') and is_numeric=0)
)
then
SET #dyn_sql=CONCAT('select count(*) into #c from `',v_schema,'`.`',v_table,'` where `',v_column,'`=?');
SET #c = 0;
SET #v_value = i_value;
PREPARE stmt FROM #dyn_sql;
EXECUTE stmt using #v_value;
DEALLOCATE PREPARE stmt;
SET v_count = #c;
if v_count > 0 then
if length(o_columns <= 1800) then
set o_columns = concat(o_columns,",",v_schema,".",v_table,".",v_column);
end if;
end if;
end if;
if bNoMoreRows then
set o_columns = substring(o_columns,2);
close columns;
leave COLUMN_LOOP;
end if;
END loop COLUMN_LOOP;
END FIND_IT;
END MAIN_BLOCK$$
DELIMITER ;
2. Call find_column stored procedure with your value
call `find_column`('whatever',#columns,#message);
3. Check out the results
select #columns;
The is_numeric bit is lovingly ripped-off JBB's answer from this post.
It ain't perfect (what happens if the number of columns that your value exists exceeds 10 or so? If that is the case then this will only return the first 10 or so columns (depends on how long the schema.table.column name string is).
Hopefully it'll get you going in the correct direction.
An you're right. You're DB admins will be unhappy with you. But if you don't annoy them once in a while then you're not trying hard enough IMHO ;-)
Good luck.