Remove a single character from a varchar field SQL Server 2008 - sql-server-2008

I have a table with several varchar columns that are almost identical to primary keys I have in another table, with the exception of a period (.). I've looked at the replace function in T-SQL but the first argument isn't an expression. How can I remove all occurrences of a particular character with SQL? It seems like the correct answer might be to replace with a zero length string. Is that close?
To whomever felt the question didn't exhibit research effort it was mainly due to a misunderstanding of the documentation itself.

You can update the table directly using REPLACE on the column values:
UPDATE myTable
SET myColumn = REPLACE(myColumn, '.', '')

Do you want to remove all instances of the . from the string? If so, you were right about REPLACE:
DECLARE #Example TABLE
(
Value VARCHAR(100)
)
INSERT #Example (Value)
VALUES ('Test.Value'), ('An.otherT.est')
SELECT
REPLACE(Value, '.', '')
FROM
#Example
-- Replace only the first '.'
SELECT
STUFF(Value, CHARINDEX('.', Value, 0), 1, '')
FROM
#Example
Edit, making the example a little more useful since I played around with it anyway, I might as well post the example. :)

update your_table
set some_column = replace(some_column, '.', '')

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;

create new column with conditional values from existing column in MySql workbench?

i have a column in my MySql table named "details" like this:
now i want to add another column named "name" which will contain only the first word of each cell of column "details", like this:
so far i have done this:
select
substring(details, 1, instr(details, ' '))
from
my_table
now how to add this to a column "name"?
Please try this.
select
substring(details, 1, instr(details, ' ')) As Name
from
my_table
use position() function
DEMO
select
substring(details, 1, POSITION(' ' IN details))
from
my_table
OUTPUT:
value
Wellcare
ALSO locate() can be used
select
substring(details, 1, LOCATE(' ', details))
from
my_table
You can use SUBSTRING_INDEX to extract the first word of each value. For example:
SELECT SUBSTRING_INDEX('Wellcare Value (HMO-POS)', ' ', 1) AS name
Output:
Wellcare
To add this to your table, you have a couple of options. If you are running MySQL 5.7.6 or later, you can use a generated column as described in this question and the manual e.g.
ALTER TABLE mytable ADD COLUMN name VARCHAR(50) AS (SUBSTRING_INDEX(details, ' ', 1))
If you want to save storage at the expense of fetch time, declare the column as VIRTUAL, otherwise declare it as STORED.
You can also create a VIEW which includes this expression as a column. This is similar to a VIRTUAL generated column but will also work on versions of MySQL which don't support generated columns:
CREATE VIEW mytable_view AS
SELECT *, SUBSTRING_INDEX(details, ' ', 1) AS name
FROM mytable
The final option would be to create an actual column and then update it via a trigger, but both the preceding options are preferable.

How to delete specific value from a column in SQL row?

I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?

Avoid row was cut by GROUP_CONCAT error on insert without changing group_concat_max_len

I have an insert that uses a GROUP_CONCAT. In certain scenarios, the insert fails with Row XX was cut by GROUP_CONCAT. I understand why it fails but I'm looking for a way to have it not error out since the insert column is already smaller than the group_concat_max_len. I don't want to increase group_concat_max_len.
drop table if exists a;
create table a (x varchar(10), c int);
drop table if exists b;
create table b (x varchar(10));
insert into b values ('abcdefgh');
insert into b values ('ijklmnop');
-- contrived example to show that insert column size varchar(10) < 15
set session group_concat_max_len = 15;
insert into a select group_concat(x separator ', '), count(*) from b;
This insert produces the error Row 2 was cut by GROUP_CONCAT().
I'll try to provide a few clarifications -
The data in table b is unknown. There is no way to say set group_concat_max_len to a value greater than 18.
I do know the insert column size.
Why group_concat 4 GB of data when you want the first x characters?
When the concatenated string is longer than 10 chars, it should insert the first 10 characters.
Thanks.
Your example GROUP_CONCAT is probably cooking up this value:
abcdefgh, ijklmnop
That is 18 characters long, including the separator.
Can you try something like this?
set session group_concat_max_len = 4096;
insert into a
select left(group_concat(x separator ', '),10),
count(*)
from b;
This will trim the GROUP_CONCAT result for you.
You temporarily can set the group_concat_max_len if you need to, then set it back.
I don't know MySQL very well, nor if there is a good reason to do this in the first place, but you could create a running total length, and limit the GROUP_CONCAT() to where that length is under a certain max, you'll still need to set your group_concat_max_len high enough to handle the longest single value (or utilize CASE logic to substring them to be under the max length you desire.
Something like this:
SELECT SUBSTRING(GROUP_CONCAT(col1 separator ', '),1,10)
FROM (SELECT *
FROM (SELECT col1
,#lentot := COALESCE(#lentot,0) + CHAR_LENGTH(col1) AS lentot
FROM Table1
)sub
WHERE lentot < 25
)sub2
Demo: SQL Fiddle
I don't know if it's SQL Fiddle being quirky or if there's a problem with the logic, but sometimes when running I get no output. Not big on MySQL so could definitely be me missing something. It doesn't seem like it should require 2 subqueries but filtering didn't work as expected unless it was nested like that.
Actually, a better way is to use DISTINCT.
I had a situation to add new two fields into existing stored procedure, in a way that a value for that new fields had been obtained by a LEFT JOIN, and because it may have contained a NULL value, a single "concat" value was multiplicated for some cases more than a 100 times.
Because, a group with that new field value contained many NULL values, GROUP_CONCAT exceeded maximum value (in my case 16384).

sql - remove char from all names of objects in database

I am trying to remove name_ part of each name in database, name_ is mistakenly inserted into db and now in 30 object names. if i remove manuelly from db, it takes me much time.
one example is: name_john. it should be john.
how can i delete this name_ from all names of all objects in db with some sql statement?
If it are column values you can do it with an update statement.
UPDATE table_reference
SET column_reference = SUBSTRING(column_reference, 6)
WHERE column_reference LIKE 'name\_%' ESCAPE '\'
If this is about column values that you need to modify, you could use the REPLACE() function like this:
UPDATE tablename
SET columnname = REPLACE(columnname, 'name_', '')
WHERE columnname LIKE '%name\_%' ESCAPE '\'
;
That would remove all entries of name_ in columnname. If there can be no more than one entry (or if only one needs to be removed) and its position is fixed, you could use the INSERT() function instead, which, despite its name, can also replace and delete substrings. This is how you could use it if the position of name_ was e.g. at the beginning:
UPDATE tablename
SET columnname = INSERT(columnname, 1, 5, '')
WHERE columnname LIKE 'name\_%' ESCAPE '\'
;