Mysql: extract number from string - mysql

Hello I have a column in the table which is serialized:
I want the last number in the array if there is only one number in that array.
column
a:1:{s:7:"general";s:6:"666423";}
a:1:{s:7:"general";s:5:"36624";}
a:1:{s:7:"general";s:12:"36628, 36624";}
a:1:{s:7:"general";s:5:"36601";}
a:1:{s:7:"general";s:4:"9847";}
a:1:{s:7:"general";s:3:"444";}
a:1:{s:7:"general";s:2:"56";}
a:1:{s:7:"general";s:1:"7";}
Expected output
-
666423
36624
null (i do not want to extract if there is more than one number)
36601
9847
444
56
7
Which string function would be the most efficient in this case?

You can take advantage of the serialized format that always ends the same way and use reverse+locate-function first to get the string and then see if it contains multiple values.
See SQLFiddle.

Related

Store JSON in multiple columns and concatenate them to query them

I have a table that currently has a extended column 32,768 bytes in size and so the database uses that space no matter if we put in 1 byte or all 32,768.
I store json in this column.
I am needing to reduce the size this column is taking.
Can I store the json in multiple columns and then concatenate the columns to work with the complete JSON?
For example
column has data:
'{"REC_TYPE_IND":"1","ID":"999999","2nd ID":"1111","location":"0003","BEGIN_DT":"20000101","END_DT":"20991231"}'
I want to split it out like
column1:
'{"REC_TYPE_IND":"1","ID":"999999","2nd '
column2:
'ID":"1111","location":"0003","BEGIN_DT":"20000101","END_DT":"20991231"}'
The how to I use built in functions like json_value(column1 || column2,'location') to get a value?
The error I get when trying the above is:
ORA-00932: inconsistent datatypes: expected - got CHAR

Preserving decimal values in SSIS

I have a column from my .csv file coming in with values as 1754625.24 etc,. where as we have to save it as integer in our database. So am trying to split number with '.' and divide second part with 1000 (24/1000) as i want 3 digit number.
so i get 0.024. But i am having issues storing/preserving that value as a decimal.
I tried (DT_DECIMAL,3) conversion but i get result as '0'.
My idea is to then append '024' part to original first part. So my final result should look like 1754625024
Please help
I am not convinced why would you store 1754625.24 as 1754625024 when storing it as int.
But still for your case , we can use a derived column task and
use Replace command on the source column of csv. E.g.
Replace('1754625.24','.',0)

How to get intersect value of two comma seperated string in stored procedure?

I need to find intersect value from two comma separated string in stored procedure.
String 1: 40,31,42,23,45 => System generated numbers
String 2: 30,23,24,31,25,32 => User selected numbers
I want to check that, do any of my numbers are present in system generated numbers.
Above value should return 23,31.
I have solution that, i will loop through system generated numbers and will check one by one with my numbers using FIND_IN_SET, if present i will CONCATE that and will get required output.
But is there any shorter and optimized way to do this?

Is there any way to get value from JSON format through mysql query or function/procedure?

Is there any way to get value from JSON format which are stored in database field.
I had tried like below but sometimes it return wrong value and it doesn't seems to correct thing to fetch records.
Actually I am trying to addition of all sizes
SELECT
substring_index(substr(size,locate('"xs":"',size)+char_length('"xs":"')),'"',1)+
substring_index(substr(size,locate('"s":"',size)+char_length('"s":"')),'"',1)+
substring_index(substr(size,locate('"m":"',size)+char_length('"m":"')),'"',1)+
substring_index(substr(size,locate('"l":"',size)+char_length('"l":"')),'"',1)+
substring_index(substr(size,locate('"xl":"',size)+char_length('"xl":"')),'"',1)+
substring_index(substr(size,locate('"xxl":"',size)+char_length('"xxl":"')),'"',1)+ substring_index(substr(size,locate('"xxxl":"',size)+char_length('"xxxl":"')),'"',1)+
substring_index(substr(size,locate('"xs\/s":"',size)+char_length('"xs\/s":"')),'"',1)+ substring_index(substr(size,locate('"m\\/l":"',size)+char_length('"m\\/l":"')),'"',1)+
substring_index(substr(size,locate('"xl\\\/xxl":"',size)+char_length('"xl\\\/xxl":"')),'"',1)
as qty FROM `table_name`
I have also taken the reference to catch numeric value from a string from below question of StackOverflow:
How do you extract a numerical value from a string in a MySQL query?
it returns correct value and i can do addition addition of return value,,but for 2 digit number it is not good for me (like if it return 15 so it is 6 for my case)

Copying double value from a column of a table into another string column with fixed lenth pattern in MySql

I am using MySql and came across a problem.
I have a column name price(Double) and i want to create a new column in the same table of datatype String and wants to copy the price into new column for all rows present in the table
with fixed length pattern.
example i want to do the following
Price(double) newcolumn(String)
12 00012
1 00001
0 00000
please help me to sort out this problem.
Perhaps something like this could work:
UPDATE YourTable SET newcolumn = LPAD(REPLACE(FORMAT(Price,0),',',''),5,'0')
The FORMAT function converts the double into a string with 0 decimals and here is the api.
The FORMAT introduces commas to separate thousands so we need to use REPLACE to get rid of those and here is the api.
The LPAD function left pads with the '0' and here is the api