How do i replace the last character of a string with another character only if it ends with D for example?
-
Step 1: Check the MySQL documentation. For example, what would you call a function that can replace parts of a string? – tadman Feb 22 '19 at 17:47
assuming you want chage you last char to X where last char = 'D'
select concat(LEFT(your_col, LENGTH(left) -1), 'X' )
FROM Your_table
WHERE RIGHT(your_col, 1) = 'D';
or for avoid multybytes lenght problem as suggested by spencer7593
select concat(LEFT(your_col, CHAR_LENGTH(left) -1), 'X' )
FROM Your_table
WHERE RIGHT(your_col, 1) = 'D';
-
we might better use
CHAR_LENGTH
function in place ofLENGTH
function if there is a possibility thatyour_col
might contain multibyte characters. – spencer7593 Feb 22 '19 at 18:37