SQL Server Replace - sql

CREATE TABLE test (a VARCHAR(2000))
INSERT INTO test
VALUES ('This is a AB_CD_test.dbo.ABC'),
('This is a AB_CD.dbo.ABC')
Table before running Update query:
This is a AB_CD_test.dbo.ABC
This is a AB_CD.dbo.ABC
This is a [AB_CD].[dbo].[XYZ]
I am trying to replace the keyword AB_CD with AB_CD_test using this query:
UPDATE [US\AF83767].[test]
SET a = REPLACE(a, 'AB_CD', 'AB_CD_test')
WHERE a LIKE '%AB!_CD%' ESCAPE '!';
But as expected, data changes to:
This is a AB_CD_test_test.dbo.ABC
This is a AB_CD_test.dbo.ABC
This is a [AB_CD_test].[dbo].[XYZ]
The second is fine but the first should remain 'This is a AB_CD_test.dbo.ABC'.
Could anyone help with a query which does not change the first row and just looks for occurrences of 'AB_CD'.
Relatively new to SQL and would appreciate your help.

It's not exactly clear what you want to prevent. Do you only want to prevent the replacement when "Test" is present, or any string between "AB_CD" and ".dbo"
I'm guessing the latter, so I would suggest this:
UPDATE [US\AF83767].[test]
SET a = REPLACE(a, 'AB_CD', 'AB_CD_test')
WHERE a LIKE '%AB!_CD.%' ESCAPE '!';

I think this will do what you want:
UPDATE #test
SET a = REPLACE(REPLACE(a, 'AB_CD_test', 'AB_CD'), 'AB_CD', 'AB_CD_test')
WHERE a LIKE '%AB!_CD%' ESCAPE '!';
It replaces all occurrences of AB_CD_test to AB_CD... and then changes all AB_CD to AB_CD_test.

It appears that you want to replace instances of AB_CD ONLY WHEN it is followed by a period or a closing bracket or nothing at all.
where col like '%AB!_CD[.!]]%' ESCAPE '!' or col like '%AB!_CD' ESCAPE '!';

Related

Replacing characters in the oracle database value range

I have a database in an oracle, I filled in the "Number" fields with numbers starting from "A14602727" to "A14603000" but it turned out that I accidentally typed the symbol A (in Ukrainian) instead of A (in English). And now I when find the command:
select number from test where number 'А14602727'
...find me nothing. Is it possible somehow with the help of the command to replace all numbers from "A14602727" (Ukrainian) to "A14602727" (English) ?
I will be grateful for your help!)
You can use following trick to convert any character to its base character using accent-insesitive binary sorting.
select your_col,
utl_raw.cast_to_varchar2(nlssort(your_col, 'nls_sort=binary_ai')) converted_col
from your_table
Use it in update statement accordingly.
Cheers!!
You could use regexp_replace():
update test set number = regexp_replace(number, '^Ä', 'A');
Regexp '^Ä' represents character 'Ä' at the beginning of the string. I used 'Ä' to represent the A in Ukrainian: replace this with the correct character that you want to replace.
This can also be done, probably more efficiently, with substr and like:
update test set number = 'A' || substr(number, 2) where number like 'Ä%';
Use the simple Oracle REPLACE function:
UPDATE test
SET number = REPLACE(number, 'A', 'A');
Replace is taking 3 parameters:
String in which you will search for a string to replace (The number column)
The string you are searchin gto replace (Ukrainian A)
The string you will replace it with (English A)
Here you have a DEMO example.
Also please note that the query in your question:
select number from test where number 'А14602727';
Is not valid. It should be something like this:
select number from test where number like 'А14602727';
or like this:
select number from test where number = 'А14602727';
So first do check that!
Cheers!

Match Character Whether or Not It Exists in Like Statement

I need a like expression that will match a character whether or not it exists. It needs to match the following values:
..."value": "123456"...
..."value": "123456"...
"...value":"123456"...
This like statement will almost work: LIKE '%value":%"123456"%'
But there are values like this one that would also match, but I don't want returned:
..."value":"99999", "other":"123456"...
A regex expression to do what I'm looking to do is 'value": *?"123456"'. I need to do this in SQL Server 2008 and I don't believe there is good regex support in that version. How can I match using a like statement?
Remove the whitespace in your compare with REPLACE():
WHERE REPLACE(column,' ','') LIKE '%"value":"123456"%'
May need a double replace for tabs:
REPLACE(REPLACE(column,' ',''),' ','')
I don't think you can with the like operator. You could exclude ones you could match, like if you want to make sure it just doesn't contain other:
[field] LIKE '%value":%"123456"%` AND [field] NOT LIKE '%"other"%'
Otherwise I think you'd have to do some processing on the string. You could write a UDF to take the string and parse it to find the value for 'value' and compare based on that:
dbo.fn_GetValue([field], 'value') = '123456'
The function could find the index of '"' + #name + '"', find the next index of a quote, and the one after that, then get the string between those two quotes and return it.

drop characters from end of string

In SQL Server 2008, I need to drop the last two characters from a series of item numbers in our database. not all the numbers are the same format and i only need to drop characters from certain ones. the numbers i need to truncate look like this DOR-12345_X where _X is a revision letter. i tried this
SELECT LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2)
FROM IV00103
WHERE LEFT STR = 'DOR'
but it doesn't like the syntax near FROM or the STR = 'DOR'
Can anyone assist? Do you need more info? I'm really a newb at SQL =) THANKS!
-jon
I think the correct syntax is:
SELECT LEFT(IV00103.VNDITNUM, LEN(IV00103.VNDITNUM)-2)
FROM IV00103
WHERE LEFT(IV00103.VNDITNUM, 3) = 'DOR'
Or:
WHERE IV00103.VNDITNUM like 'DOR%'
It doesn't like the syntax near FROM because you're missing a closing bracket on the LEFT. I suspect what you want is something like this:
SELECT CASE WHEN IV00103.VNDITNUM LIKE 'DOR%' THEN LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2)) ELSE IV00103.VNDITNUM END AS VNDITNUM
FROM IV00103
That selects absolutely everything from the IV00103 table, removing those last two characters only from those that have a value in the VNDITNUM column beginning with DOR.
EDIT: If you want to actually update the contents of the table, you could do it this way using a WHERE:
UPDATE IV00103
SET VNDITNUM = LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2))
WHERE VNDITNUM LIKE 'DOR%'
If you only need the ones that match the pattern DOR-somecharacters_asinglecharacter then you should probably do:
UPDATE IV00103
SET VNDITNUM = LEFT(IV00103.VNDITNUM,(LEN(IV00103.VNDITNUM)-2))
WHERE VNDITNUM LIKE 'DOR-%\__' ESCAPE '\'
The \_ is an escaped underscore, which will be treated as an actual underscore. The ESCAPE '\' part tells SQL that the \ character is being used to escape special characters in the pattern. The second _ is the special character, and matches a single character.

Regex remove all occurrences of multiple characters in a string

In my PostgreSQL I want to replace all characters (;<>) occurrences in a string.
My query:
update table_name set text = regexp_replace(text, '/[(;<>)]+/g', '');
I think my regexp is wrong. Can anyone help me out with it?
Use the much faster translate() for this simple case:
UPDATE tbl SET text = translate(text, '(;<>)', '');
Every character in the second parameter that has no counterpart in the third parameter is replaced with nothing.
The regular expression solution could look like this:
regexp_replace(text, '[(;<>)]', '', 'g');
Essential element is the 4th parameter 'g' to replace "globally" instead of just the first match. The second parameter is a character class.
You were on the right track, just a matter of syntax for regexp_replace().
Hint on UPDATE
If you don't expect all rows to be changed, I would strongly advise to adapt your UPDATE statement:
UPDATE tbl
SET text = translate(text, '(;<>)', '')
WHERE text <> translate(text, '(;<>)', '');
This way you avoid (expensive) empty updates. (NULL is covered automatically in this particular case.)

Replacing an Unknown Value with SQL Replace

I'm probably missing something really obvious here, but this has been a bear to search for on Google (Maybe I don't have the right terminology).
I want to replace an unknown value with another value from a temp table. I know the length of the value so my thought was to use underscores as you would in a LIKE statement. The following DOES NOT work however:
UPDATE MyTable
SET Name =
Replace(Name, '__SomeString', TempTable.value + ' SomeString')
FROM MyTable INNER JOIN TempTable
ON Name LIKE TempTable.Name
This is MS SQL 2000 FWIW.
EDIT: To try and clarify it looks like the underscore '_' wildcard that is used in a LIKE statement is taken literally inside of the replace function. Is there another way?
Any thoughts?
UPDATE MyTable
SET Name =
CASE WHEN (Name like '_SomeString')
THEN TempTable.value + SUBSTRING(Name,2,LEN(Name)-1)
ELSE Name END
FROM MyTable INNER JOIN TempTable
ON MyTable.Name = TempTable.Name
WHERE MyTable.Name = 'TheNameToReplace' -- I don't know if it will be for a specific name hence the where...
This will then replace 'SomeString' in the Name field, with the value from TempTable.value
Is this what you were looking for or something else?
Perhaps you can use stuff instead of replace. You need to know the start position in the string where you want to replace the characters and you need to know the length of the expression that is to be replaced. If you don't know that perhaps you can use charindex or patindex to figure that out.
select stuff('A123', 1, 1, 'B ')
Result:
(No column name)
B 123
Would somethi8ng like this work?
UPDATE mytable
SET field1 = 'A' + SUBSTRING(field1,2,LEN(field1))
WHERE LEFT(field1) IN (0,1,2,3,4,5,6,7,8,9)
Apparently it is not possible to use wild cards in the REPLACE function. This is the closest match on SO that I could find: MySQL Search & Replace with WILDCARDS - Query While the link is for MySQL I believe it is true for MS SQL as well.
The other answers here are all creative solutions to the problem, but I ended up going the brute force route.