我想在t-sql中实现这个伪代码
UPDATE Resources SET [Path]= CASE ([Path].Substring([Path].LastIndexOf('.')))
WHEN '.jpg' THEN '/image.jpg'
WHEN '.png' THEN '/image.jpg'
WHEN '.avi' THEN '/video.jpg'
WHEN '.mkv' THEN '/video.jpg'
为此我使用此解决方案
UPDATE Resources SET [Path] = CASE (SUBSTRING([Path], LEN([Path]) - CHARINDEX('.', REVERSE([Path])) + 1, 3))
WHEN '.jpg' THEN '/image.jpg'
WHEN '.png' THEN '/image.jpg'
WHEN '.avi' THEN '/video.jpg'
WHEN '.mkv' THEN '/video.jpg'
END
但它不会返回预期的结果。
有人可以给我工作版吗?
答案 0 :(得分:7)
UPDATE
Resources
SET
Path = CASE SUBSTRING(Path, LEN(Path) - CHARINDEX('.', REVERSE(Path)) + 1, 4)
WHEN '.jpg' THEN '/image.jpg'
WHEN '.png' THEN '/image.jpg'
WHEN '.avi' THEN '/video.jpg'
WHEN '.mkv' THEN '/video.jpg'
END
答案 1 :(得分:3)
而不是SUBSTRING([Path], LEN([Path]) - CHARINDEX('.', REVERSE([Path])) + 1, 3)
,
尝试使用lower(right([Path], 4))
答案 2 :(得分:2)
您阅读的扩展程序错误,请尝试:
SUBSTRING(Path, LEN(Path) - CHARINDEX('.', REVERSE(Path)) + 1, LEN(Path))
(使用LEN(Path)
作为读取长度;如果它溢出字符串的末尾并允许n个字符扩展,则很好)
答案 3 :(得分:2)
尝试使用ParseName
UPDATE Resources SET [Path] = CASE (Parsename(Path,1))
WHEN 'jpg' THEN '/image.jpg'
WHEN 'png' THEN '/image.jpg'
WHEN 'avi' THEN '/video.jpg'
WHEN 'mkv' THEN '/video.jpg'
END
答案 4 :(得分:0)
UPDATE Resources SET ThumbnailPath = CASE SUBSTRING(ThumbnailPath, LEN(ThumbnailPath) - CHARINDEX('.', REVERSE(ThumbnailPath)) + 1, LEN(ThumbnailPath))
WHEN '.doc' THEN @root + '/doc.png'
WHEN '.docx' THEN @root + '/doc.png'
WHEN '.jpg' THEN @root + '/image.png'
WHEN '.jpeg' THEN @root + '/image.png'
WHEN '.gif' THEN @root + '/image.png'
WHEN '.png' THEN @root + '/image.png'
WHEN '.ppt' THEN @root + '/ppt.png'
WHEN '.pptx' THEN @root + '/ppt.png'
WHEN '.pdf' THEN @root + '/pdf.png'
ELSE @root + '/other.png'
END
谢谢我终于使用了这个
答案 5 :(得分:0)
此脚本将确保您每次运行脚本时都不会更新所有行。它只会更新更改。
UPDATE r
SET ThumbnailPath = newvalue
FROM Resources r
cross apply
(SELECT right(ThumbnailPath, patindex('%_.%', reverse(ThumbnailPath))) T) a
cross apply
(SELECT CASE
WHEN a.T in ('doc','docx' ) THEN @root + '/doc.png'
WHEN a.T in ('jpg','jpeg','gif','png') THEN @root + '/image.png'
WHEN a.T in ('ppt','pptx') THEN @root + '/ppt.png'
WHEN a.T = 'pdf' THEN '/pdf.png'
ELSE @root + '/other.png'
END newvalue) b
WHERE r.ThumbnailPath <> b.newvalue