0

I have one Table in my SQL Server Database like

Table1

 srno   Filepath      
   1     C:\Folder1\Folder2\Folder3\Files\File_1.70837292036d41139fcf8fa6b4997d3c.pdf
   2     File_2.pdf
   3     C:\File_3.pdf
   4     File_4.70837292036d41139fcf8fa6b4997d3c.pdf
   5      
   6     NULL

i want only srno, filename from this.

how can i get only filename through simple query?

Desired Output like

  srno    Filename
    1       File_1.pdf
    2       File_2.pdf
    3       File_3.pdf
    4       File_4.pdf
    5       
    6       NULL 

I have one Query but it is not showing proper working with ( srno 1 and 4 ) only

Select srno, CASE WHEN LEN(CHARINDEX('\', REVERSE(ISNULL(table1.Filepath,''))))= 1 THEN ISNULL(table1.Filepath,'') ELSE  REVERSE(SUBSTRING(REVERSE(ISNULL(table1.Filepath,'')), 1, CHARINDEX('\', REVERSE(ISNULL(table1.Filepath,'')))-1))  END  as Filename
4

1 回答 1

1

反转刺痛并搜索第一个的索引\。然后使用此索引获取列的右侧。

SELECT RIGHT(Filename,PATINDEX('%\%',REVERSE(Filename))-1)

如果您想将 File_1.70837292036d41139fcf8fa6b4997d3c.pdf 转换为 File_1.pdf 那么您可以尝试以下操作,尽管它可能看起来很丑:

SELECT 
LEFT
(
    RIGHT
    (
        Filepath,
        CASE WHEN PATINDEX('%\%',REVERSE(Filepath)) > 0 
        THEN PATINDEX('%\%',REVERSE(Filepath))-1 
        ELSE LEN(Filepath) 
        END 
    ),
    CASE WHEN 
    PATINDEX
    (
        '%.%',
        RIGHT
        (
            Filepath,
            CASE WHEN PATINDEX('%\%',REVERSE(Filepath)) > 0 
            THEN PATINDEX('%\%',REVERSE(Filepath))-1 
            ELSE LEN(Filepath)  
            END
        )
    )>0
    THEN
    PATINDEX
    (
        '%.%',
        RIGHT
        (
            Filepath,
            CASE WHEN PATINDEX('%\%',REVERSE(Filepath)) > 0 
            THEN PATINDEX('%\%',REVERSE(Filepath))-1 
            ELSE LEN(Filepath)  
            END
        )
    )-1
    ELSE 0 END
)
+
RIGHT
(
    Filepath,
    CASE WHEN PATINDEX('%.%',REVERSE(Filepath)) > 0 
    THEN PATINDEX('%.%',REVERSE(Filepath)) 
    ELSE LEN(Filepath)  
    END
)
于 2013-09-05T06:40:20.527 回答