简体   繁体   English

从SQL Server中的字符串末尾提取子字符串

[英]Extract a substring from the end of a string in sql server

I have following string : 我有以下字符串:

DECLARE @build_names VARCHAR(5000) = NULL; 

SET @build_names = 'BB10-1_X-4759-566549'; 

i want to extract it from the last, - is the delimiter. 我想从最后一个提取它, -是分隔符。 The string will be extracted into 3 sub-strings ie 566549 , 4759 , BB10-1_X . 该字符串将被提取到3的子串即5665494759BB10-1_X

Please help me, I am a beginner. 请帮助我,我是初学者。

You can use charindex() and reverse() to get your desired results: 您可以使用charindex()reverse()获得所需的结果:

declare @temp varchar(40)
set @temp = 'BB10-1_X-4759-566549'

select @temp, REVERSE(@temp)
select REVERSE(substring(REVERSE(@temp),0,CHARINDEX('-',REVERSE(@temp),0)))

Give this a shot. 试一下。 It answers your first question of extracting from after the last - of the string. 它回答你从上次提取后的第一个问题-字符串。

The next part of your question seems to indicate that you want to split the entire thing up based on the - . 问题的下一部分似乎表明您想基于-拆分整个事物。 Using stored procedures or functions will fail because you want BB10-1_X to be a single string. 使用存储过程或函数将失败,因为您希望BB10-1_X为单个字符串。 So if these strings are always in this format of having exactly three - 's but you only want 3 substrings, you can hard code it like this. 因此,如果这些字符串总是恰好三个具有这种格式-的,但你只需要3子,你可以硬这样的代码吧。

declare @temp varchar(40), @reverse varchar(40), @sub1 varchar(20), @sub2 varchar(20), @sub3 varchar(20)

SET @temp = 'BB10-1_X-4759-566549'
SET @reverse = REVERSE(@temp)

SET @sub3 = REVERSE(substring(@reverse,0,CHARINDEX('-',@reverse,0)))

SELECT @temp = substring(@temp,0,charindex(REVERSE(substring(@reverse,0,CHARINDEX('-',@reverse,0))),@temp,0)-1)
SELECT @reverse = REVERSE(@temp)

SET @sub2 = REVERSE(SUBSTRING(@reverse,0,CHARINDEX('-', @reverse, 0)))
SET @sub1 = REVERSE(SUBSTRING(@reverse,CHARINDEX('-',@reverse,0)+1,LEN(@temp)-CHARINDEX('-',@reverse,0)))

select @sub1, @sub2, @sub3

There has been a lot written on efficient splitting functions for TSQL. 关于TSQL的高效拆分功能,已经写了很多文章。 I suspect that diving into that is probably overkill for this question as it is very specific. 我怀疑,由于这个问题非常具体,因此深入探讨该问题可能是过大的。

Here's something that demostrate some basic TSQL string manipulation. 这里介绍了一些基本的TSQL字符串操作。

NOTE: I use a common table expression (CTE) for clarity, but those manipulations could be done in line. 注意:为了清楚起见,我使用公用表表达式(CTE),但是可以按行完成这些操作。

DECLARE @build_names VARCHAR(5000) = NULL

SET @build_names = 'BB10-1_X-4759-566549'

;WITH cte AS (
 SELECT CHARINDEX('-',REVERSE(@build_names),1) RevPosLastSep
       ,CHARINDEX('-',@build_names,CHARINDEX('-',@build_names,1)+1) PosFirstSep
)
SELECT RIGHT(@build_names,RevPosLastSep-1)
      ,SUBSTRING(@build_names,PosFirstSep+1,LEN(@build_names) - RevPosLastSep - PosFirstSep)
      ,LEFT(@build_names,PosFirstSep-1)
  FROM cte

You might want to create split function 您可能要创建拆分功能

CREATE FUNCTION [dbo].[fnSplitString] 
( 
@string NVARCHAR(MAX), 
@delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
DECLARE @start INT, @end INT 
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
WHILE @start < LEN(@string) + 1 BEGIN 
    IF @end = 0  
        SET @end = LEN(@string) + 1

    INSERT INTO @output (splitdata)  
    VALUES(SUBSTRING(@string, @start, @end - @start)) 
    SET @start = @end + 1 
    SET @end = CHARINDEX(@delimiter, @string, @start)

END 
RETURN 
END

Exceute this T-sql statements to create function and use as 执行此T-sql语句以创建函数并用作

select 4,3,1+'-'+2 from dbo.fnSplitString('BB10-1_X-4759-566549','-')

source: http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/ 来源: http : //www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM