How to update without the last part of a string - sql

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

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;

Sqlite - How to remove substring in a field using wildcards

I would like to remove all occurrences of a certain string pattern across all fields in a table.
For example, find all occurrences of the pattern "<XY>*<Xy>" where * represents any possible configuration of characters. I want to remove just those substrings and leave the remainder of the string intact.
This is an example of what I would like to use as my SQL command, but of course this doesn't work:
UPDATE Table SET Field = replace(Field, '<XY>*<Xy>', '');
What is the solution?
Here is an option which attempts to splice around the <XY>...</XY> tags:
UPDATE yourTable
SET Field = SUBSTR(Field, 1, INSTR(Field, '<XY>') - 1) ||
SUBSTR(Field, INSTR(Field, '</XY>') + 5)
WHERE Field LIKE '%<XY>%</XY>%'
It updates fields containing this pattern with the concatenation of everything coming before the first <XY> and everything coming after the second </XY>.
Note that I used <XY>...</XY> rather than what you had originally, because INSTR() is not case sensitive, and both tags would appear as being the same thing.
Demo
The demo is for MySQL but the sytnax is almost identical to SQLite.

Replace Azerbaijani characters in string not works correct

When I want to replace Azerbaijani character 'ş' in my string with 'sh'.it works but it also replaces 's' with 'sh'
How can i solve it .Any ideas?
REPLACE(mystring,'ş','sh')
The character "ş" belongs to Turkish_CI_AS. It is a problem inserting them to database and retreiving them too. The trick is to use nvarchar and use N while inserting and querying.
Refer to the example below.
SELECT REPLACE(N'arshad khan earns 1000ş',N'ş','sh');
SELECT 'ş'
SELECT N'ş'
Output is as below
arshad khan earns 1000sh
s
ş
This character is represented as Unicode
http://en.wikipedia.org/wiki/%C5%9E
Use this article, to solve your problem
how to insert unicode text to SQL Server from query window
Regards
R
Only solution after research.
It happens in two characters 'ş' and 'ç' if you want to replace these characters with something else it will also replace 's' and 'c'.
Of course if your database collation is 'Turkish_CI_AS' it will work .But in my case only for two characters i could not change my database collation.No logic in it.
so my client just wanted from me change Azeri characters to latin 'ş'->'s'.
My solution when i start i replace 's' with special character and put it back after i replace all azeri characters to latin.so my original 'c' and 's' characters not effected after replacement.
This is a function I wrote
Create FUNCTION [dbo].[funRGMReplaceAzeriCharacters]
(
#string nvarchar (MAX)
)
RETURNS varchar(MAX)
AS
BEGIN
DECLARE
#Result nvarchar(MAX)
Begin
SET #Result=REPLACE(#string,'s' ,'V1986Q')
SET #Result=REPLACE(#Result,'c' ,'V1987Q')
SET #Result=REPLACE(#Result,'ı' ,'i')
SET #Result=REPLACE(#Result,'ə','a')
SET #Result=REPLACE(#Result,'ğ','g')
SET #Result=REPLACE(#Result,'ü','u')
SET #Result=REPLACE(#Result,'ş','sh')
SET #Result=REPLACE(#Result,'ç','ch')
Set #Result=REPLACE(#Result,'ö','o')
-- bring back s and c
Set #Result=REPLACE(#Result,'V1986Q','s')
Set #Result=REPLACE(#Result,'V1987Q','c')
END
RETURN UPPER (#Result)
END

Transact SQL replace part of string

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.

Trimmining a column with bad data

My data looks like
ID LPNumber
1 30;#TEST123
2 302;#TEST1232
How can I update MyText to drop everything before the # and including the #, so I'm left with the following:
ID LPNumber
1 TEST123
2 TEST1232
I've looked at SQL Server Replace, but can't think of a viable way of checking for the ";"
On the MSDN REPLACE page, the menu on the left gives the complete list of string functions available.
UPDATE
MyTable
SET
LPNumber = SUBSTRING(LPNumber, CHARINDEX('#', LPNumber)+1, 8000);
I'll let you work out (from MSDN) the filter needed in case there is no # in the column...
Edit:
Why 8000?
The longest non-LOB string length is 8000 so it is shorthand for "until end of string". You can use 2147483647 too for max columns or to make it consistent.
Also, LEN can bollix you.
SET ANSI_PADDING is ON by default
LEN ignores trailing spaces
You'd need to use DATALENGTH but then you need to know the data type because this counts bytes, not characters. See https://stackoverflow.com/a/2557843/27535 for example
So using a magic number is perhaps a lesser evil...
Use CHARINDEX(), LEN() and RIGHT() instead.
RIGHT(LPNumber, LEN(LPNumber) - CHARINDEX('#', LPNumber, 0))