Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

sql server - Retrieving row and column number from within a string in sql

I am trying retrieve row and column number for following strings. The numbers that follow the R are the row no and numbers that follow the C are column numbers. Also as you can see some of them are 4 digits and some of them are 3 digits. How do I use a single function to retrieve the row and column number respectively.

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 

I started by using the patindex but not sure how to determine based on it. Could somebody help

 SELECT PATINDEX('%R%C%','C75.01R2220C050')
question from:https://stackoverflow.com/questions/65645224/retrieving-row-and-column-number-from-within-a-string-in-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This forum isn't for solving your programming tasks. But for the fun of it, I've done it for the row number:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...