Remove single characters in sql records? - sql

I'm working on importing some files and I noticed that some of the email addresses are prefixed with a comma.
Eg: ,abc#abc.com
In a table with 1500 emails, I've found that 700+ of these have this issue.
How would I update them so that the comma is removed?

UPDATE dbo.Table
SET Email = STUFF(Email, 1, 1, '')
WHERE Email LIKE ',%';

try:
UPDATE YourTable
SET YourColumn=RIGHT(YourColumn,LEN(YourColumn)-1)
WHERE LEFT(YourColumn,1)=','

If you know they always start with a ,:
UPDATE SomeTable
SET SomeColumn = SUBSTRING(SomeColumn, 2, 4000)
WHERE SomeColumn LIKE ',%';
Otherwise you could do this to get rid of all , no matter where they appear. Note that here adding a WHERE SomeColumn LIKE '%,%' clause might adversely affect performance.
UPDATE SomeTable
SET SomeColumn = REPLACE(SomeColumn, ',', '');

UPDATE table SET email = SUBSTRING(email, 2, 1000) WHERE email LIKE ",%";
The % is a wildcard, so this will only match those fields that start with a comma.

Related

How to delete 1 character in SQL

ID column has some values starting with ' and some do not. How can I use function to delete all ' in ID Column.
Thanks a lot!
You should provide your attempts next time, that shows you're actually trying to achieve something on your own but here you go:
update <your_table> t
-- desired columns go here
set t.id = regexp_replace(t.id, '^''{1}', '')
-- can be omitted if you want to scan all records
where t.id like '''%';
If you want to replace all quote characters you can do this:
update <your_table> t
-- desired columns go here
set t.id = replace(t.id, '''', '')
where t.id like '''%';

SQL server trimming string

I have a query, I want to Trim a column and get the only Right side values:
Here in the pic the result is like SubAccountCode and SubaccountName but user has entered code and name in SubaccountName. I want to trim the codes from Subaccountname and update the table. T
The query I have tried is mentioned below, but I think it's not so working:
Select Substring(Subaccountname,8,20)as Name from #temp
There are several ways to do this.
When looking at your example data the easiest way would be using an replace() in the update statement.
Syntax:
REPLACE ( string_expression , string_pattern , string_replacement )
Example:
UPDATE table_name
SET column2 = replace([column2], [column1], '')
What this does it updates the column2 with the value from column2 where the value from column1 is replaced with '' nothing. In your example this does leave you with an unwanted space in front. You could trim this or try as followed:
UPDATE Test
SET [SubAccountName] = replace([SubAccountName], [SubAccountCode] + ' ', '')
If the SubAccountCode could be different from the code in the SubAccountName and you only want to remove the first 8 characters (if you are sure it is always the first 8) you can use:
UPDATE YourTable SET SubAccountName = RIGHT(SubAccountName, LEN(SubAccountName) - 7)
Example script:
create table test (
SubAccountName varchar(100),
SubAccountCode varchar(100)
)
insert into test (SubAccountCode, SubAccountName) VALUES
(1234567, '1234567 AUBC' ),
(1234467, '1234467 AUBC' ),
(1235567, '1235567 AUBC' )
select * from test -- Check that the data is like your example.
UPDATE Test SET SubAccountName = RIGHT(SubAccountName, LEN(SubAccountName) - 8)
select * from test -- Check that the result is like your wanted result.
drop table test -- Cleanup the test table.
This code will work for you.
UPDATE TableName SET SubaccountName = REPLACE(LTRIM(RTRIM(SubaccountName)),LTRIM(RTRIM(SubAccountCode)),'')

How to replace duplicate words in a column with just one word in SQL Server

I have a few million strings that relate to file paths in my database;
due to a third party program these paths have become nested like below:
C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\
I want update the entries so that thirdparty\thirdparty\etc becomes \thirdparty.
I have tried this code:
UPDATE table
SET Field = REPLACE(Field, 'tables\thirdparty\%thirdparty\%\', 'tables\thirdparty\')
WHILE EXISTS (SELECT * FROM table WHERE Field LIKE '%\thirdparty\thirdparty\%')
BEGIN
UPDATE table SET Field = REPLACE(Field, '\thirdparty\thirdparty\', '\thirdparty\')
END
So do you want something like this?
SELECT SUBSTRING('tables\thirdparty\%thirdparty\%\',0,CHARINDEX('\','tables\thirdparty\%thirdparty\%\',0)) + '\thirdparty\'
OR
UPDATE table
SET Field = REPLACE(Field, Field, (SELECT SUBSTRING(Field,0,CHARINDEX('\',Field,0)) + '\thirdparty\'))
You can avoid using a loop with the following technique:
Update TABLE
SET Field = left(Field,charindex('*',replace(Field, 'thirdparty\', '*'))-1)+'thirdparty\'+right(Field,charindex('*',reverse(replace(Field, 'thirdparty\', '*')))-1)
Its too late, but I just guess if I want replace a single word in repeating multiple time same word as he want. This will replace all with append a single time '\thirdparty'...
Check this.
Declare #table table(Field varchar(max))
insert into #table values('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\1')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\2')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\3')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\4')
UPDATE #table
SET Field = SUBSTRING (Field, 1, CHARINDEX('\thirdparty', Field ) ) + 'thirdparty\'
--replace (Field , 'thirdparty\' ,'')
+ reverse( SUBSTRING ( REVERSE(Field), 1, CHARINDEX(reverse('\thirdparty'), REVERSE(Field) )-2 ) )
--REPLACE(Field, 'tables\thirdparty\%thirdparty\%\', 'tables\thirdparty\')
select * from #table

Replace every comma in a column with a dot

Could someone tell me a query to replace every comma from a column 'myColumn' with a dot, without messing up with the numbers?
You would use an update statement:
update t
set myColumn = replace(myColumn, ',', '.')
where myColumn like '%,%'
Pruebe con:
UPDATE prices SET pvp = REPLACE(pvp, ',', '.');

Update and append unless empty

Im trying to update a field by appending data to it.
if it contains the data already i wont update it otherwise I will.
if it already contains data i want it to append a comma and space followed by the word. e.g.
update myTable
set Prefixes = convert(nvarchar(max),Prefixes) + ', abc'
where MyCol='xyz' and Prefixes not like '%abc%'
im trying to get this to work so that if the prefixes column is empty initially it only includes the word 'abc'
and not ', abc'
How can i do this?
Sounds like you need a CASE:
update myTable
set Prefixes =
case
when Prefixes is null or Prefixes = ''
then 'abc'
else convert(nvarchar(max),Prefixes) + ', abc'
end
where MyCol='xyz' and (Prefixes not like '%abc%' or Prefixes is null)
See SQL Fiddle with Demo
You need to check for null values of Prefixes before filtering as NOT LIKE in the WHERE clause as well. Sql-Demo
update myTable
set Prefixes = isnull(nullif(rtrim(Prefixes),'') + ', abc','abc')
where MyCol='xyz' and isnull(Prefixes,'') not like ', abc%'