在sql中分割字符串
我有以下文本Banker's XXXX YYYY〜#0018800〜MMMMM〜0401 from sql table我需要从select text中的文本中只筛选0018800我该怎么做? DBMS是SQL Server在sql中分割字符串
正如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
这是非常有用的,我如何检查符号#是否存在于此字符串中? – Khan
试试这个
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)
产品特定的答案,对没有标记dbms的问题。至少告诉我们这是为什么。 – jarlh
这是'Sql-server' –
解决方案严格依赖于具体的DBMS你正在使用。无论如何,StackOverflow中几乎所有已存在的DBMS都有几十个类似的问题,只是使用搜索。这里有一个可能的解决方案:[我如何拆分字符串,以便可以访问项目x?](http://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can -access-item-x) –