How do you add multiple decimal points to a column in SQL? - mysql

I have a column in my table that contains 10-digit hts codes (0000.00.0000). Some of the values do not have the full stop points (0000000000). How can I add the full stop points to all the rows that do not have them?
Edit
The column type is VARCHAR
I want to update all rows where full stop is not present.

I would remove the full stops from all these columns using REPLACE() as part of the update, then you can apply some simple logic using a CONCAT() LEFT(), RIGHT() and SUBSTRING()
to change the simple 0000000000 into 0000.00.0000 like this, rather than trying to identify only the columns without the dots
UPDATE table
set column = CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
);
Test it using a select so you do no damage
SELECT some_identifying_column,
CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
) as justtesting;

Another approach using insert comes to mind. As others already mentioned, it's a good idea to remove the full stops before inserting them in the 5th and 8th position in the string
select *, insert(insert(replace(hts,'.',''),5,0,'.'),8,0,'.')
from t;

Related

Counting how many fields (in a row) are filled in SQL

I want to count how many columns in a row are not NULL.
The table is quite big (more than 100 columns), therefore I would like to not do it manually or using php (since I dont use php) using this approach Counting how many MySQL fields in a row are filled (or empty).
Is there a simple query I can use in a select like SELECT COUNT(NOT ISNULL(*)) FROM big_table;
Thanks in advance...
Agree with comments above:
There is something wrong in the data since there is a need for such analysis.
You can't completely make it automatic.
But I have a recipe for you for simplifying the process. There are only 2 steps needed to achieve your aim.
Step 0. In the step1 you'll need to get the name of your table schema. Normally, the devs know in what schema does the table reside, but still... Here is how you can find it
select *
from information_schema.tables
where table_name = 'test_table';
Step 1. First of all you need to get the list of columns. Getting just the list of cols won't help you out at all, but this list is all we need to be able to create SELECT statement, right? So, let's make database to prepare select statement for us
select concat('select (length(concat(',
group_concat(concat('ifnull(', column_name, ', ''###'')') separator ','),
')) - length(replace(concat(',
group_concat(concat('ifnull(', column_name, ', ''###'')') separator ','),
'), ''###'', ''''))) / length(''###'')
from test_table')
from information_schema.columns
where table_schema = 'test'
and table_name = 'test_table'
order by table_name,ordinal_position;
Step 3. Execute statement you've got on step 2.
select (length(concat(.. list of cols ..)) -
length(replace(concat(.. list of cols .. ), '###', ''))) / length('###')
from test_table
The select looks tricky but it's simple: first replace all nulls with some symbols that you're sure you'll never get in those columns. I usually do that replacing nulls with "###". that what all that "ifnull"s are here for.
Next, count symbols with "length". In my case it was 14
After that, replace all "###" with blanks and count length again. It's 11 now. For that I was using "length(replace" functions together
Last, just divide (14 - 11) by a length of a replacement string ("###" - 3). You'll get 1. This is exactly amount of nulls in my test string.
Here's a test case you can play with
Do not hesitate to ask if needed

MySQL: How to restructure this code to eliminate adding and deleting intermediate colums

How do I update the following code so it's not adding and then deleting columns?
This code calculates the values for a numeric column (named "LengthFloat") from a column with values such as "3 yards" (named "Length").
alter table sometable add Value float;
alter table sometable add Units varchar(15);
Update sometable
set Value = SUBSTRING_INDEX(SUBSTRING_INDEX(Length, ' ', 1), ' ', -1),
Units = SUBSTRING_INDEX(SUBSTRING_INDEX(Length, ' ', 2), ' ', -1);
update sometable
set LengthFloat = case Units
when 'yards' then Value*3
when 'yard' then Value*3
when 'feet' then Value
when 'foot' then Value
end;
alter table sometable drop column Value;
alter table sometable drop column Units;
drop view if exists sometable_with_value_units;
create view sometable_with_value_units as
select id,
SUBSTRING_INDEX(SUBSTRING_INDEX(Length, ' ', 1), ' ', -1) as value,
SUBSTRING_INDEX(SUBSTRING_INDEX(Length, ' ', 2), ' ', -1) as units
from sometable;
select id,
case units
when 'yards' then value * 3
when 'yard' then value * 3
when 'feet' then value
when 'foot' then value
end as feet
from sometable_with_value_units;
Please see http://sqlfiddle.com/#!9/ed7ca1/3
Sure it is possible to fiddle around with length expressions. But it is painful in SQL. Instead...
I would step back and decide which units to use. I would probably pick the metric system. For example, I might pick "meter" for all lengths, not kilometer, not Angstrom, not foot, not furlong, not barn, not light-year. (OK, if the app dealt with astronomy, I might use light-year.) Then I would do conversions in the app, not the database. I would not be adding and dropping columns except in extreme situations.
The code should be left as is.
The only other suggested answer that describes another method (using a View) is so terribly inefficient it has to be rejected (see the comments there).

Join two columns of the same table ignoring the null value. MySql

I'm trying to join two columns of the same table but, if there are null values (in my case they are in the second column), I want to take anyway the row that interests me and, rather than putting the null value, I would put ''. The columns that I want to join are Surname and Name. In other words, I tried to use :
SELECT CONCAT(CSurname, ' ', CName)
FROM Client;
In this way if I have a valid value for surname and a null value for name I obtain null. I use MySql, thanks.
If you want to avoid the problem with a leading space, then the easiest way is probably CONCAT_WS():
SELECT CONCAT_WS(' ', CSurname, CName)
FROM Client;
Unlike most other functions, CONCAT_WS() ignores NULL values (except for the separator), greatly simplifying this logic -- particularly when you are working with more than two columns.
Without it, the equivalent logic could be expressed as:
SELECT CONCAT(COALESCE(CONCAT(CSurname, ' '), ''), COALESCE(CName, ''))
Try the ifnull function
SELECT CONCAT(CSurname, ' ', IFNULL(CName,'')) FROM Client;
I don't have a local mysql installation to try it out but the IFNULL function should achieve what you need.

Getting unique entries from a columns generated by matching regexp in SQL

I have a table which i am using to query and getting its one column which matches regular expression which is (\/.+\/\?).
Content of the resulted column is like:
/Anything here/?
Example output:
\abc\cdf\?....
\ab\?....
\abc\cdf\?....
\sb\?....
where '....' can be anything
Desired result i want is unique values before \? such that rows with duplicate regexp matched content are shown once only like here (\abc\cdf\?.... showing twice instead of onece)
\abc\cdf\?....
\ab\?....
\sb\?....
OR
\abc\cdf\?
\ab\?
\sb\?
I have looked very much but couldn't find anything there is regexp_substr in oracle but that is not working in SQL.
Please if someone could help me with the sql query that would be awesome.
If you want everything before the last \, then you can use substring_index() and some string manipulation:
select substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
) as firstpart,
count(*)
from table t
group by substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
);

I would like to replace the text in a column from "300-21-2" to "300-21-02" with one query

Is there an easy way to replace all the text in a VARCHAR 255 column from "300-21-2" to "300-21-02" with one query?
Thank you.
This is basic SQL
UPDATE tablename
SET columnname = '300-21-02'
WHERE columnname = '300-21-2'
If the pattern is always the same NNN-NN-N then what you need is:
update tablex
set column = concat( substr(column,1,7), lpad(substr(column,8),2,'0') )
see it at fiddle:
http://sqlfiddle.com/#!2/f59fe/1
EDIT As the op showed the pattern
update tablex
set column = CONCAT(
substring_index(col, '-',1), '-',
lpad(substring_index(substring_index(col, '-',-2), '-', 1),2,'0'), '-',
lpad(substring_index(col, '-',-1), 2, '0') )
If you like to convert the first set like 300 to 00300 as your pattern you add the lpad as this: lpad(substring_index(col, '-',1),5,'0')
This should be a lot easier if mysql has support to regex replace, but as it hasnt you have to work with the strings:
from this value: '300-02-1'
from substring_index(col, '-',1) I'm getting: 300
from substring_index(substring_index(col, '-',-2), '-', 1) I'm getting 02 I did this because just put the substring_index(col, '-',2) gave me 300-02 so, i got it from right to left (-2) then i get the first
and substring_index(col, '-',-1) it bring me 1 because it gets the value from right to left
Then I just concatenate it all formatting the ones I want.