Changing the fractions in sql - mysql

I have fractions as string in my database and it is currently like this:
3/8
I want to change to this:
<sup>3</sup>⁄<sub>8</sub>
I have many fractions like this. How do I change them at one shot in SQL? I know I need to use Regular Expressions but not sure how to use it.
What I have tried so far:
UPDATE question_table
SET `option` = Replace(`option`, ?? ,??)
WHERE `option` LIKE '%/%'
Not sure what to fill up in ??.

SELECT * FROM strings;
+--------+
| string |
+--------+
| 19/32 |
| 3/8 |
| 5/16 |
+--------+
SELECT *
, CONCAT('<sup>'
, SUBSTRING_INDEX(string,'/',1)
, '</sup>⁄<sub>'
, SUBSTRING_INDEX(string,'/',-1)
,'</sub>'
) x
FROM strings;
+--------+-----------------------------------+
| string | x |
+--------+-----------------------------------+
| 19/32 | <sup>19</sup>⁄<sub>32</sub> |
| 3/8 | <sup>3</sup>⁄<sub>8</sub> |
| 5/16 | <sup>5</sup>⁄<sub>16</sub> |
+--------+-----------------------------------+
UPDATE strings
SET string = CONCAT('<sup>'
, SUBSTRING_INDEX(string,'/',1)
, '</sup>⁄<sub>'
, SUBSTRING_INDEX(string,'/',-1)
, '</sub>'
);
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
SELECT * FROM strings;
+-----------------------------------+
| string |
+-----------------------------------+
| <sup>19</sup>⁄<sub>32</sub> |
| <sup>3</sup>⁄<sub>8</sub> |
| <sup>5</sup>⁄<sub>16</sub> |
+-----------------------------------+

Related

How to remove the last occurring comma, the comma itself may not be the last character from a String in SQL

The table structure is like this :-
mysql> select * from formjson;
+----+---------------------------------------------------------+
| id | jsonData |
+----+---------------------------------------------------------+
| 1 | {"721005":"NO","720931":"1156","720940":"aegiseng",} |
| 2 | {"721005":"NO","720931":"1156","720940":"aegiseng",} |
| 3 | {"721005":"NO","720931":"50253","720940":"d1-gateway",} |
| 4 | {"721005":"NO","720931":"11102","720940":"uxinfra",} |
| 5 | {"720931":"1152","720940":"zappops-notify",} |
+----+---------------------------------------------------------+
5 rows in set (0.00 sec)
I want to remove the last occurring comma from the jsonData i.e ',}' to '}'
I tried
mysql> update formjson set jsonData=CONCAT(TRIM(TRAILING ',}' FROM jsonData),'}') where jsonData like '%,}';
Above is working.
But is there any other way to do this in MySQL, using regexp user defined functions etc?
Please try:
UPDATE formjson SET jsonData=REPLACE(jsonData, ',}', '}') WHERE jsonData LIKE '%,}'

How to use regex in mysql query to remove specific characters?

In my query I am using REPLACE( b.DESCRIPTION,'SP. Z O.O.','') AS DESCRIPTION to remove SP. Z O.O. these characters from columns. And hopefully it's working for me. But in my database SP. Z O.O. this characters are stored in different ways. Like sp. Z.o.o, SP. z.o.o etc.
Somewhere it's stored in capital letters and somewhere it's stored in small letters.
REPLACE( b.DESCRIPTION,'SP. Z O.O.','') AS DESCRIPTION by this method I am only able to remove capital letters. I want all conditions to remove similar words like this.
How to apply regex or case in this situation?
This is my query:
SELECT b.TRANS_DETAILS_ID, b.CREDIT_AMOUNT, b.ENTITY_NAME, REPLACE( b.DESCRIPTION,'SP. Z O.O.','') AS DESCRIPTION, DATE_FORMAT(a.TRANSACTION_DATE_TIME,'%d-%m-%Y') AS TRANS_DATE FROM bank_book_transaction_master a, bank_book_transaction_details b WHERE a.TRANSACTION_DATE_TIME BETWEEN '2017-12-01' AND '2017-12-26' AND DEBIT_CREDIT_FLAG = 1 AND a.ORG_ID = '53' AND a.BANK_ID = '14' AND a.TRANSACTION_ID = b.TRANS_MASTER_ID
You can do it with a query like this:
UPDATE strtest
SET mystring = CONCAT(
LEFT (mystring, POSITION('SP. Z O.O.' IN mystring)-1),
RIGHT(mystring, LENGTH(mystring)-POSITION('SP. Z O.O.' IN mystring)-9)
)
WHERE mystring LIKE '%SP. Z O.O.%';
Sample:
Test Table
MariaDB [bernd]> SELECT * from strtest;
+----+------------------+
| id | mystring |
+----+------------------+
| 1 | SP. Z O.O. |
| 2 | ABCSP. Z O.O. |
| 3 | SP. Z O.O.XYZ |
| 4 | QWESP. Z O.O.IOP |
| 5 | AAASp. Z o.O.LLL |
+----+------------------+
5 rows in set (0.00 sec)
Remove the String in a SELECT ( The >>><<< are only for test)
MariaDB [bernd]> SELECT CONCAT( '>>>',
-> LEFT (mystring, POSITION('SP. Z O.O.' IN mystring)-1),
-> RIGHT(mystring, LENGTH(mystring)-POSITION('SP. Z O.O.' IN mystring)-9),
-> '<<<') AS resultstring
-> FROM strtest;
+--------------+
| resultstring |
+--------------+
| >>><<< |
| >>>ABC<<< |
| >>>XYZ<<< |
| >>>QWEIOP<<< |
| >>>AAALLL<<< |
+--------------+
5 rows in set (0.00 sec)
To UPDATE the Table
MariaDB [bernd]> UPDATE strtest
-> SET mystring = CONCAT(
-> LEFT (mystring, POSITION('SP. Z O.O.' IN mystring)-1),
-> RIGHT(mystring, LENGTH(mystring)-POSITION('SP. Z O.O.' IN mystring)-9)
-> )
-> WHERE mystring LIKE '%SP. Z O.O.%';
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
MariaDB [bernd]> SELECT * from strtest;
+----+----------+
| id | mystring |
+----+----------+
| 1 | |
| 2 | ABC |
| 3 | XYZ |
| 4 | QWEIOP |
| 5 | AAALLL |
+----+----------+
5 rows in set (0.00 sec)
MariaDB [bernd]>
If you are using MariaDB, you could use REGEXP_REPLACE() like the next line:
REGEXP_REPLACE(col, regexp, replace)
Here you will find examples about the usage.

Get count of zeros in an integer using MySQL

Let's say I have an integer value in MySQL (10090). I need to count all occurrences of the zero digit in that number. So for the previous case it would return 3:
select count_zeros(number) from dual;
-- when number = 10090, it return 3
-- when number = 10000, it return 4
How can I do that the fastest way using a MySQL query?
You can compare the string length with and without the character you want to count.
Solution using LENGTH
-- 0 in 10090: 3
-- 0 in 10000: 4
SELECT
(LENGTH(number) - LENGTH(REPLACE(number, '0', ''))) AS char_count
FROM dual;
A better and safer solution is to use the CHAR_LENGTH function instead of the LENGTH function. With CHAR_LENGTH function you can also count multi-byte characters (like §).
Solution using CHAR_LENGTH
-- § in 100§0: 1
SELECT
(CHAR_LENGTH(number) - CHAR_LENGTH(REPLACE(number, '§', ''))) AS char_count
FROM dual;
You can also extend the above solution to count for a string value using multiple characters.
-- 12 in 10120012: 2
SELECT number,
FLOOR((CHAR_LENGTH(number) - CHAR_LENGTH(REPLACE(number, '12', ''))) / CHAR_LENGTH('12')) AS str_count
FROM dual;
demo on dbfiddle.uk
On MySQL you can create a function to use the above logic on a simpler way:
CREATE FUNCTION GetStringCount(strValue VARCHAR(255), strSearchValue VARCHAR(255))
RETURNS INT DETERMINISTIC NO SQL
RETURN FLOOR((CHAR_LENGTH(strValue) - CHAR_LENGTH(REPLACE(strValue, strSearchValue, ''))) / CHAR_LENGTH(strSearchValue));
You can use this new function GetStringCount like this:
-- example to count non-multi-byte character (here 0).
-- 0 in 10090: 3
-- 0 in 10000: 4
SELECT number, GetStringCount(number, '0') AS strCount
FROM dual;
-- example to count multi-byte character (here §).
-- § in 100§0: 1
SELECT number, GetStringCount(number, '§') AS strCount
FROM dual;
-- example to count a string with multiple characters.
-- 12 in 10120012: 2
SELECT number, GetStringCount(number, '12') AS strCount
FROM dual;
I think the first thing to be done is, casting those integer values to string.
https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast
Then find occurences of a certain char
https://lists.mysql.com/mysql/215049
mysql> create table numbers(x int);
Query OK, 0 rows affected (0,38 sec)
mysql> select * from numbers;
+-----------+
| x |
+-----------+
| 123000 |
| 1300 |
| 135600 |
| 135623400 |
| 13560 |
| 135160 |
| 13514560 |
| 1351120 |
| 13512310 |
+-----------+
9 rows in set (0,00 sec)
Find occurences of zero
mysql> select x, round((length(cast(x as char(11))) - length( replace( cast( x as char(11) ), "0", "" ) ))/length("0")) as str_x from numbers limit 5;
+-----------+-------+
| x | str_x |
+-----------+-------+
| 123000 | 3 |
| 1300 | 2 |
| 135600 | 2 |
| 135623400 | 2 |
| 13560 | 1 |
+-----------+-------+
5 rows in set (0,00 sec)
Find thirteens
mysql> select x, round((length(cast(x as char(11))) - length( replace( cast( x as char(11) ), "13", "" ) ))/length("13")) as str_x from numbers;
+-----------+-------+
| x | str_x |
+-----------+-------+
| 123000 | 0 |
| 1300 | 1 |
| 135600 | 1 |
| 135623400 | 1 |
| 13560 | 1 |
| 135160 | 1 |
| 13514560 | 1 |
| 1351120 | 1 |
| 13512310 | 1 |
| 132134534 | 2 |
+-----------+-------+
10 rows in set (0,00 sec)
mysql>

How to select row which a certain text format?

Sorry for an unclear question coz I don't know how to ask correctly. Let me explain this. I'd like to search for some rows in mySQL. Which is only in this format - "product1" (text and number). While in my table. There're some other rows begins with "product" as well. But they're not followed by number.
Here's my table.
**product_db**
+----------+-------+
| key | value |
+----------+-------+
| product1 | 100 |
+----------+-------+
| product2 | 184 |
+----------+-------+
| product3 | 170 |
+----------+-------+
| productA | 210 |
+----------+-------+
| productB | 100 |
+----------+-------+
This is mySQL:
select * from product_db where key like 'product%'
After run the code. Every row shows up. Because they're all begins with "product". I expect only the first 3 rows that followed by number to be displayed. How can I write a command in mySQL.
you should use regex:
select * from product_db where key REGEXP '^product[0-9]+$';
SELECT 'product1' REGEXP '[product][0-9]';
+------------------------------------+
| 'product1' REGEXP '[product][0-9]' |
+------------------------------------+
| 1 |
+------------------------------------+
1 row in set (0.01 sec)
SELECT 'productA' REGEXP '[product][0-9]';
+------------------------------------+
| 'productA' REGEXP '[product][0-9]' |
+------------------------------------+
| 0 |
+------------------------------------+
1 row in set (0.00 sec)

How do I use MySQL 5.1's RLIKE with a regular expression containing a backslash?

I need select rows with the word "foo" (i.e. str =~ /\b(foo)\b/). This doesn't seem to work in MySQL 5.1:
mysql> SELECT * FROM foo WHERE keyword RLIKE '\bbar\b' LIMIT 10;
Empty set (0.00 sec)
mysql> SELECT * FROM foo WHERE keyword RLIKE 'bar' LIMIT 10;
+------+-----------------------+
| id | keyword |
+------+-----------------------+
| 1220 | foo bar |
| 1221 | foo |
| 1222 | foobar |
| 1223 | afoo bar |
| 1224 | foo barf |
| 1225 | foo bar baz |
| 1226 | food bar |
| 1227 | fool bar |
| 1228 | football |
| 1229 | the game is afoot |
+------+-----------------------+
10 rows in set (0.00 sec)
I've tried SELECT * FROM foo WHERE keyword RLIKE '\\bbar\\b' LIMIT 10 as well.
Any ideas?
Mysql does not support the \b regexp code. However, they have another way to do it.
Try this instead:
SELECT * FROM foo WHERE keyword RLIKE '[[:<:]]bar[[:>:]]' LIMIT 10
try using REGEXP it's really nice and better than rlike by a mile
In accordance with http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html, it states that all backslashes need to be done twice. eg
\\bfoo\\b
should work
You can use REGEXP http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp