I have a column with credit card numbers in different formats. Some have dashes some don't. I need the first character and the last four formatted like this:
5-1234
Just want to run a UPDATE query from the console. I know how to get the first and last but can't get the union right. Here is my code.
UPDATE table
SET credit_no = LEFT(credit_no, 1)
union "-"
union SUBSTRING(credit_no, -4, 4);
Try something like this:
UPDATE table SET credit_no = CONCAT(LEFT(credit_no, 1), "-", RIGHT(credit_no, 4));
Or even:
UPDATE table SET credit_no = CONCAT_WS('-', LEFT(credit_no, 1), RIGHT(credit_no, 4));
See Docs: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
Related
I have a column that has values of the forms:
AB232/10D20
A232/10D20
232/10D20
How can I extract the three numbers in mysql? I want to get 232, 10 and 20 separately and insert them in other columns.
For example, if the the column containing those values is called original_column, the query I need is something like this:
update mytable
set number_one = something(original_column),
number_two = something2(original_column),
number_three = something3(original_column)
Try the following query using LOCATE and SUBSTRING functions:
UPDATE mytable
SET number_one = SUBSTRING(original_column, LOCATE('/', original_column) - 3, 3),
number_two = SUBSTRING(original_column, LOCATE('/', original_column) + 1, 2),
number_three = SUBSTRING(original_column, -2, 2)
The middle number is easy. The last number and the first number are pretty easy, if we assume that they are always 3 and 2 characters.
update mytable
set number_one = right(substring_index(original_column, '/', 1), 3),
number_two = substring_index(original_column, '/', -1) + 0,
number_three = right(original_column, 2);
There are other tricks you can use if the columns are not exactly those lengths. But in your example, all the values have the same length.
When splitting it using regex, use the group feature. Regex for splitting 3 numbers with non-numeric delimiters (for integers only) would be
([0-9])[^0-9]([0-9])[^0-9]([0-9]*)
Use a regex tester to improve the regex.
I have a database with 2 fields named "number" and "form" and need to alter some fields.
I have a list in excel with the values i.e 1234.5, 1233.7 where the 1234 is the number and the 5 is the form number in my table.
is it possible to do something like:
UPDATE `table` SET `field`='value' WHERE `number` IN (1234,1233) AND `form` IN (5,7)
the first problem I see is when I have two "numbers" that is the same but different "form" numbers.
or can I do something like:
UPDATE `table` SET `field`='value' WHERE CONCAT(number,'.',form) IN (1234.5,1233.7)
so is there any other way I can approach this?
UPDATE `table` SET `field`='value' WHERE CONCAT(number,'.',form) IN ('1234.5','1233.7')
does the job, just needed the IN ('1234.5','1233.7') instead of IN (1234.5,1233.7)
I am not exactly sure what you are trying to accomplish, but it sounds like the numbers would be different. Try finding the POSITION() of the "." in the number. Then, do a SUBSTRING() on those positions. It Would look like so:
UPDATE `table`
SET `field`='value'
WHERE number = SUBSTRING(input, 1, POSITION('.', input) - 1)
AND form = SUBSTRING(input, POSITION('.', input) + 1)
The following code will check the Number before the ".", and then the form after the ".".
I have a table with 8 columns, all varchar (24, 24, 24, 5, 5, 255, 255, 255).
The three important columns are:
icd10code (varchar 24)
icd10codedot (varchar 24)
icd10length (varchar 5)
I wish to convert one column that looks like this - "ABCDEF" to this - "ABC.DEF".
Here is the MySQL query used, and it worked perfectly!
UPDATE icd10
SET icd10codedot =
CONCAT ( LEFT(icd10code, 3) ,
"." ,
RIGHT( icd10code, ( LENGTH(icd10code)-3 )
)
);
All 91,000 rows came out nice EXCEPT there was a trailing "." at the end of those entries that only had 3 characters - like this: "ABC.".
I don't want that dot at the end.
So I tried this:
UPDATE icd10 SET icd10codedot = LEFT(icd10codedot, 3) WHERE LENGTH(icd10codedot)=4;
and this:
UPDATE icd10 SET icd10codedot = LEFT(icd10code, 3) WHERE LENGTH(icd10code)=3;
and this:
UPDATE icd10 SET icd10codedot = LEFT(icd10code, 3) WHERE LENGTH(icd10code)=3;
and none of them worked (a bunch of other combinations were also run, but none worked).
So to see how things were doing in the code, I made a column called "icd10length" varchar int and did this query:
UPDATE icd10 SET icd10length = LENGTH(icd10code);
and all I got was 91,000 "7"s lined up on the right of the column.
I changed the column from INT to VARCHAR 5, and reran the query and got 91000 "7"'s lined up on the RIGHT of the column ( :-) ).
So two questions:
How can I get the correct length of the variable icd10code or icd10codedot into a column?
Do you have a slick way of clipping the last character of a string that is 4 characters long?
Thanks very much again!
PS - I tried the initial query in PHP and it crashed - timing out after 300 seconds. Found on the web that you should always do things in MySQL whenever possible.
PSS - I'm doing all of this via phpMyAdmin.
It seems you have empty space at the end, try using rtrim.
How to remove characters from end from any column in MYSQl using SUBSTR ?
For example:
Suppose column value is 1221213.2, so I want to achieve 1221213.
SELECT SUBSTR(colname,1,-2) FROM tablename.
It's this query (assuming your 'remove' is update data in DB):
UPDATE tablename SET colname=SUBSTR(colname, 1, CHAR_LENGTH(colname)-1)
-this will remove 1 symbol from end of string. (so, SUBSTR(colname, 1, CHAR_LENGTH(colname)-2) for 2 sumbols - you've updated)
(upd. there are suggestions for LEFT() function, it's better. I will not repeat that code, however).
It work for firebirb:
select substr(p.name, 1, strlen(rtrim(p.name))-2) from products p
I think for MySQL strlen need replaced with CHAR_LENGTH() function.
You can also use LEFT function.
select LEFT(colname, LENGTH(colname)-2);
LEFT(str, len) selects number of characters from left of the string specified.
SELECT LEFT(colname, CHARACTER_LENGTH(colname)-2);
Prefer CHARACTER_LENGTH() over LENGTH() to avoid surprises.
Now it looks like you are trying to truncate the decimal digits from a decimal number. This is what you might be looking for instead:
SELECT TRUNCATE(colname, 0);
And to update your table:
UPDATE tablename SET colname = TRUNCATE(colname, 0); -- or LEFT(...), or whatever approach you choose
The previous answers all assume that you always want to remove the last 2 characters. But what if there's more (or less) significant digits after the period?
UPDATE your_table
SET colname = SubStr(colname, 0, Locate('.', colname))
WHERE Locate('.', colname) > 0
UPDATE
Even better method:
UPDATE your_table
SET colname = SubString_Index(colname , '.', 1)
WHERE Locate('.', colname) > 0
I have one phone field column which contains phone numbers like that
'123456789'
'123-456-789'
etc
means it contain 9 digit number or number + hyphen.
I want to make a SQL query which updates all records in 'xxx-xxx-xxx' format.
I have made few attempts but cannot get exact solution.
Please any one help me.
Thanks in advance.....
use something like
UPDATE mytable SET phone =
CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 3))
Also to only get the rows that are missing hyphens you would say WHERE phone not like '%-%'
Close. You'd first need to test if the string contained a '-' before adding more, but that's the right track.
UPDATE mytable
SET phone = CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 4))
where INSTR( phone, '-' ) = 0;