我有一个包含这些值的表:
Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx
OurStory/MeettheFoodieandtheMD.aspx
TheFood/OurMenu.aspx
我想得到这个
Diet.aspx
MeettheFoodieandtheMD.aspx
OurMenu.aspx
我怎样才能做到这一点?
我有一个包含这些值的表:
Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx
OurStory/MeettheFoodieandtheMD.aspx
TheFood/OurMenu.aspx
我想得到这个
Diet.aspx
MeettheFoodieandtheMD.aspx
OurMenu.aspx
我怎样才能做到这一点?
在 SQL 中执行此操作的方法:
SELECT SUBSTRING( string , LEN(string) - CHARINDEX('/',REVERSE(string)) + 2 , LEN(string) ) FROM SAMPLE;
JSFiddle 这里http://sqlfiddle.com/#!3/41ead/11
SELECT REVERSE(LEFT(REVERSE(columnName), CHARINDEX('/', REVERSE(columnName)) - 1))
FROM tableName
其他来源
请试试:
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
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
) 以恢复原始 chars 序列。
这是使用 SQL 表时的常用方法:
SELECT REVERSE ((
SELECT TOP 1 VALUE FROM STRING_SPLIT(REVERSE(mySearchString), '/')
)) AS myLastValue
FROM myTable
试试这个。这更容易。
SELECT RIGHT(string, CHARINDEX('/', REVERSE(string)) -1) FROM TableName
一种更紧凑的方法(类似于 ktaria 的答案,但在 SQL Server 中)将是
SELECT TOP 1 REVERSE(value) FROM STRING_SPLIT(REVERSE(fullPath), '/') AS fileName
更简单优雅:
select reverse(SPLIT_PART(reverse('Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx'), '/',1))
请尝试以下代码:
SELECT SUBSTRING( attachment, LEN(attachment)
- CHARINDEX('/', REVERSE(attachment)) + 2, LEN(attachment) ) AS filename
FROM filestable;
你也可以试试这个
( SELECT TOP(1) value
FROM STRING_SPLIT(#string, '/')
ORDER BY CHARINDEX('/' + value + '/', '/' + #string+ '-') DESC)
我更正了 jazzytomato 对单个字符标记 ( D
) 和单个标记 ( Diet.aspx
)的解决方案
SELECT SUBSTRING( string , LEN(string) - CHARINDEX('/','/'+REVERSE(string)) + 2 , LEN(string) ) FROM SAMPLE;
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
PostgreSQL 的等价物:
SELECT reverse(split_part(reverse(column_name), '/', 1));
reverse(SUBSTRING(reverse(yourString),0,CHARINDEX('/',reverse(yourString)))) as stringLastPart