Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从sql中的字符串中检索行数和列数_Sql_Sql Server - Fatal编程技术网

从sql中的字符串中检索行数和列数

从sql中的字符串中检索行数和列数,sql,sql-server,Sql,Sql Server,我正在尝试检索以下字符串的行和列编号。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,

我正在尝试检索以下字符串的行和列编号。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')
与交叉应用配合使用
parsename()

示例

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

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

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

用您正在使用的数据库标记您的问题。另外,要非常清楚字符串格式(它似乎包含其他信息)。期望的结果会有所帮助。我已经标记了“谢谢”,请更具体地说明您的逻辑。行数在R和C之间?列跟随C到字符串的末尾?总是吗?字符串格式没有例外?别猜了。不要想当然。不要只看一些小样本行。您使用哪个SQL Server版本?