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');
Related
I changed the position of some files in my file server and I need the update the URL of the file in my database.
For example from:
/sba/sbazen/test/SBA33G_TC/3300009//00150671.pdf
I want to get:
/sba/sbazen/test/SBA33G_TC/new_folder_name/3300009//00150671.pdf
Important to know: The number on SBA33G is variable. We can have SBA100G or SBA02G.
if the old folder path is always the same length you can use stuff
select len('/sba/sbazen/test/SBA33G_TC/'), stuff(t.oldval, 27, 1, '/new_folder_name/')
from (
select '/sba/sbazen/test/SBA33G_TC/3300009//00150671.pdf' oldval
) t
Why not simply use Update?
UPDATE tableName
SET
colName = '/sba/sbazen/test/SBA33G_TC/new_folder_name/3300009//00150671.pdf'
WHERE colName = '/sba/sbazen/test/SBA33G_TC/3300009//00150671.pdf'
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%'
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.
A previous developer created a table that stores the absolute path to files in our server. I want to convert them to relative paths instead.
I already wrote the portion that properly strips the string down to a relative path. My issue is understanding how to basically update each record, with a new version of its own string.
Here is what I originally tried:
UPDATE LFRX_Attachments
SET [File] = (SELECT TOP 1 SUBSTRING([File], PATINDEX('%Files\%', [File]) + 6, LEN([File]))
FROM LFRX_Attachments A
WHERE [Type] = 4 AND AttachmentId = A.AttachmentId)
However, this tanked in epic fashion by just overwriting every record to have the value of the first record in the table. Any suggestions?
UPDATE LFRX_Attachments
SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))
WHERE [Type] = 4
From a readability/maintenance standpoint, you're better off selecting for the data you want to alter, then iterating through the result set and updating each record separately.
Does this work for you?
UPDATE LFRX_Attachments SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))
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