Transact SQL replace part of string - sql

Is it possible to delete part of string using regexp (or something else, may be something like CHARINDEX could help) in SQL query?
I use MS SQL Server (2008 most likely).
Example: I have strings like "[some useless info] Useful part of string" I want to delete parts with text in brackets if they are in line.

Use REPLACE
for example :
UPDATE authors SET city = replace(city, 'To Remove', 'With BLACK or Whatever')
WHERE city LIKE 'Salt%'; // with where condition

You can use the PATINDEX function. Its not a complete regular expression implementation but you can use it for simple things.
PATINDEX (Transact-SQL)> Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.
OR You can use CLR to extend the SQL Server with a complete regular expression implementation.
SQL Server 2005: CLR Integration

SELECT * FROM temp where replace(replace(replace(url,'http://',''),'www.',''),'https://','')='"+url+"';

You can use STUFF to insert a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
For example, the code below, replaces the 5 with 666666:
DECLARE #Variable NVARCHAR(MAX) = '12345678910'
SELECT STUFF(#Variable, 5, 1, '666666')
Note, that the second argument is not a string, it is a position and you are able to calculate it position using CHARINDEX for example.
Here is your case:
DECLARE #Variable NVARCHAR(MAX) = '[some useless info] Useful part of string'
SELECT STUFF(
#Variable
,CHARINDEX('[', #Variable)
,LEN(SUBSTRING(#Variable, CHARINDEX('[', #Variable), CHARINDEX(']', #Variable) - LEN(SUBSTRING(#Variable, 0, CHARINDEX('[', #Variable)))))
,''
)

Finally helps REPLACE, SUBSTRING and PATINDEX.
REPLACE(t.badString, Substring(t.badString , Patindex('%[%' , t.badString)+1 , Patindex('%]%' , t.badString)), '').
Thanks to all.

Related

How to trim string (with Ideographic space U+3000) in sql server?

I have to trim Japanese characters string which has double byte space at start of string and end of string.
I have to do this by procedure of SQL server 2016.
For Example,
SELECT LTRIM(RTRIM(' A A '))
above one is working perfect
But Problem is in bellows line
SELECT LTRIM(RTRIM(' A A '))
i want output of above one is 'A A'
Have any idea, how to do this ?
Adapted SQL from OP's post:
SELECT LTRIM(RTRIM(REPLACE(' A A ', ' ', ' ')))
Screenshot with result:
The space in that string is the Ideographic space (U+3000) Unicode character, which LTRIM and RTRIM don't recognize as whitespace. Even TRIM in SQL Server 2017 won't recognize it unless it's specified explicitly.
Another problem is that this character is outside the normal range of characters and can't appear in a varchar field or value. This leads to inconsistent results between SQL Server versions. In SQL Server 2014 it will even appear as a ?. In later versions LTRIM/RTRIM may or may not work without emitting the error character. I don't have access to all versions to test this.
In SQL Server 2017 it's possible to explicitly specify the trimmed character, eg :
select trim(N' ' from N' A A ')
This produces A A.
In previous versions, PATINDEX can be used to find the locations of the first and last non-space positions :
declare #str nvarchar(10)=N' A A ';
declare #start int=PATINDEX(N'%[^ ]%',#str)
declare #end int=PATINDEX(N'% ',#str)
SELECT SUBSTRING(#str,#start,#end-#start)
The pattern N'%[^ ]%' finds the first non-U+3000 character in the string. N'% ' finds the position of the last one. SUBSTRING(#str,#start,#end-#start) extracts the content between the two positions.
The result is:
A A
I got solution
Thank you so much for your efforts.
Please use this function for double byte space remove.
CREATE FUNCTION [RTRIMBYTE](#AV_VALUE NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #AV_RETURN NVARCHAR(MAX) = #AV_VALUE;
WHILE DATALENGTH(#AV_RETURN) > 0 AND RIGHT(#AV_RETURN, 1) in (' ', ' ')
SET #AV_RETURN = LEFT(#AV_RETURN, LEN('X' + #AV_RETURN + 'X') -3 ) ;
RETURN #AV_RETURN;
END;

How to update without the last part of a string

Consider this string:
http://kliks.affiliate4you.nl/?adv=17847&web=1426&subid=4083&url=http%3A%2F%2Fwww.bartsmit.com%2Fshop%2Fnl%2Fbsnl%2Fpaw-patrol%2Fpaw-patrol-racers-pup-zuma**%3Fchannel_code%3D83%26product_code%3D94193039%26referer%3Da4you&linkinfo=czA4YBartSmit**
I have thousand of strings like this in a database field.
I want to delete the bold part of the string.
I know that it starts with %3Fchannel_code
I know it ends with czA4YBartSmit
Between those two it is different in each record
I want to do something like: update table set string = (string, but without the part starting with %3Fchannel_code and ending with czA4YBartSmit)
You'll be able to update the rows in the table using a combination of CHARINDEX to locate the start position, and LEFT to truncate the string.
You can use '3Fchannel_code' and 'czA4YBartSmit' as a filter criterion to limit the update to just strings which contains, and ends in these terms respectively. Also, % is used as a wildcard when used with LIKE, so needs to be escaped with [%].
BEGIN TRAN;
UPDATE MyTable
SET [string] =
LEFT([string], charindex('%3Fchannel_code', [string]) - 1)
FROM MyTable
WHERE [string] like '%[%]3Fchannel_code%czA4YBartSmit'
-- Check the update with a SELECT
-- If you are Happy, COMMIT, otherwise ROLLBACK
SqlFiddle here
There's a good overview of doing string manipulation with LEFT, RIGHT and CHARINDEX on MSDN here
Caveat: Before updating data in this way, recommend that you wrap the update in a transaction, just in case.
use replace, substring, charindex this works for even when the removed string is not at the end or start:
update table
set string =
replace(string ,
substring(
string ,charindex('%3Fchannel_code',string ),charindex('czA4YBartSmit',string ))
,'')
sqmple:
declare #str varchar(500)='http://kliks.affiliate4you.nl/?adv=17847&web=1426&subid=4083&url=http%3A%2F%2Fwww.bartsmit.com%2Fshop%2Fnl%2Fbsnl%2Fpaw-patrol%2Fpaw-patrol-racers-pup-zuma%3Fchannel_code%3D83%26product_code%3D94193039%26referer%3Da4you&linkinfo=czA4YBartSmit'
select replace(#str,
substring(#str,charindex('%3Fchannel_code',#str),charindex('czA4YBartSmit',#str))
,'')
Output:
http://kliks.affiliate4you.nl/?adv=17847&web=1426&subid=4083&url=http%3A%2F%2Fwww.bartsmit.com%2Fshop%2Fnl%2Fbsnl%2Fpaw-patrol%2Fpaw-patrol-racers-pup-zuma

SQL: Finding dynamic length characters in a data string

I am not sure how to do this, but I have a string of data. I need to isolate a number out of the string that can vary in length. The original string also varies in length. Let me give you an example. Here is a set of the original data string:
:000000000:370765:P:000001359:::3SA70000SUPPL:3SA70000SUPPL:
:000000000:715186816:P:000001996:::H1009671:H1009671:
For these two examples, I need 3SA70000SUPPL from the first and H1009671 from the second. How would I do this using SQL? I have heard that case statements might work, but I don't see how. Please help.
This works in Oracle 11g:
with tbl as (
select ':000000000:370765:P:000001359:::3SA70000SUPPL:3SA70000SUPPL:' str from dual
union
select ':000000000:715186816:P:000001996:::H1009671:H1009671:' str from dual
)
select REGEXP_SUBSTR(str, '([^:]*)(:|$)', 1, 8, NULL, 1) data
from tbl;
Which can be described as "look at the 8th occurrence of zero or more non-colon characters that are followed by a colon or the end of the line, and return the 1st subgroup (which is the data less the colon or end of the line).
From this post: REGEX to select nth value from a list, allowing for nulls
Sorry, just saw you are using DB2. I don't know if there is an equivalent regular expression function, but maybe it will still help.
For the fun of it: SQL Fiddle
first substring gets the string at ::: and second substring retrieves the string starting from ::: to :
declare #x varchar(1024)=':000000000:715186816:P:000001996:::H1009671:H1009671:'
declare #temp varchar(1024)= SUBSTRING(#x,patindex('%:::%', #x)+3, len(#x))
SELECT SUBSTRING( #temp, 0,CHARINDEX(':', #temp, 0))

Why does CHARINDEX function return an index for 'Œ' in string 'manoeuvre'?

I have this SQL code
declare #s varchar(8000) = 'manoeuvre'
select CHARINDEX(char(140), #s, 0)
char(140) = Œ, which dose not exist in the string 'manoeuvre'.
yet SQL server returns the following
4 (indicating it had located the char(140) on this line)
if I replace 'Œ' with a '*' I get
man*uvre
it seem like SQL has replaced the 'o' and 'e' with the one character, but why?
why is is replacing 'oe' with 'Œ'?
the same effect can be see with the string 'mass' and 'ß' (which I believe is German for double s). replacing on this character returns the sting 'ma*'.
Is SQL trying to do something "smart" under the covers?
EDIT
Extra information:
SQL server 2008 R2.
collation of database is Latin1_General_CI_AS.
If you look up that sign (ASCII 140) it is described as
capital OE ligature
See www.table-ascii.com for instance
try
select CHARINDEX(char(140), #s COLLATE Latin1_General_BIN, 0)
which will do a binary search.

SQL Query: data with comma

Im using sql server 2005. i want data with comma. for example ['5000'],['5001'],..
but the last record should not include comma. Pls help me.
Query:
select '['''+convert(varchar,parcelid)+'''],' from sampletable
Try the COALESCE function
SELECT #groupedText = COALESCE(#groupedText, '') + [Text] + ','
FROM Requirement
WHERE CampaignId = #campaignId
ORDER BY [Text]
Then you could try one of the string functions to kill the end comma
T-SQL string functions
You can use regular expressions to remove the last comma or do it using your programming language (ASP etc. like a chop function or something).
http://weblogs.sqlteam.com/jeffs/archive/2007/04/27/SQL-2005-Regular-Expression-Replace.aspx
Consider using XML for this purpose. The "aggregate concatenation" solution may not be reliable, because it is not clearly documented and supported. You can get rid of the final comma with SUBSTRING, as boon suggested.
See this thread.