文件名上的Sql Trim

问题描述 投票:0回答:4

我正在尝试修剪一个长文件名,只想获取该文件名的最后一个字符,该字符位于最后一个正斜杠()之后,我也想删除该文件名末尾的.xlsx 。

原始

sasa\sasas\sasas\1234_IASS.xlsx

预期输出

1234_IASS

感谢您的帮助。谢谢

sql sql-server trim
4个回答
1
投票

您可以首先将文件名提取为shown

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

然后您可以将其与REPLACE结合使用以删除扩展名。

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

1
投票

您可以尝试执行此操作,如注释中所述,文件扩展名已修复。

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

您可以找到现场的example here


1
投票

您说您的目录是相同的,并且扩展名始终是相同的?将[path]替换为您的表列名称:

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

0
投票

您的帖子标题具有误导性,在TRIM中执行sql-server可以通过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));

0
投票
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)
© www.soinside.com 2019 - 2024. All rights reserved.