-1

我正在尝试检索以下字符串的行号和列号。R后面的数字是行号,C后面的数字是列号。此外,您可以看到其中一些是 4 位数字,其中一些是 3 位数字。如何使用单个函数分别检索行号和列号。

NSFR80R0020C0030   e.g Row is 0020  Column is  0030
C75.01R2220C050    e.g Row is 2220  Column is  050 
C76R380C010        e.g Row is  380  Column is  010 

我从使用 patindex 开始,但不确定如何根据它来确定。有人可以帮忙吗

 SELECT PATINDEX('%R%C%','C75.01R2220C050')
4

2 回答 2

1

parsename()配合 使用CROSS APPLY

例子

Declare @YourTable Table ([SomeCol] varchar(50))  Insert Into @YourTable Values 
 ('NSFR80R0020C0030')
,('C75.01R2220C050')
,('C76R380C010')
 
Select SomeCol
      ,Row = parsename(rS,2)
      ,Col = parsename(rS,1)
 From @YourTable A
 Cross Apply ( values (replace(right(SomeCol,charindex('R',reverse(SomeCol))-1),'C','.'))) B(rS)

退货

SomeCol             Row     Col
NSFR80R0020C0030    0020    0030
C75.01R2220C050     2220    050
C76R380C010         380     010
于 2021-01-09T17:32:57.673 回答
-1

这个论坛不是用来解决你的编程任务的。但是为了好玩,我已经为行号做了:

create FUNCTION getRow 
(
    @s varchar(max)
)
RETURNS varchar(4)
AS
BEGIN
    DECLARE @Result varchar(4)
    declare @ind int

    SET @ind = PATINDEX('%R[0-9][0-9][0-9][0-9]%', @s)
    if @ind > 0
        set @Result = SUBSTRING(@s, @ind + 1, 4)
    else begin
        SET @ind = PATINDEX('%R[0-9][0-9][0-9]%', @s)
        if @ind > 0
            set @Result = SUBSTRING(@s, @ind + 1, 3)
        else
            set @Result = ''
    end

    RETURN @Result

END
于 2021-01-09T17:37:23.857 回答