I have a text like following on a database field (SQL Server 2000)
"Led sledding leding led go led"
I want SQL Command to replace the word led to LED, But it shouldn't change words like "sledding" / "leding"
There are 15,000 records with similar text. Need to apply this for all of them.
I have tried following but it takes more than 24 hours. (With in a cursor)
update rprd
set dsc = replace(dsc, 'led ', 'LED ')
where dsc not like 'LED %' collate sql_latin1_general_cp1_cs_as
and dsc like 'led %'
update rprd
set dsc = replace(dsc, ' led ', ' LED ')
where dsc not like '% LED %' collate sql_latin1_general_cp1_cs_as
and dsc like '% led %'
update rprd
set dsc = replace(dsc, ' led', ' LED')
where dsc not like '% LED' collate sql_latin1_general_cp1_cs_as
and dsc like '% led'
Please suggest me a faster and simple way of doing this.
You didn't (edit: didn't initially) specify which database you are using.
Most database vendors have a function with general syntax like, or very close to REPLACE( source-string, from-string, to-string ), but the syntax will vary in terms of what kinds of wildcards you can use or whether you can use regular expressions, and case-sensitivity differs among vendors with regard to object names and string lookups. However, replacing a string with a string of a specific case will work with every vendor.
For your first pass, you might try something as simple as replacing ' led ' (led with a space on either side of it), like this:
REPLACE( somefield, ' led ', ' LED ' )
TSQL does support some modestly advanced wildcard searches: https://msdn.microsoft.com/en-us/library/ms179859.aspx
I take it, you just need to correct some existing data once, and you dont need a solution to use constantly.
so why dont you run a few queries that solve your problem easily and quickly,
UPDATE table SET field=regexp_replace(field, '^led ', 'LED ');
UPDATE table SET field=regexp_replace(field, ' led$', ' LED');
UPDATE table SET field=regexp_replace(field, ' led ', ' LED ');
make sure to check your db docs on correct syntax of your functions
Related
I would like to extract rows that has 'venture' in the Name Column as shown below.
The following SQL code is used to get that result
CASE
WHEN summary.cust_analysis.Name LIKE '%VENTURE%'
However, how can I extract only the first row which has 'Venture' as a word instead of having it as a part of a word like Bonaventure?
If I remove the '%' from the SQL code non of the rows will get extracted.
Appreciate all your help. Thank you :)
CASE WHEN summary.cust_analysis.Name LIKE '% VENTURE %'
or
CASE WHEN summary.cust_analysis.Name LIKE '% VENTURE %'
OR summary.cust_analysis.Name LIKE '% VENTURE'
OR summary.cust_analysis.Name LIKE 'VENTURE %'
OR summary.cust_analysis.Name = 'VENTURE'
with due concern for upper/lower case presumably too
If you are using SQL Server you can use Regular Expressions.
So you can match entire word followed or preceded by another symbols like dot or comma:
CASE WHEN Name LIKE '%[^A-Z]Venture[^A-Z]%'
OR Name LIKE 'Venture[^A-Z]%'
OR Name LIKE '%[^A-Z]Venture'
OR Name = 'Venture'
This will match ,Venture, Venture., Venture:
More info here
The simplest method is to prepend and postpend the string with spaces:
where concat(' ', summary.cust_analysis.Name, ' ') like '% VENTURE %'
You can use the same logic in a CASE expression:
select (case when concat(' ', summary.cust_analysis.Name, ' ') like '% VENTURE %'
then 'VENTURE'
end)
Note: This uses the CONCAT() function for string concatenation. The SQL Standard operator is || and some databases have other methods.
I have a database backup that I restored in SQL Server 2014 and I am using SQL Server Management Studio 18 (15.0.18206.0)
In a column of type text strings of XML files are stored. Yes, I know that the datatype text is obsolete. But I cannot change the datatype in the old program so easily.
Now I want to remove the string <SCHNITTLINIE>.
In the cell, it looks like this: ..._MIN> <SCHNITTLINIE> <S...
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), '<SCHNITTLINIE>', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'.
As a result, I get: ...MIN> <S...
So far so good.
I'd like to remove the "spaces" as well.
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), ' <SCHNITTLINIE> ', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'
Now he finds the string ' ' but not anymore.
At the beginning of the cell is a <?xml version="1.0". So I tried the following to test the spaces:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml ', 'xxx') AS TEXT)
FROM .[dbo].[XMLdatei].
WHERE xml LIKE '%SCHNITT%'.
As a result I got the following: <?xxxversion="1.0"
The following also works:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml' + char(32), 'xxx') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITT%'
Check, the test was successful. :)
Only with the string ' <SCHNITTLINIE> ' this does not work.
How can I find out what the characters before < or after > are?
How can I remove them without knowing what the characters are?
Thanks for your help.
Have a nice weekend.
Christoph
I have looked at these somewhat related articles for ideas, and did a number of searches like "sqlite ltrim %" but I'm having a surprisingly hard time figuring out what I'm doing wrong.
What I want to do is remove everything before, and including a space in SQLite.
One major issue is that I have to use SQLite from PortableApps, and cannot use MySQL for this.
I tried:
UPDATE `temp_Week17` SET `c_name`=ltrim(%,' ');
I was thinking I could trim the space from the front easily enough after, but I get a "near '%': syntax error."
Just to see what would happen, I also tried:
UPDATE temp_Week17
SET c_name = LEFT(c_name, CHARINDEX(' ', c_name) - 1)
WHERE CHARINDEX(' ', c_name) > 0
And that produces a "near '(': syntax error."
Another one I tried was:
UPDATE temp_Week17 SET c_name = ltrim(c_name, ' ');
That one runs succesfully but does not change anything.
Updated to add per #scaisEdge:
The column is arranged as so:
|c_name|
--------
|John Smith|
|Abe Lincoln|
|Taco Cat|
And the desired outcome is:
|c_name|
--------
|Smith|
|Lincoln|
|Cat|
Thanks very much for any help!
You should concatenate the two string (the trimmed and the space)
UPDATE temp_Week17 SET c_name = ltrim(c_name) || ' ' ;
or you need the space before the trimmed
UPDATE temp_Week17 SET c_name = ' ' || ltrim(c_name) ;
based on the sample provide seems you need substring
UPDATE temp_Week17 SET c_name = ltrim(substr( c_name, instr(c_name, ' '))) ;
I have a Text,
'Me and you against the world' // false
'Can i have an email address' // true
'This is an' // true
'an' //true
I want to check whether the word an is inside my String.
How do I check if a text contains a specific word in SQL? I can't add a full-text catalog. Otherwies i could
SELECT * FROM TABLE WHERE CONTAINS(Text, 'an')
Here's one approach.
DECLARE #table_name table (
column_name varchar(50)
);
INSERT INTO #table_name (column_name)
VALUES ('Me and you against the world')
, ('Can i have an email address')
, ('This is an')
;
SELECT column_name
FROM #table_name
WHERE ' ' + column_name + ' ' LIKE '% an %'
;
There are some way to do this, seem you want find a word and not a part of a word, so you can do in easy way with like operator
You can have 3 cases to found a word
'space'WORD
WORD'space'
'space'WORD'space'
SELECT * FROM TABLE WHERE Field like ' an' OR Field like 'an ' OR
Field like ' an '
Hope it helps
It is perfectly done in MS SQL Server by the CHARINDEX function (it is internal to MS SQL):
if CHARINDEX('an ',#mainString) > 0
begin
--do something
end
The solution was showed before in another post.
The three cases you'll encounter as Luka mentions:
Space before word
Space after word
Space before and after word
To accomplish this, you'll write a query like the following which searches for the whole word, and pads the expression to search with a leading and trailing space to capture words at the start/end of the expression:
Note: I've used a contrived example to make this portable and demonstrable.
select
t.txt
from (
select
'this is an awesome test of awesomeness man' as txt
) t
where
charindex(' an ', ' ' + t.txt + ' ') > 0;
I'm trying to update a column that could possibly have a single space or multiple spaces into just one single space using a plain sql statement not pl sql
I could do it through update table set column_name='' where column_name like '% %'
However, there could be some data such as abc def in that column. I do not want to disturb the pattern of that data meaning if want to do it only when the column is filled with white space and not touch columns that have any data.
I would recommend using a regular expression to do this, both to do the replacement and to do the matching:
UPDATE mytable
SET mycolumn = REGEXP_REPLACE(mycolumn, '\s{2,}', ' ')
WHERE REGEXP_LIKE(mycolumn, '\s{2,}')
This will replace two or more consecutive whitespace characters (spaces, tabs, etc.) with a single space. If you just want to replace spaces and not tabs, carriage returns, or newlines, use the following:
UPDATE mytable
SET mycolumn = REGEXP_REPLACE(mycolumn, ' {2,}', ' ')
WHERE REGEXP_LIKE(mycolumn, ' {2,}')
The reason for using {2,} is so that we don't bother replacing spaces where it need not be done.
With regular expression:
update table set column=regexp_replace(column, ' +', ' ')
Try:
update mytable
set col = ' '
where replace (col, ' ', null) is null;
I have used regexp_like to solve this
update table set column= ' '
where column in (select column from table where regexp_like('column','^\s+$')
Thanks
Try this:
update product set name = replace(name, ' ', ' ') where name like '% %';
where the second parameter of replace expression and like ('% %') contains two blank spaces.
In my case, the result was:
TENIS NIKE AIR => TENIS NIKE AIR
TENIS NIKE MAN => TENIS NIKE MAN