sqlsql-server

I need to replace old path's images to new path with the same image


I have a column Path_Image in table DBPaths :

 Path_Image          
 C:\Users\Pc\Desktop\archive\1.png
 D:\pics_back\22.jpg
 C:\Users\Pc\Desktop\archive\947141.JPG

And I have a new Path:

E:\pics2024

I want to replace all old paths with a new one without touch images , like this :

 Path_Image
 E:\pics2024\1.png
 E:\pics2024\22.jpg
 E:\pics2024\947141.JPG

The question is that possible and how I can do it , for me no idea how it could be?


Solution

  • I have little experience with string functions, so the code below is just a rough idea

    WITH your_table(PATH_IMAGE)AS
    (  
      SELECT ' C:\Users\Pc\Desktop\archive\1.png' UNION ALL
      SELECT 'D:\pics_back\22.jpg' UNION ALL
      SELECT 'C:\Users\Pc\Desktop\archive\947141.JPG'
    )
    SELECT C.PATH_IMAGE,'E:\pics2024'+RIGHT(C.PATH_IMAGE,CHARINDEX('\',REVERSE(C.PATH_IMAGE)))
    FROM your_table AS C