2016-02-26 26 views
-4

我有以下文本Banker's XXXX YYYY〜#0018800〜MMMMM〜0401 from sql table我需要從select text中的文本中只篩選0018800我該怎麼做? DBMS是SQL Server在sql中分割字符串

+6

解決方案嚴格依賴於具體的DBMS你正在使用。無論如何,StackOverflow中幾乎所有已存在的DBMS都有幾十個類似的問題,只是使用搜索。這裏有一個可能的解決方案:[我如何拆分字符串,以便可以訪問項目x?](http://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can -access-item-x) –

回答

1

正如Andy Korneyev指出的那樣,這完全取決於DBMS。

對於SQL Server,你可以使用CHARINDEX並做類似如下:

DECLARE @Str VARCHAR(120) 

SET @Str = 'XXXX YYYY~#0018800~MMMMM~0401' 

SELECT SUBSTRING(@Str, CHARINDEX('#', @Str)+1, CHARINDEX('~', @Str)-3) 

或爲MySQL中,你可以使用SUBSTRING_INDEX

+0

這是非常有用的,我如何檢查符號#是否存在於此字符串中? – Khan

0

試試這個

declare @test nvarchar(max) ='XXXX YYYY~#0018800~MMMMM~0401' 

declare @index1 int = (charindex('#', @test)+1); 
declare @firstvariable nvarchar(max) = substring(@test, @index1 ,LEN(@test)) 

declare @index2 int = (charindex('~', @test)-2); 
select @index1,@firstvariable,@index2,substring(@firstvariable, 0,@index2) 
+1

產品特定的答案,對沒有標記dbms的問題。至少告訴我們這是爲什麼。 – jarlh

+0

這是'Sql-server' –