MySQL insert CASE WHEN/THEN/ELSE - mysql

i', trying to insert table with case statement. But it's not working at all. i'm so confused for this problem. this is my query.
INTO T_Person_Phone(ID_Person, Number_Phone_Person, Piority_Phone_Person)
VALUES ( 255893E0 ,6474748,CASE When 1 = 1 Then 'Default' END)
if i set value 1 then show default. it's that any wrong with my query?
anyone can help me ??

In MySQL, when you want to concatenate strings, CONCAT() is used.
CASE When 1 = 1 Then 'Default' ELSE CONCAT('Piority', Priority) END
'A' + 'B' means, numeric summation. 'A' is converted to 0, 'B' converted to 0' so, 'A' + 'B' = 0.
mysql> select 'A' + 'B';
+-----------+
| 'A' + 'B' |
+-----------+
| 0 |
+-----------+
BTW, where Priority come from?.
priority it's just my decrale then, I think, "'piority' + 2 = 2"
UPDATED
your CASE returns string.
mysql> select CASE When 1 = 1 Then 'Default' END;
+-------------------------------------+
| CASE When 1 = 1 Then 'Default' END |
+-------------------------------------+
| Default |
+-------------------------------------+
and Piority_Phone_Person is TINYINT. tinyint can't store string. I would recommend you to convert Piority_Phone_Person to VARCHAR(255).

Related

How to replace all the digits before hyphen with a new digit using MySQL? [duplicate]

I have a table called myTable which has a column called col1. This column contains data in this format: (1 or 2 digits)(hyphen)(8 digits).
I want to replace all the data in this column and replace everything before hyphen with 4, so this is an example:
--------------------------------
| old values | New Values |
--------------------------------
| 1-654283568 => 4-654283568 |
| 2-467862833 => 4-467862833 |
| 8-478934293 => 4-478934293 |
| 12-573789475 => 4-573789475 |
| 16-574738575 => 4-574738575 |
--------------------------------
I am using MySQL 5.7.19, I believe REGEXP_REPLACE is available in MySQL Version 8+... not sure how this can be achieved?
You don't need regex; you can use SUBSTRING_INDEX to extract everything after the hyphen and concatenate 4- to that:
UPDATE myTable
SET col1 = CONCAT('4-', SUBSTRING_INDEX(col1, '-', -1))
Demo on dbfiddle
This will work regardless of the number of characters after the hyphen.
Looking to your pattern seem you could avoid regexp
update myTable
set col1 = concat('4-', right(col1,8))
or
update myTable
set col1 = concat('4', right(col1,9))
Try this:
UPDATE testing SET val=REPLACE(val,SUBSTRING(val,1,LOCATE('-',val)),'4-');
Fiddle here :https://www.db-fiddle.com/f/4mU5ctLh8NB9iKSKZF9Ue2/2
Using LOCATE to find '-' position then use SUBSTRING to get only the front part of the '-'.
SELECT CONCAT( #new_prefix, SUBSTRING(old_value FROM LOCATE('-', old_value)) ) AS new_value
UPDATE sourcetable
SET fieldname = CONCAT( '4', SUBSTRING(fieldname FROM LOCATE('-', fieldname)) )
WHERE LOCATE('-', fieldname)
/* AND another conditions */

How to replace a regex pattern in MySQL

I have a table called myTable which has a column called col1. This column contains data in this format: (1 or 2 digits)(hyphen)(8 digits).
I want to replace all the data in this column and replace everything before hyphen with 4, so this is an example:
--------------------------------
| old values | New Values |
--------------------------------
| 1-654283568 => 4-654283568 |
| 2-467862833 => 4-467862833 |
| 8-478934293 => 4-478934293 |
| 12-573789475 => 4-573789475 |
| 16-574738575 => 4-574738575 |
--------------------------------
I am using MySQL 5.7.19, I believe REGEXP_REPLACE is available in MySQL Version 8+... not sure how this can be achieved?
You don't need regex; you can use SUBSTRING_INDEX to extract everything after the hyphen and concatenate 4- to that:
UPDATE myTable
SET col1 = CONCAT('4-', SUBSTRING_INDEX(col1, '-', -1))
Demo on dbfiddle
This will work regardless of the number of characters after the hyphen.
Looking to your pattern seem you could avoid regexp
update myTable
set col1 = concat('4-', right(col1,8))
or
update myTable
set col1 = concat('4', right(col1,9))
Try this:
UPDATE testing SET val=REPLACE(val,SUBSTRING(val,1,LOCATE('-',val)),'4-');
Fiddle here :https://www.db-fiddle.com/f/4mU5ctLh8NB9iKSKZF9Ue2/2
Using LOCATE to find '-' position then use SUBSTRING to get only the front part of the '-'.
SELECT CONCAT( #new_prefix, SUBSTRING(old_value FROM LOCATE('-', old_value)) ) AS new_value
UPDATE sourcetable
SET fieldname = CONCAT( '4', SUBSTRING(fieldname FROM LOCATE('-', fieldname)) )
WHERE LOCATE('-', fieldname)
/* AND another conditions */

Replace first character in a string in MySQL

On my database MySQL I have this string :
19-003950
From this string I need extract only
003950
And transform to :
103950
replacing first character '0' to '1' in a string.
I have tried withous success this SQL query:
mysql> SELECT
REPLACE (
SUBSTRING_INDEX('19-003950', '-' ,- 1),
SUBSTRING(
SUBSTRING_INDEX('19-003950', '-' ,- 1),
1,
1
),
'1'
) AS NEWSTRING;
+-----------+
| NEWSTRING |
+-----------+
| 113951 |
+-----------+
1 row in set
Please can you help me ?
Consider:
select
concat(
'1',
substring(str, locate('-', str) + 1)
) new_string
from (select '19-003950' str) t
locate('-', str) gives you the position of the dash in the string. You can add 2 to that and take everything from that position until the end of string. Finally, concatenate '1' at the beginning of the string.
Demo on DB Fiddlde:
| new_string |
| :--------- |
| 103950 |
Here's your query.
select concat('1', right(reverse(substring_index(reverse('19-003950'),'-', 1)),
length(substring_index(reverse('19-003950'),'-', 1) - 1)))

How to select all longer, shorter versions of the string, as well as the string itself

What would be the most elegant way to get all entries to a given record that are "longer", "shorter" or the same.
Here is an example:
Table 1
"1234567"
"123456"
"12345"
"12346"
"12355"
"123"
"12"
Suppose the original is "12345". Then I would like to select "1234567", "123456" (the longer versions), "123", "12" (The shorter versions) as well as the original "12345" from Table 1, but NOT "12346" and "12355".
I know how to do it using two queries, but is this possible to select the records with a single query?
You can use the following:
SELECT col1,
CASE WHEN col1 LIKE '12345%' AND LENGTH(col1) > LENGTH('12345') THEN 'longer'
WHEN col1 LIKE '12345%' AND LENGTH(col1) = LENGTH('12345') THEN 'same'
WHEN '12345' LIKE CONCAT(col1, '%') AND LENGTH(col1) < LENGTH('12345') THEN 'shorter'
ELSE '' END AS compared_length
FROM test
demo: https://www.db-fiddle.com/f/jzaz6GpfgZ3iBcnjiApybL/0
You can use two LIKE conditions:
select *
from t
where t.str like '12345%'
or '12345' like concat(t.str, '%')
Demo: https://rextester.com/FGKMC15900
Read it as: Select all rows where the column value starts with the "original" string or the "original" string starts with the column value.
Here is an alternative version, that uses a WHERE clause with a REGEXP to filter out unrelevant records ; then, all that is left to do is compare the lenghts of the strings :
SELECT
col1,
CASE
WHEN LENGTH(col1) > LENGTH(#match) THEN 'longer'
WHEN LENGTH(col1) < LENGTH(#match) THEN 'shorter'
ELSE 'same'
END result
FROM mytable
WHERE col1 REGEXP #match OR #match REGEXP col1
Demo on DB Fiddle :
SET #match = '12345';
SELECT
col1,
CASE
WHEN LENGTH(col1) > LENGTH(#match) THEN 'longer'
WHEN LENGTH(col1) < LENGTH(#match) THEN 'shorter'
ELSE 'same'
END result
FROM mytable
WHERE col1 REGEXP #match OR #match REGEXP col1;
| col1 | result |
| ------- | ------- |
| 1234567 | longer |
| 123456 | longer |
| 12345 | same |
| 123 | shorter |
| 12 | shorter |

How to string-compare for a single space

I want to check whether a column has any values that are a single space character.
I initially thought that
WHERE my_column = ' '
would be sensible. But no. That will also match columns which have multiple spaces for some reason:
SELECT ' ' = ' ' => true
So I can use a regular express or hex encoding to test:
WHERE HEX(my_column) = '20'
WHERE my_column REGEXP '^\ $'
Both work. But I suspect both (certainly the latter) will be quite inefficient.
Is there a better way?
A BINARY comparison of the two strings is required for an exact match
Under normal circumstances, trailing whitespace is not regarded in the comparison, but the BINARY operator forces it to be:
BINARY also causes trailing spaces to be significant.
mysql> SELECT BINARY ' ' = ' ';
+--------------------+
| BINARY ' ' = ' ' |
+--------------------+
| 0 |
+--------------------+
Incidentally, it isn't just whitespace-only comparisons that are affected by the trailing whitespace issue:
mysql> SELECT 'abc ' = 'abc';
+------------------+
| 'abc ' = 'abc' |
+------------------+
| 1 |
+------------------+
...but...
mysql> SELECT BINARY 'abc ' = 'abc';
+-------------------------+
| BINARY 'abc ' = 'abc' |
+-------------------------+
| 0 |
+-------------------------+
...and even more confusingly, leading whitespace is significant:
mysql> SELECT ' abc ' = 'abc';
+-------------------+
| ' abc ' = 'abc' |
+-------------------+
| 0 |
+-------------------+
Regarding indexing:
BINARY will prevent an index from being used on the character column. However, a note on the docs suggests that the index will be used if the BINARY operator is applied to the string literal side of the comparison as in:
SELECT * FROM `tbl` WHERE `col` = BINARY 'string '
where my_column = ' ' and LENGTH(my_column) = 1
SELECT CASE WHEN COL_NAME='' THEN 'yes' ELSE 'no' END AS SPACE_INDICATOR FROM Table_NAME WHERE LENGTH(COL_NAME)=1;