SQL get all records which contains invalid character for e-diaeresis - sql

I have some values in table which text contains A« instead of ë
How to replace those characters with ë in SQL ?
I tried with
SELECT *
FROM [dbo].[Table]
WHERE [dbo].[Table].CustomValue LIKE '%A«%'
How to update those items in query ?

If you want just to SELECT the data then
SELECT *, REPLACE(CustomValue, 'A«', N'ë')
FROM [dbo].[Table]
WHERE [dbo].[Table].CustomValue LIKE '%A«%'
If you really needs to UPDATE the data then
UPDATE [dbo].[Table]
SET CustomValue = REPLACE(CustomValue, 'A«', N'ë')
WHERE [dbo].[Table].CustomValue LIKE '%A«%'

An UPDATE statment with the REPLACE function should do the trick.
UPDATE [dbo].[Table]
SET CustomValue = REPLACE(CustomValue, 'A«', 'ë')
WHERE CustomValue LIKE '%A«%'

Maybe you are looking just for
update dbo.table
set customvalue = replace(customvalue, 'A«', 'ë')
where customvalue like '%A«%';
But maybe your collation does not allow the character 'ë'. In that case you'd have to change the column definition first. E.g.
alter table dbo.table
alter column customvalue nvarchar(1000) collate latin1_general_cs_as;

Related

How to update a SQL Server table that has a column defined as TEXT with UPDATETEXT

How to update a SQL Server table that has a column defined as TEXT with UPDATETEXT
I have tried
UPDATETEXT db.tablename
SET ColumnName = ColumnName (‘data’)
WHERE UserID = ‘myID’
I get an error:
Incorrect syntax near the keyword 'SET'.
It works fine if I use NVARCHAR
UPDATE
SET...
WHERE..
Any help will be greatly appreciated.
Your syntax is all off. A correct usage of UPDATETEXT would look like this
DECLARE #ptrval BINARY(16);
SELECT #ptrval = TEXTPTR(t.ColumnName)
FROM tablename t
WHERE UserID = 'myID';
IF #ptrval IS NOT NULL
UPDATETEXT tablename.ColumnName #ptrval 0 NULL 'data';
However, you don't actually need it at all, as you are replacing the whole value. It's only really useful if you want to do a partial update on a big value. You might as well do a normal update.
UPDATE tablename
SET ColumnName = 'data'
WHERE UserID = 'myID';
db<>fiddle
Ideally, you wouldn't use text or ntext at all, as it's deprecated.
ALTER TABLE tablename ALTER COLUMN ColumnName varchar(max);
Then you can still do that UPDATE, or if you want a partial update you can use .WRITE
UPDATE tablename
SET ColumnName.WRITE('data', 0, NULL)
WHERE UserID = 'myID';
db<>fiddle

SQL Replace only a part of a path

I have a table (about 160k rows) with a column called paths.
In that column there are paths like:
"\\ab.local\folder1\folder2\folder3\folder_x"
"\\ab.local\folderA\"
The length of the paths differ.
What I would like to do is to replace only the "ab" before the .local in to "cd" and leave the rest untouched.
I have been told to use a replace function, but somehow I don't get it to work the way I want to.
I am looking for the right syntax to do this.
Declare #oldval as varchar(30) = '\\ab.local\' ;
Declare #newval as varchar(30) = '\\cd.local\' ;
update yourtable set yourfield = replace(yourfield,#oldval ,#newval) where yourfield like #oldval + '%'
Solved Reference : https://stackoverflow.com/a/814551/6923146
Hope it's perfect for your solution
UPDATE my_table
SET columnName = replace(columnName, 'oldstring', 'newstring')
WHERE columnName like '%oldstring%'
For example:
UPDATE my_table
SET columnName = replace(columnName, '\ab.', '\ab.')
WHERE columnName like '%\ab.%'
If the part to replace is on a fixed position with a fixed length you could use STUFF, like so:
UPDATE yourTable SET paths = STUFF(paths, 3, 2, 'cd')
This replaces two characters in paths beginning at position 3 with cd

Update table to add a literal value to a column

I am trying to update/replace column values in the table with hard coded value.
value = "c:\temp\"
This:
COLUMN
file.txt
file1.txt
Should become this:
FINAL COLUMN
c:\temp\file.txt
c:\temp\file1.txt
Attempted solution:
SELECT REPLACE(t.column, t.column, 'c:\temp't.column)
FROM TABLE t
Is this correct logic? Do we have another function I can use?
Assuming Oracle:
If you want to change the values in the table permanently you can just run an update query:
update your_table
set your_column = 'C:\temp\' || your_column;
Sample SQL Fiddle
If you're using MS SQL you can do concatenation like this:
MS SQL (all versions?):
update your_table
set your_column = 'C:\temp\' + your_column;
MS SQL 2012 and later:
update your_table
set your_column = concat('C:\temp\',your_column);
Do a UPDATE statement like below in case you want to change it permanently
update table1 set [column] = 'c:\temp\' + [column];
Else, if you just want to display it that way then SELECT query should be
select 'c:\temp\' + [column] as new_col
from table1
NOTE: above code syntax is for SQL Server. Not sure since you tagged as tsql

How to replace a value in SQLdata table

I have a table name Image there is two column with Name & Url all the rows in url having same value and starting in that some values are same i want to update the in one query...
For example...
http://farm3.staticflickr.com/2854/10380193164_9b65e4c5ed_n.jpg
I want to replace this and want to add a folder name like pages after the .com/
http://farm3.staticflickr.com/pages/2854/10380193164_9b65e4c5ed_n.jpg
How to do this ?
Please try using REPLACE:
declare #var nvarchar(max)='http://farm3.staticflickr.com/2854/10380193164_9b65e4c5ed_n.jpg'
select REPLACE(#var, '.com/', '.com/pages/')
i.e. for selecting from a table, try
SELECT
REPLACE(ColumnName, '.com/', '.com/pages/') as ColumnName
FROM YourTable
To UPDATE table, use query:
UPDATE YourTable
SET ColumnName=REPLACE(ColumnName, '.com/', '.com/pages/')
#skgacharya, I hope the query for update was already given by #Techdo. Try this...
UPDATE YourTable_name
SET yourURLColumn_Name=REPLACE(yourURLColumn_Name, '.com/', '.com/pages/')

Vietnamese characters in sql select like

When I search with the following query
Select * From [table] WHERE Name like '%Hà Nội T&T%'
in my mssql database I get no results, even though I'm sure it exists in the db.
I seem to have trouble with characters like ộ, ẫ, and Đ.
I have tried changing collation-settings but nothing helps.
Any suggestions?
Try:
Select * From [table] WHERE Name like N'%Hà Nội T&T%'
Try changing connection encoding using:
SET character_set_client = charset_name;
Also please do not use Latin1 encoding, try switching to UTF8. Here is a FAQ for Vietnamse Unicode: http://vietunicode.sourceforge.net/main.html
You need to alter your column containing Vietnamese characters.
ALTER TABLE [table] ALTER COLUMN name NVARCHAR(100) COLLATE Vietnamese_CI_AS
And then do
SELECT * FROM [table] WHERE name LIKE '%Hà Nội%'
SQL FIDDLE DEMO