Transforming a column to have 10 Digits - mysql

I have a csv file that contains phone numbers, some of them have 9 digits and some of them have 10. Is there a command that would allow the transformation of the column such that numbers that have only 9 digits will have a 0 appended in front of the numbers.
For example,
if the column has values "443332332" and "0441223332", I would like to have the value of the one with 9 digits changed to "0443332332"?
Sorry, I should have elaborated.
I was wondering if there was a command to do it in SQLlite easily? I prefer not to use excel to transform the column as if I can get it to working with sqllite it would be so much easier and faster.

A more generic solution would be:
select substr('0000000000'||'1234567', -10, 10) from table_name;
The above query would always return 10 digits and add leading zeroes to the missed out number of digits.
For example, the above query would return : 0001234567
For Update, use
UPDATE TABLE_NAME SET PHONE_NO = substr('0000000000'|| PHONE_NO, -10, 10);

If you're sure that just prepending a zero on strings with length 9 will work for your application, something simple will work:
SELECT CASE WHEN LENGTH(phone_number) = 9 THEN '0'||phone_number
ELSE phone_number
END AS phone_number
FROM your_table
;
You could also update the table, depending on your needs:
UPDATE your_table
SET phone_number = '0'||phone_number
WHERE LENGTH(phone_number) = 9
;

Open the .csv using Excel,
Add a filter to the column,
Sort from A-Z to get all the columns with 9 digits,
Then follow the steps here
http://office.microsoft.com/en-au/excel-help/keep-leading-zeros-in-number-codes-HA010342581.aspx

Related

How do I Query for used BETWEEN Operater for text searches in MySql database?

I have a SQL Table in that i use BETWEEN Operater.
The BETWEEN Operater selects values within range. The values can be numbers, text , dates.
stu_id name city pin
1 Raj Ranchi 123456
2 sonu Delhi 652345
3 ANU KOLKATA 879845
4 K.K's Company Delhi 345546
5 J.K's Company Delhi 123456
I have a query like this:-
SELECT * FROM student WHERE stu_id BETWEEN 2 AND 4 //including 2 & 4
SELECT * FROM `student` WHERE name between 'A' and 'K' //including A & not K
Here My Question is why not including K.
but I want K also in searches.
Don't use between -- until you really understand it. That is just general advice. BETWEEN is inclusive, so your second query is equivalent to:
WHERE name >= 'A' AND
name <= 'K'
Because of the equality, 'K' is included in the result set. However, names longer than one character and starting with 'K' are not -- "Ka" for instance.
Instead, be explicit:
WHERE name >= 'A' AND
name < 'L'
Of course, BETWEEN can be useful. However, it is useful for discrete values, such as integers. It is a bit dangerous with numbers with decimals, strings, and date/time values. That is why I encourage you to express the logic as inequalities.
In supplement to gordon's answer, one way to get what you're expecting is to turn your name into a discrete set of values:
SELECT * FROM `student` WHERE LEFT(name, 1) between 'A' and 'K'
You need to appreciate that K.K's Company is alphabetically AFTER the letter K on its own so it is not BETWEEN, in the same way that 4.1 is not BETWEEN 2 and 4
By stripping it down to just a single character from the start of the string it will work like you expect, but take cautionary note, you should always avoid running functions on values in tables, because if you had a million names, thats a million strings that mysql has to strip out to just the first letter and it might no longer be able to use an index on name, battering the performance.
Instead, you could :
SELECT * FROM `student` WHERE name >= 'A' and name < 'L'
which is more likely to permit the use of an index as you aren't manipulating the stored values before comparing them
This works because it asks for everything up to but not including L.. Which includes all of your names starting with K, even kzzzzzzzz. Numerically it is equivalent to saying number >= 2 and number < 5 which gives you all the numbers starting with 2, 3 or 4 (like the 4.1 from before) but not the 5
Remember that BETWEEN is inclusive at both ends. Always revert to a pattern of a >= b and a < c, a >= c and a < d when you want to specify ranges that capture all possible values
Compare in lexicographical order, 'K.K's Company' > 'K'
We should convert the string to integer. You can try that mysql script with CAST and SUBSTRING. I've updated your script here. It will include the last record as well.
SELECT * FROM student WHERE name CAST(SUBSTRING(username FROM 1) AS UNSIGNED)
BETWEEN 'A' AND 'K';
The script will work. Hope it will helps to you.
Here I've attached my test sample.

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

mysql sort order, need slightly different result

I have a varchar column that I am currently sorting by using: ORDER BY (col_name+0)
This column contains both digits and non-digits, and the result of this sorting is this:
D3
D111
M123-M124
M136
4
9
10
25
37b
132
147-149
168b
168ca
This sorting is almost perfect for our application, but with one exception: we want the items that start with letters to display after those that start with numbers. This being the ideal result:
4
9
10
25
37b
132
147-149
168b
168ca
D3
D111
M123-M124
M136
I'm hoping this can be achieved in the select statement, rather than needing to loop through everything in code again after the select. Any ideas?
You can use this:
ORDER BY
col_name regexp "^[^0-9]",
case when col_name regexp "^[0-9]" then col_name + 0
else mid(col_name, 2, length(col_name )-1) + 0 end,
col_name
this will put rows that begins with a digit at the top. If col_name begins with a digit, I'm sorting by it's numeric value, if not I'm sorting by the numeric value of the string beginning at the second character.

MySQL add data to an existing field

I have a field enq_id - it currently contains numbers such as:
80081
414365
567
Now, I need to update the field in two ways, firstly I need to find out if any of the numbers are more than 6 characters long (there shouldn't be but I need to check). I then need to set a character limit of 6 characters on that field and then, finally, add 0's to the beginning of any enq_id that isn't 6 characters long to make it 6 characters.
Any ideas?
If the following returns the result you're trying to achieve:
SELECT
`enq_id`,
IF(CHAR_LENGTH(`enq_id`) < 6,
LPAD(`enq_id`,6,'0'),
SUBSTRING(`enq_id`,1,6)
) AS 'result'
FROM `some_table`
WHERE CHAR_LENGTH(`enq_id`) != 6
Then using the following will update your table accordingly.
UPDATE `some_table`
SET `enq_id` =
IF(CHAR_LENGTH(`enq_id`) < 6,
LPAD(`enq_id`,6,'0'),
SUBSTRING(`enq_id`,1,6))
WHERE CHAR_LENGTH(`enq_id`) != 6
Note that the SUBSTRING() function deletes all the characters after the 6th character, and LPAD adds preceding zeros (in the above example) if needed, to each record.
UPDATE: For some reason I added an extra condition. The optimized code(s) should have been:
SELECT `enq_id`,LPAD(`enq_id`,6,'0') AS 'result'
FROM `some_table`
WHERE CHAR_LENGTH(`enq_id`) < 6
and
UPDATE `some_table`
SET `enq_id` = LPAD(`enq_id`,6,'0')
WHERE CHAR_LENGTH(`enq_id`) < 6

MySQL Regular Expression Match

I am trying to update my product database and find duplicates after some product numbers where changed.
In the past the changed item numbers just had an extra dash and number or letter on the end and I used this where clause:
where a.ProdNum REGEXP CONCAT('^', b.ProdNum, '(\-|\s)[a-zA-Z0-9]+')
Now the REGEXP is over my head.
Old Product number: BRB-0325
New Product number: 0325-15 (the number after the dash can be any 1 or 2 digit number)
So basically I need a where clause that will drop the "BRB-" from the Old Number and drop any 1 or 2 digit number after the new item number.
I can do the dropping of the "BRB-", but not sure how to handle dealing with the 1-2 digit number, and I really don't know how to do both at the same time.
where b.ProdNum = CONCAT('BRB-', c.ProdNum)
Here is what I tried with the 2 digits and that didn't work.
where c.ProdNum REGEXP CONCAT('^', b.ProdNum, '(\-)[0-9]{1,2}+')
Thanks for your help.
Assuming c.ProdNum is the new number and b.ProdNum is the old:
WHERE CONCAT('BRB-', c.ProdNum) REGEXP CONCAT('^', b.ProdNum, '-[0-9]{1,2}$')
This adds "BRB-" to the new number and compares it to the old. So you will be essentially comparing "BRB-0325-15" with "BRB-0325-nn".
Why are you using REGEXP? Use a straightforward comparison:
where a.ProdNum like concat(substring(b.ProdNum, 4), '-??')
This will match 0325-ab, but surely you don't have such new product numbers.
This should work for you:
SELECT
old.*
FROM
products old,
products new
WHERE new.ProdNum REGEXP CONCAT('^', SUBSTRING(old.ProdNum, 5), '-[0-9]{1,2}$')
AND old.ProdNum LIKE 'BRB-%'
This should use an index on ProdNum if one is available.