Is there a way to replace all number's digits to "0" but keep only first digit:
Sample convert:
`3849` => `3000`
`3001` => `3000`
`3838383838` => `3000000000`
and apply this on mysql column?
Thanks,
One straightforward option is to concatenate together the first digit with a sequence of zeroes whose length is one less than the length of the number.
SELECT
CONCAT(LEFT(col, 1), REPEAT('0', CHAR_LENGTH(col) - 1)) AS new_col
FROM yourTable
Note that if col be a numeric type column, then technically we should be casting it to text before taking a substring. In this case, MySQL would do an implicit conversion to text, so technically we don't need a formal cast, though this point should be made.
Demo here:
CONCAT(LEFT(col, 1), REPEAT('0', CHAR_LENGTH(col) - 1)), you can do RPAD(LEFT(col, 1), '0', CHAR_LENGTH(col)).TRUNCATE with a negative second argument will set digits to the left of the decimal point to zero:
SELECT TRUNCATE(column_name, -FLOOR(LOG10(column_name)))
FROM table_name;
Try this for MYSql:
DECLARE v_test NVARCHAR(100)
SET v_test = '585623658'
SELECT LEFT(v_test,1) + REPLICATE(0,(CHAR_LENGTH(RTRIM(v_test)) - 1)) AS Colum