这是我的查询。
select imgsrc from images where id = 1
像这样输出,
imgsrc
<img src="http://mypage/tiles/imgs/imgfilename.png"/>
但我希望我的输出就像imagefilename.png
可能的那样?
那么我遵循什么方式来获得所需的输出是可能的吗?
您可以创建一个 UDF,它在匹配并找到模式后返回名称。
CREATE FUNCTION ufcGetImageName(@imageSrc varchar(200))
RETURNS varchar(200)
AS
BEGIN
DECLARE @position INT
/* here it will fetch the image source followed by any other image attributes*/
SET @position = CHARINDEX('src="', @imageSrc) + 5
SET @imageSrc = SUBSTRING(@imageSrc, @position, LEN(@imageSrc))
/* here it wil fetch only the image url leaving other image attributes*/
SET @position = CHARINDEX('"', @imageSrc)
SET @imageSrc = SUBSTRING(@imageSrc, 0, @position)
/* here it will fetch the image name alone */
SET @position = CHARINDEX('/', reverse(@imageSrc))
SET @imageSrc = SUBSTRING(reverse(@imageSrc), 0, @position)
RETURN reverse(@imageSrc)
END;
并拨打任何查询
SELECT
imgSrc as imagesrc,
dbo.ufcGetImageName(imgSrc) AS imagename
FROM
images;
这是演示
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句