SQL Server How can I replace a URL prefix using SQL?

yizd12fk  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(109)

In my SQL database, I have a SQL table where there is a column that stored the URL of some images that I am displaying on my website. The URL field is something like http://some_domain/some_thing

I need to replace all of them with something like https://some_domain/some_thing

I can write a C# script to do the same and write the required regex in C#. Does anyone know how to do this kind of replacement in SQL Server.

lx0bsm1f

lx0bsm1f1#

You need to search for all entries that have an http:// prefix and then replace it, eg:

UPDATE MyTable
set ImageUrl=REPLACE(ImageUrl,'http://','https://')
where ImageUrl like 'http://%'

The WHERE clause ensures that only entries with http:// are processed and allows the query to take advantage of possible indexes on the image field. LIKE 'abc%' is essentially a range search that matches values between abc and the next entry after the prefix, ie abd`

相关问题