简体   繁体   English

对文件名进行 SQL 修剪

[英]SQL Trim on a file name

I am trying to trim a long file name and just want to get the last characters of that file name which is after the last forward slash ( \\ ) and I also want to eliminate the .xlsx at the end of the file name as well.我正在尝试修剪一个长文件名,只想获取该文件名的最后一个字符,该字符位于最后一个正斜杠 ( \\ ) 之后,我还想删除文件名末尾的.xlsx

Original:原来的:

sasa\sasas\sasas\1234_IASS.xlsx

Expected Output:预期输出:

1234_IASS

You can try this it will work as said in the comment the file extension are fixed.你可以试试这个,它会像评论中所说的那样工作,文件扩展名是固定的。

SELECT 
Replace(
    RIGHT('\' + RTRIM('sasa\sasas\sasas\1234_IASS.xlsx'), CHARINDEX('\', REVERSE('\' + RTRIM('sasa\sasas\sasas\1234_IASS.xlsx'))) - 1)
          ,'.xlsx', '') 
as FileName

You can find the live example here .您可以在此处找到现场示例

Your post's title is misleading, TRIM is performed in sql-server to remove whitespace either by RTRIM or LTRIM or a combination of the two, what you are trying to do is get a substring out of your example string, I am providing a solution which uses a combination of REVERSE , SUBSTRING and CHARINDEX , this answer is good for if you ever need to do this for different file extensions:您的帖子标题具有误导性,在sql-server执行TRIM以通过RTRIMLTRIM或两者的组合删除空格,您要做的是从示例字符串中获取子字符串,我提供了一个解决方案使用REVERSESUBSTRINGCHARINDEX的组合,如果您需要为不同的文件扩展名执行此操作,则此答案非常有用:

DECLARE @test varchar(255) = 'sasa\sasas\sasas\1234_IASS.xlsx';
DECLARE @lastOccurance INT = CHARINDEX('\', REVERSE(@test)); --Last occurence of the slash character to denote the end of the directory name or what not
DECLARE @lastPeriod INT = CHARINDEX('.', REVERSE(@test)); --This is the last occurence of the period, denoting the file extension
SET @lastOccurance = LEN(@test) + 1 - @lastOccurance;
SET @lastPeriod = LEN(@test) + 1 - @lastPeriod;
SELECT SUBSTRING(@test, @lastOccurance + 1, @lastPeriod - (@lastOccurance + 1));

You say your directory is the same and the extension is always the same?你说你的目录是一样的,扩展名总是一样的? Replace [path] with your table column name:[path]替换为您的表列名称:

select replace(replace([path],'sasa\sasas\sasas\',''),'.xlsx','')

Your comments state that both your file path and file extension are constant.您的评论表明您的文件路径和文件扩展名都是不变的。 If the number of characters in your file is also constant the simplest solution is to use SUBSTRING .如果文件中的字符数也是恒定的,最简单的解决方案是使用SUBSTRING

SELECT SUBSTRING(YourColumn, 18, 9)
FROM YourTable

If the number of characters is changing, a more robust solution is to use RIGHT to extract the file name and REPLACE to remove the file extension.如果字符数发生变化,更可靠的解决方案是使用RIGHT提取文件名并使用REPLACE删除文件扩展名。

SELECT REPLACE(RIGHT(YourColumn, LEN(YourColumn) - 17), '.xlsx', '')
FROM YourTable

If you need a more dynamic solution, you can first extract the filename as shown .如果您需要更动态的解决方案,您可以先提取文件名,如图所示

SELECT RIGHT(YourColumn, CHARINDEX('\', REVERSE(YourColumn)) - 1)
FROM YourTable

You can then combine this with REPLACE as before to remove the extension.然后,您可以像以前一样将其与REPLACE结合使用以删除扩展名。

SELECT REPLACE(RIGHT(YourColumn, CHARINDEX('\', REVERSE(YourColumn)) - 1), '.xlsx', '')
FROM YourTable
declare @x varchar(100) = 'sasa\sasas\sasas\1234_IASS.xlsx'

declare @filename varchar(max) = reverse(substring(reverse(@x), 1, charindex('\', reverse(@x))-1 ))
select substring(@filename, 1, charindex('.', @filename)-1)

If you want to remove extensions from filename then you can try this:如果你想从文件名中删除扩展名,那么你可以试试这个:

UPDATE TableName
SET FileName = REVERSE(SUBSTRING(REVERSE(FileName), 
                       CHARINDEX('.', REVERSE(FileName)) + 1, 999))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM