0

If I want to update a column by pulling out only a part of a substring of another column.

What I want to do is pull the name of the jpg from the file and for example i want imageName to be equal to great-family.jpg" a varchar string. But the image names are all different.

update tblPetTips
set imageName = "great-family.jpg"
where articleText = "<img src="/images/imgs/great-family.jpg" alt="A Great Family Dog">"

In this case I would like to say

update tblPetTips
set imageName = "yellow-smile.jpg"
where articleText = "<img src="/images/imgs/yellow-smile.jpg" alt="A Yellow Smiley Face">"

How do I (without hardcoding) update imageName fromthe articleText column. All the directories are the same - all the images live in images/imgs.

bummi
  • 26,435
  • 13
  • 58
  • 97
YelizavetaYR
  • 1,451
  • 5
  • 15
  • 30

2 Answers2

0

You can try to get values between the last / and " with SUBSTRING_INDEX function:

UPDATE tblPetTips
SET imageName = SUBSTRING_INDEX(SUBSTRING_INDEX(articleText, '/', -1), '"', 1);

It will only work if format of <img srs=... > html is consistent.

Bulat
  • 6,494
  • 1
  • 25
  • 47
  • Error: Msg 195, Level 15, State 10, Line 2 'SUBSTRING_INDEX' is not a recognized built-in function name. – YelizavetaYR Sep 20 '14 at 21:49
  • that is strange - http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index – Bulat Sep 20 '14 at 21:52
  • Apparently he doesn't know the difference between MySQL and SQL-Server, because he's actually using the latter. – Barmar Sep 20 '14 at 21:53
  • http://stackoverflow.com/questions/19505875/parse-file-name-and-path-from-full-path – Bulat Sep 20 '14 at 22:05
  • i'm helping out a friend - this is what i told there is - and i'm trying to help resolve some issues best i can. what is a good way to fix it. – YelizavetaYR Sep 20 '14 at 22:20
  • Any ideas how to fix it via TSQL? – YelizavetaYR Sep 20 '14 at 22:55
  • you can do it with combination of SUBSTRING and CHARINDEX like it is done here - http://stackoverflow.com/questions/19505875/parse-file-name-and-path-from-full-path – Bulat Sep 21 '14 at 08:55
0

if the source of your images is always /images/imgs/, you can use patindex to find the position of '/images/imgs/' and '" alt', then extract the text between them. check if this works:

substring(articletext, 
          patindex('/images/imgs/', articletext) + length('/images/imgs/'), 
          patindex('" alt') - (patindex('/images/imgs/', articletext) + length('/images/imgs/')))

if your images can have any url, then it would be feasible with regular expressions, but I don't think sqlserver provides regex directly. in that case you could write a function to extract the filename part of a url and call it in the update.

1010
  • 1,681
  • 15
  • 26