How to remove the first characters in a column - mysql

I have a column that contains string. It is a number but saved as string. The string might start with 00 or more than two zeros. I need to remove the zeros and insert the new value (after removing) into another column. The problem is the number of zeros at the beginning is not fixed. It can be 2 or more. Is this task possible to be done with MySQL ? How?

A good hint is provided here:
UPDATE your_table
SET column2 = TRIM(LEADING '0' FROM column1)
supposing that the original value is stored in column1 and you want to write the 0-trimmed one in column2

you can use CONVERT(col,UNSIGNED INTEGER) to convert it into the number, that should remove the leading zeros.
query can be
insert into <table> values(select CONVERT(col,UNSIGNED INTEGER),..... from <table2>);

Related

SQL READING THE COLUMN VALUES?

0.0.0.1 saved in sql table column as 10001.
MY data base contains values as such above mentioned i wanted to sort it based on the values but if I sort is as it is it will give me wrong order so i need tp convert it to the above mentioned format(10001). i.e. Remove the dots(.)
Thank you.
(I guess you're actually using Oracle as a database, regarding the tool - Oracle SQL Developer - you've also tagged, which means that MySQL tag should be removed).
To me, it looks as if you'd want to a) remove dots, b) change datatype to number (so that it is correctly sorted):
order by to_number(replace(col, '.', ''))
It presumes that only characters allowed are digits and dots. If there's a value like 'A.0.0.1', it'll - of course - fail, as you can't convert letter A to a number.
Why are you storing the period '.' Character to begin with? If it's not needed you can remove it.
If you want to remove all non-alphanumeric characters you could use a regular expresion.
create table t (nm varchar2(20));
insert into t values ('.0.0.0.1');
insert into t values ('10.0.1.1.0');
commit;
select * from t;
NM
.0.0.0.1
10.0.1.1.0
update t
set nm = regexp_replace(
regexp_replace(nm, '[^A-Z0-9 ]', ''),
' {2,}', ' '
);
select * from t;
NM
0001
100110
You can use the translate command with a SELECT and the data will not be charged in the table.
See below
create table t (nm varchar2(20));
insert into t values ('.0.0.0.1');
insert into t values ('10.0.1.1.0');
commit;
SELECT translate(nm, '*.','*') from t
TRANSLATE(NM,'*.','*')
0001
100110
SELECT DISTINCT(column_name)
FROM Table_name
ORDER BY
TO_NUMBER (REGEXP_SUBSTR (column_name, '\d+',1,2)),
TO_NUMBER (REGEXP_SUBSTR (column_name,'\d+',1,3)) NULLS FIRST,
TO_NUMBER (REGEXP_SUBSTR (column_name,'\d+',1,4)) NULLS FIRST;

how to properly do the following update MYSQL statement?

i have a string with the following value
Germany,Sweeden,UAE
and i have an mysql row that have a value of one of this separated string with comma
as example a Country Row with a value Sweeden .
i have tried to use this sql statement to update a targeted row where its value is like one of this string separated with comma
hence if the row have a value of sweeden it should be updated sense sweeden value is include in the comma string
UPDATE CountryList SET Time= '100' WHERE Country LIKE '%Germany,Sweeden,UAE%'
but i couldn't update the targeted row using this syntax. any idea what is the correct syntax i should use ?
I think you want find_in_set():
update CountryList
set time = 100
where find_in_set(country, 'Germany,Sweeden,UAE')
This checks if country matches any of the values in the comma-separated row given as second argument to find_in_set().
Note that I removed the single quotes around the literal 100: if time is a number, then treat it as such (if it's a string instead, then keep the quotes).

MYSQL, Insert the numeric characters of a string in a column to another column

I am trying to clean the telephone numbers of a database so we can easily search for them. In the column TEL we have rows like:
654-598-5487
654.254.2456
(458)-5458789 e.3
I want to copy all those values to a new column where only the numeric characters are transferred:
6545985487
6542542456
45854587893
The new column (TEL_NO_FORMAT) is a big int and it only allows numbers, but if I execute something like this:
UPDATE CLIENTS set `TEL_NO_FORMAT` = `TEL`
It will only transfer the first numeric characters found and ignore the rest:
654
654
NULL
Easiest way is the REGEXP_REPLACE(MySQL 8.0+):
SELECT *, REGEXP_REPLACE(tel_no_format, '[^0-9]','') AS result
FROM clients
Answering the question in an update query
UPDATE CLIENTS SET TEL_NO_FORMAT = REGEXP_REPLACE(TEL, '[^0-9]','');
You should replace the undesired char before
UPDATE CLIENTS set `TEL_NO_FORMAT` = replace(replace(replace(`TEL`, '.',''),'-',''),')','')
because some char (eg '-') are create problem during conversion ..
anyway rember that a big int can't manage properly eventual tel number 0 prefixed eg:
00453778988

Remove leading '0' from a char and update the table

I have a table with ids they read '0050001B' and such then there is '50001B'. They are both the same but due to the fact that I'm using char when I want to return something it sees both as two different ids. Is there anyway to update the table such that I can get rid of the leading zero.
I used this code below to output without leading zeros, but I want to fix the table.
SELECT TRIM(LEADING '0' FROM id) FROM mytable;
but i want to fix the table so the zeros are gone on the table, not just on the output.
any help is greatly appreciated!!
you should use update.
update mytable
set id = TRIM(LEADING '0' FROM id);

Replace values with string mysql

I have a VARCHAR column in which some values end with the digit '5'. These
values may be of different lengths. I want to replace all such values
with the string 'UTR-5'. How can I do that?
Maybe not the fastest solution, but:
update myTable set value = 'UTR-5' where value like '%5'