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%'
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'
Here's an example table:
I'm trying to write a SQL Statement (MS SQL) that will look for records containing a specific string and then replace that string with a new string.
In this example we let's say we want to see all records that:
Contain the string: "~/main/fruits/apples"
Replace the "contains string" with string: "~/main/fruits/stuff"
The system should edit record IDs: 4, 7, and 8 in this fashion:
My failed attempt is this:
UPDATE mydb.dbo.test
SET [FILE_PATH] =
(
SELECT REPLACE('~/main/fruits/apples', 'apples', 'stuff') WHERE ([FILE_PATH] LIKE '~/main/fruits/apples%')
)
WHERE ([FILE_PATH] LIKE '~/main/fruits/apples%');
The problem with the statement above is that for IDs 7 and 8 it revises them to both equal: '~/main/fruits/stuff'
You seem to want:
UPDATE mydb.dbo.test
SET FILE_PATH = REPLACE(FILE_PATH, 'apples', 'stuff')
WHERE FILE_PATH LIKE '~/main/fruits/apples%';
Personally, though, I would align the replace pattern with the like pattern, so it was more like:
UPDATE mydb.dbo.test
SET FILE_PATH = REPLACE(FILE_PATH, '~/main/fruits/apples', '~/main/fruits/stuff')
WHERE FILE_PATH LIKE '~/main/fruits/apples%';
This prevents a problem if apples occurs more than once in the path.
I need help in filtering the content of this SQL column. I have unfortunately been unsuccessful so far. I will be happy for any assistance.
My goal is for all the unc paths to bear the same format.
All should look like: \\ps9\wa033242. Meaning all should begin with the "\\" replacing the "///"
I tried truncating it but because of the different string length, I have problems.
I tried truncating and UPDATING
SELECT
cw_platz.nummer,
cw_platz.nwaddress,
cw_platz.bezeichnung,
os_cw.cw_ldzuplatz.ldruckernr,
os_cw.cw_ldzuplatz.papierschacht,
os_cw.cw_ldzuplatz.treibername,
cw_logischerdrucker.bezeichnung
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung in cw_platz.bezeichnung
This is my result:
My first thought is to simply use something like REPLACE(yourstringhere, '/','\').
Is it something you already tried?
Reference: https://learn.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-2017
you could use replace
select replace('client/ps9///wa033242//', '/' ,'\');.
and for update
update your_table
set your_column = replace(your_column, '/' ,'\')
try avoid .. the where like
UPDATE os_cw.cw_ldzuplatz
SET os_cw.cw_ldzuplatz.treibername = REPLACE(os_cw.cw_ldzuplatz.treibername, '/' ,'\')
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung = cw_platz.bezeichnung
Try this it will help you.
UPDATE os_cw.cw_ldzuplatz
SET os_cw.cw_ldzuplatz.treibername = REPLACE(os_cw.cw_ldzuplatz.treibername, '\\','///')
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung = cw_platz.bezeichnung
and TREIBERNAME like '\\%'
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';
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]))