Replacing words in a string - sql-server

I would like to replace words in a string using mssql.
Query:
UPDATE VS_48538
SET name = REPLACE(name, '%[N]eptun[ ][-]kód%', 'Neptunkód')
WHERE name LIKE '%[N]eptun[ ][-]kód%'
What I'd like to change is cc neptun -kód dd, (just this part: neptun -kód --> Neptunkód). Replace is not working.

Here's what worked for me:
replace(name,N'neptun -kód',N'Neptunkód')
Credits to #George Menoutis.

Related

Update with wildcard

in need to update all the rows that have sub string like this:
'forcestartpage=xx'and replace it with 'forcestartpage=18'
* there is lots of characters before and after the substring that shouldnt change
tried this, doesnt work:
update t_reminderscont
set body = REPLACE (body,'forcestartpage=__','forcestartpage=18')
thanks
You can get away with a STUFF function:
SELECT
T.body,
Replaced = STUFF(
T.Body, -- Insert in T.Body
CHARINDEX('forcestartpage=', T.Body), -- ... at the position where 'forcestartpage=' starts
LEN('forcestartpage=18'), -- ... while replacing 17 characters
'forcestartpage=18') -- ... the value forcestartpage=18
FROM
YourTable AS T
WHERE
T.body LIKE '%forcestartpage=__%' AND
T.body NOT LIKE '%forcestartpage=18%'
However this will only work for the first appearance of the forcestartpage= on each row.

How can perform an update to replace one or more of: ";to " with "; " in a column

I have a [Phrase] table with a column named [English]. The data in this table can look like this:
"zzz; to abc; to def" needs to be changed to: "zzz; abc; def"
"zzz; to xxx" needs to be changed to: "zzz; xxx"
I think I can do this with a regular expression if I was to put this into a string but is there any way that I can do this inside a SET part of an UPDATE? If not does anyone have another suggestion as to how I could do this?
A simple replace should work. https://msdn.microsoft.com/en-us/library/ms186862.aspx
REPLACE(YourColumn, '; to ', '; ')
REPLACE should do the trick
UPDATE Table
SET FieldValue = REPLACE(FieldValue, 'TO ', '')
WHERE....
Use REPLACE function
select replace(Yourcolumn,'; to',';') as Replaced_column
From yourtable

Output a database and its string

I get an error where it states invalid column 'DatabaseSQL' when I try to add a database reference into an OUTPUT statement like below:
OUTPUT [DatabaseSQL] +'.dbo.Package' 'TableName', 'PackageID', inserted.PackageId,
Core.updXMLFragment('StatusID', inserted.StatusID, Deleted.StatusID)
INTO #OutputList
If I remove the DatabaseSQL and added it as a string like OUTPUT 'Database.dbo.Package..., then it works fine, but I need the statement to actually recognise the database as well as add it as a string, just outputting it as a string alone isn't good enough. Any ideas?
You could use: DB_NAME():
OUTPUT DB_NAME() + '.dbo.Package', 'TableName', 'PackageID' ...
If you store DB name in variable use:
DECLARE #DatabaseSQL SYSNAME = QUOTENAME('name with space');
OUTPUT #DatabaseSQL + '.dbo.Package', 'TableName', 'PackageID' ...
LiveDemo

SQL finding value

I would like to find F20300000000 in this string:
0xE90300000000EA0300000000EB0300000000EC0300000000ED0300000000EE0300000000EF0300000000F00300000000F10300000000F20300000000F30300000000F40300000000F60300000000F70300000000E90B00000000010C000000000D0C000000003E0C000000005E0C000000005F0C00000000630C00000000811B000000008B1B00000000951B000000009F1B00000000A91B00000000B31B00000000BD1B00000000C71B00000000
I've used already the wildcard like
LIKE '%F20300000000%' then I didn't get any result.
to make it clear, when my condition is true then it will show the name of the person who has the F20300000000 in their field, so my problem now is it seems I don't know how to find F20300000000 from the given value.
my query:
select C.Name
FROM
[SERVER01].[dbo].[character_table] AS C,
[SERVER01].[dbo].[achievement] AS T
WHERE C.CharacterIdx = T.CharacterIdx and T.AchievementData LIKE '%F20300000000%';
AchievementData Data Type is varbinary(4800)
Your column is likely to be binary so you should cast it to string:
select C.Name
FROM
[SERVER01].[dbo].[character_table] AS C,
[SERVER01].[dbo].[achievement] AS T
WHERE C.CharacterIdx = T.CharacterIdx and CONVERT(varchar(max), T.AchievementData, 2) LIKE '%F20300000000%';

bad use of variables in db.query

I'm trying to develop a blog using webpy.
def getThread(self,num):
myvar = dict(numero=num)
print myvar
que = self.datab.select('contenidos',vars=myvar,what='contentTitle,content,update',where="category LIKE %%s%" %numero)
return que
I've used some of the tips you answer in this web but I only get a
<type 'exceptions.NameError'> at /
global name 'numero' is not defined
Python C:\xampp\htdocs\webpy\functions.py in getThread, line 42
Web GET http://:8080/
...
I'm trying to make a selection of some categorized posts. There is a table with category name and id. There is a column in the content table which takes a string which will be formatted '1,2,3,5'.
Then the way I think I can select the correct entries with the LIKE statement and some %something% magic. But I have this problem.
I call the code from the .py file which builds the web, the import statement works properly
getThread is defined inside this class:
class categoria(object):
def __init__(self,datab,nombre):
self.nombre = nombre
self.datab = datab
self.n = str(self.getCat()) #making the integer to be a string
self.thread = self.getThread(self.n)
return self.thread
def getCat(self):
'''
returns the id of the categorie (integer)
'''
return self.datab.select('categorias',what='catId', where='catName = %r' %(self.nombre), limit=1)
Please check the correct syntax for db.select (http://webpy.org/cookbook/select), you should not format query with "%" because it makes code vulnerable to sql injections. Instead, put vars in dict and refer to them with $ in your query.
myvars = dict(category=1)
db.select('contenidos', what='contentTitle,content,`update`', where="category LIKE '%'+$category+'%'", vars=myvars)
Will produce this query:
SELECT contentTitle,content,`update` FROM contenidos WHERE category LIKE '%'+1+'%'
Note that I backquoted update because it is reserved word in SQL.

Resources