SQL - Strip first digit if it starts with the number '9' - mysql

Basically I have a table called 'Telephone' with over 100,000+ records.
In this database I have numbers such as:-
90123456789
91234567893
97126372319
Basically I want to Update the Telephone.DB and SET the numbers so it strips the first number only if it starts with the number '9', so the result would end up as follows:-
0123456789
1234567893
7126372319
Any idea how I can achieve this using MySQL?

Provided that the datatype is a string type, this should do it;
UPDATE numbers
SET number = SUBSTR(number, 2)
WHERE number LIKE '9%';
An SQLfiddle to test with.
As always, test yourself before running updates from random people on the Internet on your production database :)

Ok, this works for a VARCHAR column. You could always convert on the fly the value to varchar:
SELECT CONCAT(REPLACE(SUBSTR(PHONEFIELD,1,1),9,''), SUBSTR(PHONEFIELD,2)) AS PHONEWITHOUTNINE FROM TABLENAME
Check it out and tell me if it works.

Related

Split field values using specific character by mysql

sorry for the repeated question. I already asked for help about this by the use of ORACLE database. But now I really wanted to know how can I split this using MySQL database
It is possible to split field values using a specific character? Here is my sample table
value
10uF
2K
1.0uF
200UF
I want it to split by this:
value capacitance/resistance
10 uF
2 K
1.0 uF
200 UF
Hope you can help me once again. Thanks! and more power!
You can use below code
create table temp
(value varchar(10)
);
insert into temp values ('10uF');
insert into temp values('2K');
SELECT value + 0 AS num
, REPLACE(value,value+0,'') AS unit
FROM temp
O/P
num letter
10 uF
2 K
The trick the query is using is to evaluate the column value in a numeric context, by adding a zero. MySQL will return the numeric value.
But this wont work in case of 10Uf10,2k3..
Hope all your data is digit + charachter
Fiddle for the same

MySQL SUM() always returns a 0 value

I have amounts stored as a varchar in my table.
When attempting to sum them it always returns 0.00.
Below is an example using only one record from the db.
SELECT col1, SUM(CAST(col2 AS DECIMAL(20,2))) derived1
FROM table
WHERE col3 = 'FIT'
AND col1 = '6211195'
GROUP BY col1
This returns one row with a 0 value.
By removing the SUM and CAST from the query, I can see that it is pulling the value as it should, but I can not sum or cast it, adding either of those breaks it and returns 0 again.
I have also tried converting the field to a decimal type and it just zeros all the values.
EDIT:
Ughh, I just ran a REGEX query to detect anything that isnt an alphanumeric value or a decimal point. It appears that there are non ascii characters in the field that I cant see, messing with the type casting. Will continue to update as I learn more.
Show some sample values of col2. A string that really is a number is treated as a number. Hence '1' will become 1.
However, if the string does not start with a digit, '-', or '+' (after leading spaces), then it will be 0 (in most cases). So 'A1' will be 0. And so on. As will '$100'.
The lesson is: If a column contains numbers, store them as numbers. Really simple.

sql query help in sorting a field that's got both string and numbers

I've got a text field called source_recid. It stores half string half number like strings in it.
Example
shop.orders.32442
the syntax is DATABASENAME.TABLENAME.RECID
My goal is to scan this col and find out the biggest RECID ( the integer) in it.
So, in a case like this
shop.orders.32442
shop.orders.82000
shop.orders.34452
It would be the record whose source_recid is shop.orders.82000. Why? Cause 82000 happens to be the largest integer.
What SQL statement would get me that record?
One option to this is to create a new column ( the_ids ) and move all the integers in it and then run something like this
select source_recid from mytable
where source_recid like 'shop.orders.%'
order by the_ids DESC
LIMIT 1
Is there a way to pull this off without going thru this step?
First of all, unless all of your RECIDs are exactly five characters long forever and always, the select you put up won't work, becuase "shop.order.9" would come out as larger than "shop.order.10", which is wrong.
What I think we need to do here is extract the numeric part, cast it to an integer, and sort by that. Now, I don't have access to mySQL, so this may not be exactly right, but it should be close ...
SELECT
CAST(SUBSTRING_INDEX(RECID,'.',-1) AS INT) AS RecIdNumber
FROM
table
WHERE
RECID LIKE 'shop.order.%'
ORDER BY
RecIdNumber DESC
LIMIT
0, 1
This will take the part after the last dot, convert it to an INT, name it 'RecIdNumber', and sort by that.
I hope this helps.
SELECT CAST(SUBSTRING_INDEX(field,'.',-1) AS INT) AS RID
FROM yourtable
WHERE
RECID LIKE 'shop.order.%'
ORDER BY
RID DESC

Sorting an Alphanumeric value mysql

I want to find the max value in a column.
Column values are,
E00004,
A00005,
B00011,
H-00001,
E2100112,
EFQ20098,
ESSF20003
I just want to sort the values by their number, Dont mind about the alphabets. It have to be like this, I'm using MYSQL
E2100112,
ESSF20003,
EFQ20098,
B00011,
A00005,
E00004,
H-00001
Assuming the last 5 digits are the number:
select columnName from tableName
order by convert(int, right(columnName, 5)) desc
As #IkeWalker stated, the number can have an arbitrary size.
For it, you'll have to use a while cycle to check the number.
Or, you can have a function do that for you!
Check this article!

update a column by subtracting a value

I'm trying to come up with a MySQL query that will update points... Can I do something like this?
UPDATE `a75ting`.`username` SET `points` = '`points` - 5'
UPDATE a75ting.username
SET points = points - 5
by putting the single quotes around the "points -5", you converted that expression into a plaintext string. Leaving it without the quotes lets MySQL see you're referring to a field (points) and subtracting 5 from its current value.
Run this query to find out the difference:
SELECT '`points` - 5' AS string, `points` - 5 AS expression
FROM a75ting.username