Replace or extend a URL in database table - sql

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'

Related

SQL Server Insert Varbinary

I need to take a Varbinary(max) from a SQL Server column and insert it into another varbinary column, however my image does not render when displayed via the new field, what am I doing wrong?
I've copied from SQL Server, ie
E7D28201A240202178EF491E413EB4DDBCFB54A15077A989A37A0C5E4E0F143AEE7000CE3F3A70C0E4F342E524C8F4A7243888AA581EC051B030E7A8EF4E233DF.....
and then
update client
set Photo = convert(varbinary(max),'E7D28201A240202178EF491E413EB4DDBCFB54A15077A989A37A0C5E4E0F143AEE7000CE3F3A70C0E4F342E524C8F4A7243888AA581EC051B030E7A8EF4E233DF.....')
where id='40946b09-81be-42b0-ae62-63182acb1e89'
The data is not Base64 encoded as that would have a much larger range of alpha characters, whereas this value is pure hex values (i.e. 0-9, A-F). Also, the example HTML code provided by the O.P. (in a comment on the Question) is:
<img src="data:image;base64,#System.Convert.ToBase64String(Model.PhotoDisplay)
which shows that the source value of Model.PhotoDisplay is first converted to Base64 so that the data:image;base64 type designation will be correct.
The problem is that SQL Server, when asked to convert the value, does not know that it is already a hex/binary string. You need to tell SQL Server what the format of the source data is. This can be done in two ways:
Depending on how you are building this value, you just need to add the 0x prefix and remove the single-quotes:
UPDATE cl
SET cl.Photo = CONVERT(VARBINARY(MAX), 0xE7D28201A24020.....)
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
If you need to keep the value as a string, and are using SQL Server 2008 or newer, then you can use the "style" option of the CONVERT function to tell SQL Server that it is a binary value, and you have the additional option of
adding the 0x prefix and using a "style" of 1:
UPDATE cl
SET cl.Photo = CONVERT(VARBINARY(MAX), '0xE7D28201A24020.....', 1)
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
keep the value without the 0x prefix and using a "style" of 2:
UPDATE cl
SET cl.Photo = CONVERT(VARBINARY(MAX), 'E7D28201A24020.....', 2)
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
But, if this data really is coming from a VARBINARY(MAX) field in another table, then how is it ending up as a string in the first place? The easiest way to move this is to either:
Transfer the data directly between tables:
UPDATE cl
SET cl.Photo = src.SomeField
FROM Client cl
INNER JOIN SomeTable src
ON src.JoinField = cl.JoinField
WHERE something? = somethingElse?;
Use a VARBINARY(MAX) variable:
DECLARE #Photo VARBINARY(MAX);
SELECT #Photo = src.photo
FROM SomeTable src
WHERE src.SomeField = ?;
UPDATE cl
SET cl.Photo = #Photo
FROM Client cl
WHERE cl.[id] = '40946b09-81be-42b0-ae62-63182acb1e89';
Why not copy it directly using an SQL statement, rather than copy/pasting from SSMS?
I'll leave an example to copy the data from another client (bogus id for the example):
UPDATE client
SET Photo = (SELECT Photo FROM client WHERE id='00000000-0000-0000-0000-000000000000')
WHERE id='40946b09-81be-42b0-ae62-63182acb1e89';

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%'

Error in update statement, confused with insert?

UPDATE KopierenJPGs
SET(IdImage, Path, Date)
VALUES (115,'\fantasy\5.jpg', '22/02/2015 18:08:28')
WHERE IdImage = 30
And
WHERE Path = '\fantasy\6.jpg'
AND
WHERE Date = '22/02/2015 18:10:28'
I was taught to execute update statements like this, though not a single site gives this example.
It seems to me that my teacher confused an insert command with an update command?
Do I have it wrong here? Is there an actual error in this statement?
I am aware there are three where clauses, I need them for my program. I cannot update a fixed ID since the column names are variable, this is intentional.
Thank you.
I doubt your teacher made the confusion. Use the correct syntax for update:
UPDATE KopierenJPGs
SET IdImage = 115,
Path = '\fantasy\5.jpg',
Date = '2015-02-22 18:08:28'
WHERE IdImage = 30 And Path = '\fantasy\6.jpg' and Date = '2015-02-22 18:10:28';
Note that I also changed the date formats to ISO standard formats. You should use these in code to avoid ambiguity and future problems.
If you want to insert a new row, then yes instead of update you need insert statement like:
INSERT INTO KopierenJPGs (IdImage, Path, Date)
VALUES (115,'\fantasy\5.jpg', '22/02/2015 18:08:28')
If you want to update an already existing row, you could do something like:
UPDATE KopierenJPGs
SET IdImage = 115,
Path = '\fantasy\5.jpg',
Date = '22/02/2015 18:08:28'
WHERE IdImage = 30
AND Path = '\fantasy\6.jpg'
AND Date = '22/02/2015 18:10:28'

SQL query to change file extension in a record containing a file-path?

Given an SQL table Table with a column path, how can I modify values like /dir/subdir/file.aaa => /dir/subdir/file.bbb e.g. modify just the file-extension without having to hard-code the specific file/path into my query?
Seems a perfect fit for regexp_replace :
with t as (select '/dir/subdir/file.aaa' as path from dual
union all select '/dir/subdir.aaa/file.aaa' from dual)
select regexp_replace(path, '[.][^.]*$', '.bbb') path
-- ^^^^ ^^^^^^^^^ ^^^^
-- replace last dot-whatever by the "right" extension
from t
where path like '%.aaa'
-- ^^^^^
-- only for path ending with the "wrong" extension
See http://sqlfiddle.com/#!4/d41d8/37017 for some tests
If the column only contains values that are structured like a file, path the following will work:
update the_table
set path = replace(path, '.aaa', '.bbb')
where path like '%.aaa';
Note that this will also update a value like /dir/subdir.aaa/file.aaa to /dir/subdir.bbb/file.bbb.
Another option is to use a regular expression:
update foo
set file_path = regexp_replace(file_path, '\.aaa$', '.bbb', 1, 0, 'i')
where lower(file_path) like '%.aaa';

Update field in table for all records using a select statement

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]))