1

Basically I have a column in SQL Server that has icon image names

It's kind of like

ICON
------------
Icon001
Icon002.png
Icon003.png
Icon004.png
Icon005
Icon006.png
Icon007.png

I'm trying to figure out how I can write a script to remove all of the .png from the ones that have it

I have tried

Update [dbo].[screen].[icon] 
set ICON = ICON - '%.png%' 
where ICON LIKE '%.png%'

But that doesn't work.

Can anybody help me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Derick
  • 43
  • 1
  • 5
  • 1
    Hint: [`REPLACE()`](http://msdn.microsoft.com/en-us/library/ms186862.aspx) and of course research on [previous related questions](http://stackoverflow.com/search?q=sql+server+remove+part+of+string) – Pondlife Mar 22 '13 at 21:17

3 Answers3

2

Try

... set ICON=LEFT(icon, LEN(icon)-4 ) where ICON like '%.png'
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • why not? what if one of the records is `image.png.png`? replace'll kill both .png's – Marc B Mar 22 '13 at 21:21
  • I stand corrected. It's an unlikely occasion, but it could happen. – Yuriy Galanter Mar 22 '13 at 21:24
  • 1
    yep, unlike, but I've learned to not trust in "unlikely". as the old statement goes: million-to-one-chances have a 50:50 chance of occuring: either they do, or they don't. – Marc B Mar 22 '13 at 21:25
2
...SET ICON = REPLACE(ICON, '.png','')

Should do the trick

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
0

Maybe something like

UPDATE icon SET icon = LEFT(icon, CHARINDEX('.png', icon) - 1) WHERE icon LIKE '%.png%'

A demo of something

Kermit
  • 33,827
  • 13
  • 85
  • 121