How do I pull a substring of various sides? - sql

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.

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.

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.

Related

Update .jpg to .png in particular column

I have an SQL table called buzzinga_menulist with an image column containing values like:
x1.jpg
x2.jpg
x3.jpg
x4.jpg
How do I update all values ending with .jpg suffixes to have .png suffixes instead? I've tried the following but it gives me a syntax error.
UPDATE buzzinga_menulist
SET image = replace(*, '.png', '.jpg')
WHERE image = '.jpg';
Use this query
UPDATE `buzzinga_menulist`
SET `image` = REPLACE(`image`, '.jpg', '.png');

Delete specific word at the beginning

in my table within some columns i got strings which starting always with /PicsDB
like this below:
/PicsDB/Something 2015/Some thing bla/Some thing other/img34234.jpg
what i want to achieve is to for each row delete starting string /PicsDB
so using above string the final result should be:
/Something 2015/Some thing bla/Some thing other/img34234.jpg
How to achieve that?
Can i just simply do ? :
UPDATE my_table SET path = replace(path, '/PicsDB', '');
Just use substring:
UPDATE my_table SET path = substring(path, 8, 9999);
where path like '/PicsDB%'

Remove text from column in sql database

I need to remove some text from a sql db in a metadata colum that's taking up too much space and isn't necessary.
ImageDescription: Make:NIKON Model:COOLPIX L26 Orientation:1 XResolution:300 YResolution:300 ResolutionUnit:2 Software:COOLPIX L26 V1.0 DateTime:2013:01:05 05:43:12 YCbCrPositioning:2 ExposureTime:0.04 FNumber:3.2 ExposureProgram:2 ISOSpeedRatings:200 ExifVersion:3836042731,664497658,2489535484,2327246609 DateTimeOriginal:2013:01:05 05:43:12 DateTimeDigitized:2013:01:05 05:43:12 ComponentsConfiguration:185856,59901696,256,1280 CompressedBitsPerPixel:2 ExposureBiasValue:0 MaxApertureValue:3.4 MeteringMode:5 LightSource:0 Flash:24 FocalLength:4.6 UserComment:0,0,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,2105376,16973830,65539,393216,18481972,65541,62128128,18546688,65541,62652416,19398656,65539,131072,33685414,65540,63176704,33685504,65540,223674368,0,19660800,65536,19660800,65536,3640590336,2214648831,202051584,269093902,302910734,403902481,370678312,590419990,975707429,960249139,1077360691,1078877256,927291204,1366118456,1734500183,1295935336,1685092721,1734696056,303104355,404035602,790239791,1110983267,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,3237962595,528640,60817528,33562881,285409553,29687553,16777378,16843013,65793,0,16777216,84148994,151521030,1051402,50528514,84083714,263173,24969472,67109634,554829073,319177009,570909009,2167542897,587768209,365015362,619762002,2188534323,387320329,622467352,690497318,909456426,976828471,1178944579,1246316615,1448432723,1515804759,1717920867,1785292903,1987409011 FlashpixVersion:1687135923,3231872644,1301805154,2554547069 ColorSpace:1 PixelXDimension:640 PixelYDimension:480 FileSource:3 SceneType:1 CustomRendered:0 ExposureMode:0 WhiteBalance:0 DigitalZoomRatio:0 FocalLengthIn35mmFilm:26 SceneCaptureType:0 GainControl:1 Contrast:0 Saturation:0 Sharpness:0 SubjectDistanceRange:0
I want to get rid of the Usercomment part of that text.
So far I have this that will remove anything left of the usercomment.
UPDATE Document
SET MetaData = LEFT(MetaData, CHARINDEX('UserComment:', MetaData) -1)
WHERE MetaData IS NOT NULL
AND MetaData like '%UserComment:%'
AND DocumentId = '480024'
But I just want to get rid of the usercomment and not anything before or after that.
here is a sample of just the UserComment, it seems to not have any spaces.
UserComment:0,0,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,538976288,2105376,16973830,65539,393216,18481972,65541,62128128,18546688,65541,62652416,19398656,65539,131072,33685414,65540,63176704,33685504,65540,223674368,0,19660800,65536,19660800,65536,3640590336,2214648831,202051584,269093902,302910734,403902481,370678312,590419990,975707429,960249139,1077360691,1078877256,927291204,1366118456,1734500183,1295935336,1685092721,1734696056,303104355,404035602,790239791,1110983267,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,1667457891,3237962595,528640,60817528,33562881,285409553,29687553,16777378,16843013,65793,0,16777216,84148994,151521030,1051402,50528514,84083714,263173,24969472,67109634,554829073,319177009,570909009,2167542897,587768209,365015362,619762002,2188534323,387320329,622467352,690497318,909456426,976828471,1178944579,1246316615,1448432723,1515804759,1717920867,1785292903,1987409011
Any help will be appreciated.
you can use charindex to find where the user comment start and where it ends
http://msdn.microsoft.com/en-us/library/ms186323.aspx
After that use Left & Right functions to create the string you want to keep
http://msdn.microsoft.com/en-us/library/ms177601.aspx
http://msdn.microsoft.com/en-us/library/ms177532.aspx
select
CHARINDEX('UserComment:', MetaData),
CHARINDEX(' ', MetaData, CHARINDEX('UserComment:', MetaData)),
LEFT(aaa, CHARINDEX('UserComment:', MetaData)-1) + RIGHT(MetaData, LEN(MetaData) - CHARINDEX(' ', MetaData, CHARINDEX('UserComment:', MetaData)))
from dbo.MetaData

Update colum with value from another column + text

I imported an e-store's database values into my own, and it mostly worked out fine. However, there were no image file names. So, I need to update the entire database- over 6,000 records, so that under 'image' we get a path + model name + jpg, so each product can be associated with an image. Im having trouble mixing the dynamic column value with the static path. Here is what I need to accomplish:
UPDATE `store`.`pr_product`
SET `image` = 'data/products/`**model_no**`.jpg'
WHERE `pr_product`.`product_id` = `pr_product`.`product_id` ;
But cannot get it to recognize the dynamic nature of 'model_no'
Thanks in advance
Max,
Please what you means about dynamic nature of 'model_no'?
Is this column's datatype int or long or varchar
Please need more explaination with example
you can test the following if e.g model_no is column in pr_product table
UPDATE store.pr_product
SET image = 'data/products/'+pr_product.model_no+'.jpg'
WHERE pr_product.product_id = pr_product.product_id ;
Best Regards,
Mohammed Thabet Zaky

Using comma with an update sentence

I want to update a row which has some html tags inside. For instance:
src='/imagem.png'></ p></ body>
> UPDATE ISTANBUL_TABLE SET TEXT = '<
> body>< p>< img src='/imagem.png '></
> p></ body>' WHERE 1=1
You see after src=' means the query ends, but it does not end. How can i solve it without using " (double comma)? Any solution please?
best regards bk
You need to escape the single-quotes, by typing them twice:
UPDATE ISTANBUL_TABLE SET TEXT = '< body>< p>< img src=''/imagem.png ''>' WHERE 1=1
Also, your WHERE clause is nonsensical and can be dropped entirely
UPDATE ISTANBUL_TABLE SET TEXT = '<body><p><img src=''/imagem.png''>'
Use parameterised SQL:
UPDATE ISTANBUL_TABLE SET TEXT = #HTML WHERE...
Then from your calling code, you just pass in the #HTML parameter and don't need to double up the single quotes.