MYSQL add 1 to max value in substring_index - mysql

I'm trying to add 1 to my max field value in mysql query:
SELECT MAX(SUBSTRING_INDEX(0001-14-A,'-',1)) AS prefix;
RESULT:
prefix
------
0001
However when i do like this:
SELECT MAX(SUBSTRING_INDEX(0001-14-A,'-',1)) + 1 AS prefix;
RESULT:
prefix
------
2
How to remain the value format to ****? Please advise.

First, you are missing single quotes:
SELECT MAX(SUBSTRING_INDEX('0001-14-A', '-', 1)) + 1 AS prefix
When you add one to the value, it is converted to an integer. To get a string back, you need to pad it:
SELECT LPAD(MAX(SUBSTRING_INDEX('0001-14-A', '-', 1)) + 1, 4, '0') AS prefix

Related

how to do the addition of double data type value and int data type value in mysql query

here is my query
SELECT max_cet_percentage,agri_wg_total, (max_cet_percentage + agri_wg_total) as final_val
FROM agri_table_name_before_weghtage_reupdate
WHERE ApplicationNo LIKE '%30023'
and it's returning the result like this: 36.840515100000005
expected result: 36.8405151
Use truncate to specify number of digits:
SELECT max_cet_percentage,agri_wg_total, truncate(max_cet_percentage + agri_wg_total, 7) as final_val
FROM agri_table_name_before_weghtage_reupdate
WHERE ApplicationNo LIKE '%30023'
Or you could use round(), ceil() or floor() if you want it to adjust the last digit.
Alternatively change the double column to have less digits after decimal:
Alter table tablename modify doubleColumn decimal(14, 7);
Use Truncate
SELECT max_cet_percentage,agri_wg_total, truncate((max_cet_percentage + agri_wg_total), 7) as final_val
FROM agri_table_name_before_weghtage_reupdate
WHERE ApplicationNo LIKE '%30023'

mysql SUBSTRING() and LOCATE()

I am using mysql's SUBSTRING() function and LOCATE() to capture "n" characters before and after some string.
For example, using the string "apple". When I query it works fine except if the string "apple" is towards the beginning of the string since 10 characters before may be non-existent:
http://sqlfiddle.com/#!9/f41f8d/5
CREATE TABLE demo (name varchar(1000));
INSERT INTO demo (name) VALUES
("An apple a day keeps the doctor away"),
("A doctor a day keeps the apple away from the doctor");
SELECT SUBSTRING(
`name`,
LOCATE("apple",`name`) - 10, /* from 10 characters before 'string'*/
(25) /* to 10 characters after the 5 strlen string (so 10 + 5 + 10) */
)
FROM demo
WHERE name like '%apple%'
Results
| r away |
| keeps the apple away from |
The second results is as expected, but the first - I would like it to start at the beginning of the string until 10 characters after "apple".
What's wrong with my query or how can I fix it? I'm also queries millions of rows so I assume a sub-query to check if it's position is less than "string"'s length isn't performant?
Try with this:
SELECT SUBSTRING(
`name`,
GREATEST(LOCATE("apple",`name`) - 10, 1), /* from 10 characters before 'string'*/
LEAST(25, LENGTH(name) - GREATEST(LOCATE("apple",`name`) - 10, 1)) /* to 10 characters after the 5 strlen string (so 10 + 5 + 10) */
)
FROM demo
WHERE name like '%apple%'
Something like this:
SELECT SUBSTRING(name,
GREATEST(1, LOCATE('apple', name) - 10),
15 + LEAST(LOCATE('apple', name), 10)
)
FROM demo
WHERE name like '%apple%'

MYSQL : Find the last occurrence of a character in a string

Length will be dynamic and i want to find the data before last occurrence of a character in a string in MYSQL
Like strrchr in php
To get last occurrence of _ (underscore) I need to pass length. and here it's 3
mysql> SELECT SUBSTRING_INDEX ('this_is_something_here', '_', 3);
+----------------------------------------------------+
| SUBSTRING_INDEX ('this_is_something_here', '_', 3) |
+----------------------------------------------------+
| this_is_something |
+----------------------------------------------------+
And here, to get last occurrence of _ (underscore) i need to pass length. and here it's 6
mysql> SELECT SUBSTRING_INDEX ('and_this_may_go_like_this_too', '_', 6);
+-----------------------------------------------------------+
| SUBSTRING_INDEX ('and_this_may_go_like_this_too', '_', 6) |
+-----------------------------------------------------------+
| and_this_may_go_like_this |
+-----------------------------------------------------------+
i want data string before last occurrence of _ (underscore) just shown in above example but without passing length.
Note : from above example i want before data of "_here" and "_too"
last occurrence of _ (underscore)
Is there any built-in functionality to achieve this in MySQL?
Thanks in advance amigos.
I didn't quite get your examples, but I think what you want is to pass -1 as the length and prepend the substring prior.
Compare
strrchr('and_this_may_go_like_this_too', '_'); // Returns _too
SELECT SUBSTRING_INDEX('and_this_may_go_like_this_too', '_', -1);
-- Returns too, just need to concatenate `_` so...
SELECT CONCAT('_', SUBSTRING_INDEX('and_this_may_go_like_this_too', '_', -1));
-- Returns _too
If you're looking for the part of the string before and up to the needle, and not from the needle to the end of the string, you can use:
SET #FULL_STRING = 'this_is_something_here';
SELECT LEFT(#FULL_STRING, LENGTH(#FULL_STRING) - LOCATE('_', REVERSE(#FULL_STRING)));
-- Returns this_is_something
Note that the second statement is not what strrchr does.
select reverse(substr(reverse('this_is_something_here'), 1+locate('_', reverse('this_is_something_here'))));
Use reverse, locate, right then replace without using length
Set #str = 'there_is_something';
Select replace(#str,right(#str,locate('_',reverse(#str))),'');
You can write query like this
SELECT SUBSTRING_INDEX('and_this_may_go_like_this_too','_',(LENGTH('and_this_may_go_like_this_too')-LENGTH(REPLACE('and_this_may_go_like_this_too' ,'_',''))) - 1);

sql new column by delimiter with order ID

Hi this maybe a simple one but I need help specifically for MYsql
I have the data in one column lets call the column WORK 1,2,3,5,2 (these values are sometimes longer and shorter or more values are present e.g 12,15,11,15,16,143)
I need these to be put into 1 new column for each delimiter and have an ID for the order presented. e.g output
SELECT
*
FROM (SELECT
ROW_NUMBER()
OVER (ORDER BY WORK) AS Row,
RIGHT(LEFT(T.WORK, Number - 1),
CHARINDEX(',', REVERSE(LEFT(',' + T.WORK, Number - 1)))) AS a
FROM master..spt_values,
<YOUR_TABLENAME> T
WHERE Type = 'P'
AND Number BETWEEN 1 AND LEN(T.WORK) + 1
AND (SUBSTRING(T.WORK, Number, 1) = ','
OR SUBSTRING(T.WORK, Number, 1) = '')) AS A

Mysql - count values from comma-separated field [duplicate]

This question already has answers here:
How to count items in comma separated list MySQL
(6 answers)
Closed last year.
I have to do some statics from read-only db where value are stored in a weird form
example:
I have 2 rows like
ID text field
1 1001,1003,1004
2 1003, 1005
I need to be able to count that this is "5".
I don't have write access so don't know how to read and count right away without creation a function or something like that.
Clever solution here on SO: How to count items in comma separated list MySQL
LENGTH(textfield) - LENGTH(REPLACE(textfield, ',', '')) + 1
EDIT
Yes you can select it as an additional column: and correcting with the CHAR_LENGTH from #HamletHakobyan's answer:
SELECT
ID,
textfield,
(CHAR_LENGTH(textfield) - CHAR_LENGTH(REPLACE(textfield, ',', '')) + 1) as total
FROM table
SELECT SUM(LENGTH(textfield) - LENGTH(REPLACE(textfield, ',', '')) + 1)
FROM tablename
There is a small but significant omission in all answers. All will work only if database character set is utf8 or so, i.e. where symbol , gets one byte. The fact that the LENGTH function returns number of bytes instead of chars. Right answer is to use CHAR_LENGTH which returns number of characters.
SELECT
SUM(CHAR_LENGTH(textfield) - CHAR_LENGTH(REPLACE(textfield, ',', '')) + 1) cnt
FROM yourTable
You could use something like this:
select sum(total) TotalWords
from
(
select length(`text field`) - length(replace(`text field`, ',', '')) + 1 total
from yourtable
) x
See SQL Fiddle with Demo
SELECT (LENGTH(column_name) - LENGTH(REPLACE(column_name, ',', '')) + 1) as value_count
FROM table_name
Here LENGTH(column_name) - LENGTH(REPLACE(column_name, ',', '')) gives the number of commas in the value of each column. And +1 with this value provides the number of values separated by comma.
All is wrong and doesn't works for me.
The only one that work is this bellow
SELECT (length(`textfield`) - length(replace(`textfield`, ',', '')) + 1) as my
FROM yourtable;
This is my fiddle
http://sqlfiddle.com/#!9/d5a8e1/10
If someone looking for a solution to return 0 for empty fields.
IF(LENGTH(column_name) > 0, LENGTH(column_name) - LENGTH(REPLACE(column_name, ',', '')) + 1, 0)