Add value at beginning of string but only on some rows

User avatar
Timelord
4StarLounger
Posts: 504
Joined: 23 Jul 2010, 18:36
Location: Westlake, OHIO

Add value at beginning of string but only on some rows

Post by Timelord »

The script I am trying is:

update tableQ
set columnA = 'value' columnA
where columnB equals (Numeric value)

The problem is that SQL says there is incorrect syntax near the keyword 'where'

I want to replace value 'This is a important document' with 'Top Dog - This is a important document' but only where the itemvalue = '12456'

Using MS Sql 2012
Any help appreciated
Who will you Inspire today?

User avatar
HansV
Administrator
Posts: 78365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add value at beginning of string but only on some rows

Post by HansV »

Try

Code: Select all

UPDATE [tableQ]
SET [columnA] = 'Top Dog - ' & [columnA]
WHERE [columnB] = 12345
(Don't use quotes around numeric values)
Best wishes,
Hans

User avatar
Timelord
4StarLounger
Posts: 504
Joined: 23 Jul 2010, 18:36
Location: Westlake, OHIO

Re: Add value at beginning of string but only on some rows

Post by Timelord »

Thanks Hans

I receive "Incorrect Syntax near '&'"
This is the exact text that I used:

Update [hsi].[itemdata]
set [itemname] = 'A_UNDO_A - ', & [itemname]
where [itemnum] = 6684

and without the & I receive "Incorrect Syntax near 'where'"
Once I get this working I will be replacing the where statement with:
where [itemnum] in (around 242 numeric values, commas in between each of them)
Who will you Inspire today?

User avatar
HansV
Administrator
Posts: 78365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add value at beginning of string but only on some rows

Post by HansV »

Don't use a comma between the string and &:

Code: Select all

set [itemname] = 'A_UNDO_A - ' & [itemname]
Best wishes,
Hans

User avatar
Timelord
4StarLounger
Posts: 504
Joined: 23 Jul 2010, 18:36
Location: Westlake, OHIO

Re: Add value at beginning of string but only on some rows

Post by Timelord »

Thanks Hans!
It all works now.
Who will you Inspire today?