使用 switch-case 语句更新 T-SQL

发布于 2024-12-02 18:57:51 字数 897 浏览 1 评论 0原文

我想在 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

,但它没有返回预期的结果。

谁能给我工作版本吗?

I want implement this pseudocode in 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'

for it I use this solution

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

but it is does not return expected result.

Can anyone give me working version please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

牵强ㄟ 2024-12-09 18:57:51
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
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
暮凉 2024-12-09 18:57:51

而不是 SUBSTRING([Path], LEN([Path]) - CHARINDEX('.', REVERSE([Path])) + 1, 3),
尝试使用 lower(right([Path], 4))

Instead of SUBSTRING([Path], LEN([Path]) - CHARINDEX('.', REVERSE([Path])) + 1, 3),
try using lower(right([Path], 4))

坏尐絯 2024-12-09 18:57:51

您对扩展名的读取是错误的,请尝试:(

SUBSTRING(Path, LEN(Path) - CHARINDEX('.', REVERSE(Path)) + 1, LEN(Path)) 

使用 LEN(Path) 作为读取长度;如果它溢出字符串末尾并允许 n 字符扩展,则很好)

Your read of the extension is wrong, instead try:

SUBSTRING(Path, LEN(Path) - CHARINDEX('.', REVERSE(Path)) + 1, LEN(Path)) 

(Using LEN(Path) as the read length; fine if it overflows the end of the string and allows for n-character extensions)

我偏爱纯白色 2024-12-09 18:57:51

尝试使用 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

Try using the 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
孤独陪着我 2024-12-09 18:57:51
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

谢谢我终于用这个了

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

thank you I finaly use this

硬不硬你别怂 2024-12-09 18:57:51

该脚本将确保您不会在每次运行该脚本时更新所有行。它只会更新更改。

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

This script will assure that you do not update all rows every time you run the script. It will only update changes.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文