後,我在這個形式的字符串的select語句的結果:SQL - 訂單由特定符號
"123400000000020~forename~surname~P50206.00011"
"123400000111111~forname1~surname1~P50206.00011"
....
我怎麼可以在SQL姓氏排序?他們是否有可能在第二個「〜」中切斷琴絃?
後,我在這個形式的字符串的select語句的結果:SQL - 訂單由特定符號
"123400000000020~forename~surname~P50206.00011"
"123400000111111~forname1~surname1~P50206.00011"
....
我怎麼可以在SQL姓氏排序?他們是否有可能在第二個「〜」中切斷琴絃?
CHARINDEX
是你的朋友在這裏。您將需要將其嵌套在自身內部,並運行兩次,因爲您需要第二個實例。
DECLARE @SearchString nvarchar(max)
SET @SearchString = '123400000111111~forname1~surname1~P50206.00011'
DECLARE @Delimiter nvarchar(1)
SET @Delimiter = '~'
/*
SELECT @SearchString AS [Your String]
--the string puked back
SELECT
RIGHT
(
@SearchString,
LEN(@SearchString) - charindex(@Delimiter, @SearchString)
) AS [The First Instance]
--first instance of @Delimiter
*/
SELECT
RIGHT
(
RIGHT
(
@SearchString,
LEN(@SearchString) - CHARINDEX(@Delimiter, @SearchString)
),
LEN
(
RIGHT(@SearchString, LEN(@SearchString) - CHARINDEX(@Delimiter, @SearchString))
) -
CHARINDEX
(
@Delimiter, RIGHT(@SearchString, LEN(@SearchString) - CHARINDEX(@Delimiter, @SearchString))
)
) AS [Your Awful Order By]
--what you're after
但對於所有的愛在數據庫的世界,以及你自己的理智的緣故神聖的,更何況性能,不存儲這樣的數據!
感謝我說我們現在將數據保存在單獨的列中。但我會嘗試以上。 –
我認爲這會對你有所幫助;
select substr(somedata, regexp_instr(somedata, '(~)', 1, 2, 1) , regexp_instr(substr(somedata, regexp_instr(somedata, '(~)', 1, 2, 1)), '(~)', 1, 1, 1)-2) as surname from (
select '123400000000020~forename~surname~P50206.00011' as somedata from dual
union
select '123400000111111~forname1~surname1~P50206.00011' as somedata from dual
union
select '123400000000020~forename~surname3~' as somedata from dual
union
select '3~~surname5~P50206.00011' as somedata from dual
)
order by 1 asc
請用你正在使用的數據庫標記你的問題? –
不要存儲這樣的數據,它只會導致你很多麻煩!爲不同的值分開列。 – jarlh
如果您使用SQL Server:http://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can-access-item-x – nicomp