SQL UPPER only specific word - sql

Does anyone know how I can change only a word in the varchar field in my database? They are formatted like "Cheese", "cheese", "CheesE". I want to change to them all to CHEESE, but I have other text in those fields as well. I was wondering if their was a way to single that word out and make it uppper.
Something like
update table
set field = Upper(cheese)+rest of field
Thanks

update table
set field = replace(field, 'cheese', 'CHEESE')
SQLFIddle demo

From your comments I can see the diference between substring and word.
It is possible to do this kind of 'word-centric' transformation using XML as a proxy format.
Try this...
DECLARE #pattern nvarchar(max) = 'cheese'
SELECT
converted =
cast('<a><i>' + REPLACE(field, ' ', '</i><i>') + '</i></a>' as xml)
.query(
'for $x in /a/i return
if (not( ($x) is (/a/i[last()])[1] )) then
if (data($x) = sql:variable("#pattern")) then
concat(upper-case($x), "")
else
concat($x, "")
else
if (data($x) = sql:variable("#pattern")) then
string(upper-case($x))
else
string(data($x))')
.value('.', 'nvarchar(max)')
FROM table
I'm sure that it's possible to do it by parsing the expression char-by-char but I wanted to try it with XML :)

Related

String replacement with LIKE and %

my Settings table has 2 columns: Key, Value. Both are strings.
What I am trying to do is finding a smart way to create one single SQL statement that changes the string inside Value according to what's inside.
For example, Value can contain:
'wo, find, search'
OR
'find, wo, search'
OR
'find, search, wo'
What I want to do in all 3 cases is using one SQL script to replace 'wo' with an empty string. What I created so far is:
update Settings set Value = replace(Value, 'wo, ', '')
where Key = 'SelectFeature'
update Settings set Value = replace(Value, ', wo', '')
where Key = 'SelectFeature'
update Settings set Value = replace(Value, 'wo', '')
where Key = 'SelectFeature'
as you can see it's not very elegant. Can I somehow use the operator LIKE and % to achieve this? Thanks in advance!
Still not the best solution but a more optimal way would be to do
UPDATE
Settings
SET
Value = REPLACE(
REPLACE(
REPLACE(Value, 'wo, ', ''), ', wo', ''
), 'wo', ''
)
WHERE
Key = 'SelectFeature'
This way you only run one query where you do all the changes... Update queries cost a lot of process time...

How to remove text followed by a special characted in MSQL

I have a table named 'country' with a column named 'name'
and the names in this column appears with translation followed by special character '/'.
IRELAND/IRLANDE
GREECE/GRÈCE
DENMARK/DANEMARK
Now i want only the countrynames before this special character'/' so the out put should look like this..
IRELAND
GREECE
DENMARK
please help. Thanks in advance
create table #t(name nvarchar(40))
insert into #t values('IRELAND/IRLANDE')
,('GREECE/GRÈCE')
,('DENMARK/DANEMARK')
select substring(name,0,CHARINDEX('/',name)) from #t
Why LEN(names)-CHARINDEX('/',names)?
try using CHARINDEX('/',names)+1 instead.
As far as I remember, SUBSTRING's third variables should be the length of the desired output string.
A more complex query to help you out:
DECLARE #Text VARCHAR(50) = 'IRELAND/IRLANDE GREECE/GRÈCE DENMARK/DANEMARK'
SELECT
SUBSTRING(SUBSTRING(#Text,0,CHARINDEX(' ',#Text,0)),0,CHARINDEX('/',#Text,0))
+
SUBSTRING(SUBSTRING(#Text,CHARINDEX(' ',#Text,0),CHARINDEX(' ',#Text,0)),0,CHARINDEX('/',#Text,0))
+ ' '
+
REPLACE(REVERSE(SUBSTRING(REVERSE(#Text),CHARINDEX('/',#Text,0) + 1 ,CHARINDEX('/',#Text,0))),'/', ' ')

Re-arrange string contents

I have a string like: (10.00+Age)power2
I want to change the string to: power(10.00+Age,2)
How is this possible with sql server 2008.?
This expression reconstructs your strings as described:
'power' + replace(my_column, ')power', ',') + ')'
See SQLFiddle demo
If the string is always in the format you show, you could do a simple replace
replace("(10+Age)power2",")power2",",2)")
Updated
DECLARE #ans VARCHAR(200)
SET #ans = 'power'+replace('(10+Age)power2',')power2',',2)')

T-SQL Split String Like Clause

I have declare #a varchar(100) = 'abc bcd cde def'. What I need is to select from a table where a column is like 'abc' or 'bcd' or 'cde' or 'def'. I can use a split function and a while to get what I want, but somewhere I saw a smart solution using replace or something similar and I just can't remember it.
I know I can use an xml variable, and parse it that way. However, the value is part of a large procedure, and the best way for me is to use it in string form.
I know I can solve this by building a dynamic sql query, but that is not an option in the domain I'm working in.
Damn, I just can remember the solution. Its a hack, a little dirty trick that do the job.
Anyways, I ll use the code bellow (Im over SQL Server 2008), is it a good idea? I prefer it over the dirty split. Is it more performatic?
declare #w varchar(100) = 'some word'
declare #f xml
set #f = '<word>' + replace(#w, ' ', '</word><word>') + '</word>'
select
template.item.value('.', 'varchar(100)') as word
from #f.nodes('/word') template(item)
Use a function to split the individual items into a table, one record per item. Then you simply join to that table.
insert into #FilterTable (filters)
select Items from dbo.Split(#YourFilterString)
select *
from YourTable yt
join #FilterTable f on f.filters = yt.YourColumn
Of course my example is using equality. It gets more complicated if you truly intend to use "like" with wildcards.
In tsql you can use a pattern col like '[abcd]'
http://msdn.microsoft.com/en-us/library/ms179859.aspx
For matching multiple words (not letter) and without dynamic SQL, you'll have to get the values into a temp table. For a split function try this page http://www.sommarskog.se/arrays-in-sql-2005.html#iterative and look at the List of Strings function iter_charlist_to_table.
Or maybe you are thinking of this little trick Parameterize an SQL IN clause from the SO CEO.
for 4 sections max
WHERE
PARSENAME(REPLACE(#a, ' ', '.'), 1) = 'xxx'
OR
PARSENAME(REPLACE(#a, ' ', '.'), 2) = 'xxx'
OR
PARSENAME(REPLACE(#a, ' ', '.'), 3) = 'xxx'
OR
PARSENAME(REPLACE(#a, ' ', '.'), 4) = 'xxx'

Checking for empty xml column

Next query runs successfully:
select top(100) * from PackageSessionNodes
where Cast(ContentInteractions as nvarchar) != ''
Next gives me error
Target string size is too small to represent the XML instance
update PackageSessionNodes set ContentInteractions = '<contentinteractions />'
where Cast(ContentInteractions as nvarchar) = ''
ContentInteractions is an xml column. No DDT defined on it.
How can I solve the second query so that I get those records with '' as xml?
I seems like SqlServer cannot deal with empty values that are stores in a xml column... but you can insert them... how's that?
The problem is with your CAST. When you don't specify the length of nvarchar, it defaults to 30 characters in the CAST-function. So what what your statement really says is
update PackageSessionNodes set ContentInteractions = '<contentinteractions />'
where Cast(ContentInteractions as nvarchar(30)) = ''
So if the XML-content of ContentInteractions is serialized to more than 30 characters, you get this problem.