I have this string 1111-2222-3-4-55-12345678901234567 1 in MYSQL Table Field.
What I need to do is to separate first 5 parameters separated with a -. Like I need to Separate:
1111
2222
3
4
55
$stringElements = explode('-', $string);
echo $stringElements[0];// 1111
echo $stringElements[1];// 2222
echo $stringElements[2];// 3
echo $stringElements[3];// 4
echo $stringElements[4];// 55
$stringElements[5];// 12345678901234567 1
Even though I did not test it, this should solve your problem.
SELECT `mystring`,
SUBSTRING_INDEX(mystring,'-',1) AS part1,
SUBSTRING_INDEX(SUBSTRING_INDEX(mystring,'-',2),'-',-1) AS part2,
SUBSTRING_INDEX(SUBSTRING_INDEX(mystring,'-',3),'-',-1) AS part3,
SUBSTRING_INDEX(SUBSTRING_INDEX(mystring,'-',4),'-',-1) AS part4,
SUBSTRING_INDEX(SUBSTRING_INDEX(mystring,'-',5),'-',-1) AS part5,
FROM my_table;
------------------------------------------------------------------------------
| mystring | part1 | part2 | part3 | part4 | part5 |
------------------------------------------------------------------------------
| 1111-2222-3-4-55-12345678901234567 | 1111 | 2222 | 3 | 4 | 55 |
This is splitting the text with an increased index count and re-splitting it from the last index for each part through part2 to part5
If the length of each section within the string is constant, then
select replace(left(fieldname,16),'-','') as alias from table
will do the trick.
UPDATE:
If you need to have each individual value in a separate field, then it's a slightly more complicated:
SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(fieldname, '-', 1), LENGTH(SUBSTRING_INDEX(fieldname,'-', 0)) + 1) as first,
REPLACE(SUBSTRING(SUBSTRING_INDEX(fieldname, '-', 2), LENGTH(SUBSTRING_INDEX(fieldname,'-', 1)) + 1) as second,
...
FROM table
You can create function:
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
and then use it like
SELECT SPLIT_STR('1-2-3', '-', 1);
it will return 1. With all this examples you can easily build your own query
Source: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX('1111-2222-3-4-55-12345678901234567','-',i+1),'-',-1) x FROM ints;
+-------------------+
| x |
+-------------------+
| 1111 |
| 2222 |
| 3 |
| 4 |
| 55 |
| 12345678901234567 |
+-------------------+
Related
I have sample Data
+----+-----------+
| Id | Name |
+----+-----------+
| 1 | $John |
| 2 | $Carol |
| 3 | $Mike |
| 4 | $Sam |
| 5 | $David$Mohan$ |
6 | $David$
7 | $David$Mohan$
| 8 | Robert$Ram$ |
| 9 | Maxwell$ |
+----+-----------+
I need to remove the only $ first character
Need output :
+----+-----------+
| Id | Name |
+----+-----------+
| 1 | John |
| 2 | Carol |
| 3 | Mike |
| 4 | Sam |
| 5 | David$Mohan |
6 | David
7 | David$Mohan
| 8 | Robert$Ram |
| 9 | Maxwell |
+----+-----------+
Select REPLACE(col,'$','') from Tbl
select regexp_replace(name, '^$', '') name from mytable
I have tried with Replace and Substring but still missing the point .
Can anyone suggest me .
If you are only looking for starting $, you can use this below logic-
DEMO HERE
SELECT
CASE
WHEN LEFT(D,1) = '$' THEN RIGHT(D, LENGTH(D)-1)
ELSE D
END STR,
IF(LEFT(D,1) = '$', RIGHT(D, LENGTH(D)-1), D) STR2
-- you can use any of the above option
FROM
(
select '$David$Mohan$' D UNION ALL
select 'Da$Mo$'
)A
Try this:
select
id,
case when SUBSTR(Name, 1,1)='$' and SUBSTR(Name,-1,1)='$' then substr(Name,2,(length(Name)-2))
when SUBSTR(Name, 1,1)='$' then substr(Name,2)
else Name
end
from Tbl
Based on your example you should try;
Replace(trim(replace({col},'$',' ')), ' ','$')
This is turning the '$' into spaces, removing spaces at the start or end or the string, then switching back to '$'.
Try this, it's working for me for all your test cases
SELECT REGEXP_SUBSTR(name,'[^$].+[^$]') from users;
If case you want to replace $ with space, David$Ang => David Ang
SELECT REGEXP_REPLACE(REGEXP_SUBSTR(name,'[^$].+[^$]'), "[$]", " ") from users;
I have a tinytext field which can contain 3 differents value formatted as followed:
NULL
"X" or "Y" (where X and Y can be any number)
"A,B,C,D" (where A, B, C and D can be any number)
I want to query the table and count the number of items separated or not with a comma.
For example with these lines:
42
NULL
42,56,99
24,10090
Then the expected count would be 6.
I can't find the correct query.
Okay here's the test data:
mysql> create table t (f tinytext);
mysql> insert into t values ('42'), (null), ('42,56,99'), ('24,10090');
mysql> select * from t;
+----------+
| f |
+----------+
| 42 |
| NULL |
| 42,56,99 |
| 24,10090 |
+----------+
You can calculate how many numbers in the string as the difference in the length of the string and the string with commas removed (add 1 for the first number in the list).
mysql> select f, length(f), length(replace(f,',','')), 1+ length(f)-length(replace(f,',','')) from t;
+----------+-----------+---------------------------+----------------------------------------+
| f | length(f) | length(replace(f,',','')) | 1+ length(f)-length(replace(f,',','')) |
+----------+-----------+---------------------------+----------------------------------------+
| 42 | 2 | 2 | 1 |
| NULL | NULL | NULL | NULL |
| 42,56,99 | 8 | 6 | 3 |
| 24,10090 | 8 | 7 | 2 |
+----------+-----------+---------------------------+----------------------------------------+
So then use SUM() to get the total. SUM() ignores NULLs.
mysql> select sum(1+length(f)-length(replace(f,',',''))) from t;
+--------------------------------------------+
| sum(1+length(f)-length(replace(f,',',''))) |
+--------------------------------------------+
| 6 |
+--------------------------------------------+
This would be easier if you don't store comma-separated lists in a string.
I have this table
| BookID | BookTitle | NumberOfPages | NoOfCopies |
+--------+--------------------------------+---------------+------------+
| 1 | The Help | 444 | 4 |
| 2 | The Catcher in the Rye | 277 | 10 |
| 3 | Crime and Punishment | 545 | 2 |
| 4 | The Brothers Karamazov | 795 | 1 |
| 5 | A Crown of Wishes | 369 | 12 |
| 6 | The Fireman | 752 | 3 |
| 7 | Fahrenheit 451 | 174 | 9 |
| 8 | The Hobbit | 366 | 1 |
| 9 | Lord of Emperors | 560 | 4 |
| 10 | Holy Bible: King James Version | 1590 | 11 |
----------------------------------------------------------------------------
When I insert a book title and expect it to return the book id, it always returns an empty set
so far, I have tried these queries.->book_info is the name of the table:
select BookID from book_info where ucase(BookTitle) = ' THE HELP% ';
select BookID from book_info where BookTitle = ' The Help ';
select BookID from book_info where lcase(trim(BookTitle) = 'the help';
but none of them worked.
Note I don't rely on sql in my job.
you need to use like if you want to use "%"
when you use "=" you need to sure it is same. even space also count
select BookID from book_info where BookTitle LIKE 'THE HELP%';
The issue here is with the operator you are using and the value you are function you are expecting from it, = operator checks for the exact match that's why your queries are returning no records:
select BookID from book_info where ucase(BookTitle) = ' THE HELP% ';
select BookID from book_info where BookTitle = ' The Help ';
select BookID from book_info where lcase(trim(BookTitle) = 'the help';
And one more thing that is:
MySQL queries are not case-sensitive by default.
So you don't need to add the string methods here to change the values case.
We usually use the % with LIKE only like this:
select BookID from book_info where ucase(BookTitle) LIKE '%THE HELP%';
In this query LIKE %THE HELP% will match all the string having THE HELP in them;
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>
I have got the columns [id, account_id] in my table. And the values are
+------+----------------+
| id | account_id |
+------+----------------+
| 1 | 01-01-02-0007 |
| 2 | 04-05-06-0001 |
| 3 | 03-07-09-0001 |
| 4 | 03-04-04-0001 |
| 5 | 03-04-08-0101 |
| ... |
| 201 | 03-04-08-0111 |
+------+----------------+
What I want is replace the last part of the each of account_id after - i.e. 0007, 0001 and 0001 etc in this case with respective id (but still padded with the 0s to the left to make it 4 characters). To be more specific, below is what I want to achieve:
+------+----------------+
| id | account_id |
+------+----------------+
| 1 | 01-01-02-0001 |
| 2 | 04-05-06-0002 |
| 3 | 03-07-09-0003 |
| 4 | 03-04-04-0004 |
| 5 | 03-04-08-0005 |
| .... |
| 201 | 03-04-08-0201 |
+------+----------------+
I thought to use REPLACE but unfortunately, that can't be applied to my case, since it is not just the part ( that remains same for each value) that I want to change. I have been searching, but I am unable to achieve this. I think, I would have to use some regular expression and LPAD in some way to achieve this, but not sure how.
Can anyone please show me some light?
For the very simple case with fixed account number lengths you're showing, this will do;
UPDATE accounts
SET account_id=CONCAT(LEFT(account_id,9), LPAD(id, 4, '0'))
An SQLfiddle to test with.
If account_id format would not be changing, you can use
substring_index, lpad, and concat to apply changes as you wanted.
update my_table
set account_id =
concat( substring_index( account_id, '-', 3 ), '-', lpad( id, 4, '0' ) )