我有一个包含此值的表格:
Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx
OurStory/MeettheFoodieandtheMD.aspx
TheFood/OurMenu.aspx
我想得到这个
Diet.aspx
MeettheFoodieandtheMD.aspx
OurMenu.aspx
我该怎么做?
答案 0 :(得分:42)
在SQL中执行此操作的方法:
SELECT SUBSTRING( string , LEN(string) - CHARINDEX('/',REVERSE(string)) + 2 , LEN(string) ) FROM SAMPLE;
JSFiddle here http://sqlfiddle.com/#!3/41ead/11
答案 1 :(得分:10)
SELECT REVERSE(LEFT(REVERSE(columnName), CHARINDEX('/', REVERSE(columnName)) - 1))
FROM tableName
ORHER SOURCE(s)
答案 2 :(得分:5)
请尝试:
select url,(CASE WHEN CHARINDEX('/', url, 1)=0 THEN url ELSE RIGHT(url, CHARINDEX('/', REVERSE(url)) - 1) END)
from(
select 'Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx' as url union
select 'OurStory/MeettheFoodieandtheMD.aspx' as url union
select 'MeettheFoodieandtheMD.aspx' as url
)xx
答案 3 :(得分:4)
SELECT REVERSE ((
SELECT TOP 1 value FROM STRING_SPLIT(REVERSE('Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx'), '/')
)) AS fName
Result: Diet.aspx
标准STRING_SPLIT
不允许取最后一个值。
诀窍是在用REVERSE
分割之前反转字符串(STRING_SPLIT
),从末尾获取第一个值(TOP 1 value
),然后需要再次反转结果( REVERSE
)恢复原始字符序列。
在使用SQL表时,这是常见的方法:
SELECT REVERSE ((
SELECT TOP 1 VALUE FROM STRING_SPLIT(REVERSE(mySearchString), '/')
)) AS myLastValue
FROM myTable
答案 4 :(得分:1)
请尝试以下代码:
SELECT SUBSTRING( attachment, LEN(attachment)
- CHARINDEX('/', REVERSE(attachment)) + 2, LEN(attachment) ) AS filename
FROM filestable;
答案 5 :(得分:1)
更简单,更优雅:
select reverse(SPLIT_PART(reverse('Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx'), '/',1))
答案 6 :(得分:0)
我更正了jazzytomato的单字符令牌(Diet.aspx
)和单个令牌(SELECT SUBSTRING( string , LEN(string) - CHARINDEX('/','/'+REVERSE(string)) + 2 , LEN(string) ) FROM SAMPLE;
)的解决方案
uib-dropdown
答案 7 :(得分:0)
尝试一下。更简单。
SELECT RIGHT(字符串,CHARINDEX('/',REVERSE(字符串))-1) FROM TableName
答案 8 :(得分:0)
Create Table #temp
(
ID int identity(1,1) not null,
value varchar(100) not null
)
DECLARE @fileName VARCHAR(100);
INSERT INTO #temp(value) SELECT value from STRING_SPLIT('C:\Users\Documents\Datavalidation\Input.csv','\')
SET @fileName=(SELECT TOP 1 value from #temp ORDER BY ID DESC);
SELECT @fileName AS File_Name;
DROP TABLE #temp
答案 9 :(得分:0)
一种稍微更紧凑的方法(类似于 ktaria 的答案,但在 SQL Server 中)是
SELECT TOP 1 REVERSE(value) FROM STRING_SPLIT(REVERSE(fullPath), '/') AS fileName
答案 10 :(得分:0)
PostgreSQL 的等价物:
SELECT reverse(split_part(reverse(column_name), '/', 1));
答案 11 :(得分:-1)
reverse(SUBSTRING(reverse(yourString),0,CHARINDEX('/',reverse(yourString)))) as stringLastPart