sql server收缩> 5 varchar字符串以适合varchar(4)

时间:2016-10-11 17:21:03

标签: sql function truncate shrink

我对SQL函数说再见了,这是我在搜索了很多关于这个主题后的第一个问题。

我试图选择varchar(8)的值列表并将它们转换为varchar(4) 要做到这一点,我需要截断第三个位置的值,并附加一个从1到9不等的字符,然后从1重新开始

例如PATOL,PATOLB,PATOLC应该成为PAT1,PAT2,PAT3等等

我在网上发现了一段我想尝试改编的代码,但我对计数器的增量感到麻烦

请对这些错误保持耐心,对于非优雅的形式,这是我自80年代的Commodore 64 BASIC以来首次尝试编码

这是我的职责:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER function [dbo].[f_To4char] (@value varchar(10))
RETURNS varchar(4)
AS
BEGIN
    DECLARE @seq varchar(9)
    DECLARE @digit char (1)
    DECLARE @3car varchar(3)
    DECLARE @result varchar(4)
    DECLARE @count int

SET @seq = '123456789'
SET @count = 1

    IF len(@value)<=4
    return @value
else

BEGIN   
WHILE (@count <=9)

        BEGIN
        SET @digit = SUBSTRING(@seq,@count,1)
        SET @count = @count+1
        SET @3car = substring (@value,1,3)
        SET @result = @3car+@digit
        END
END
RETURN @result

END

如果值为&lt; = 4个字符长,则正确返回值,并正确地将较长的值截断为3个字符,但始终附加相同的字符而不是从1到9循环。

你能帮助我让它发挥作用吗? 谢谢

编辑(2016-10-12) 感谢您的回答和评论 与此同时,我尝试了以下内容,但无事可做:它总是返回1作为附加的字符

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

go

ALTER FUNCTION [dbo].[f_To4char](@value VARCHAR(10))
RETURNS varchar(4)
AS
BEGIN
        DECLARE @digit char(1);
        DECLARE @count smallint;
        DECLARE @3car VARCHAR(3);
        DECLARE @shrunk varchar(4);
        DECLARE @result VARCHAR(4);

set @count=1 ;

        IF LEN(@value) <= 4
           RETURN @value;
        ELSE



        BEGIN

            begin
             if @count=1 
             set @digit = '1';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=2 
             set @digit = '2';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=3 
             set @digit = '3';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=4 
             set @digit = '4';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end         

            begin
             if @count=5 
             set @digit = '5';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=6 
             set @digit = '6';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=6 
             set @digit = '6';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=7 
             set @digit = '7';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=8 
             set @digit = '8';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end

            begin
             if @count=9 
             set @digit = '9';
             SET @3car = SUBSTRING(@value, 1, 3);
             SET @shrunk = @3car + @digit;
             set @count=@count+1    
             RETURN @shrunk;    
            end         




if @count=10 set @count=1



        END;
      return @shrunk
 END;

1 个答案:

答案 0 :(得分:0)

这是你想要做的:

SET ANSI_NULLS ON;

SET QUOTED_IDENTIFIER ON;
GO

ALTER FUNCTION [dbo].[f_To4char](@value VARCHAR(10))
RETURNS VARCHAR(4)
AS
    BEGIN
        DECLARE @seq VARCHAR(9);
        DECLARE @digit CHAR(1);
        DECLARE @3car VARCHAR(3);
        DECLARE @result VARCHAR(4);
        IF LEN(@value) <= 4
           RETURN @value;
        ELSE
           BEGIN
              SET @digit = LEN(@value) - 4; --<--this may have to be 3 rather than 4 depending on what you want the suffix to be.
              SET @3car = SUBSTRING(@value, 1, 3);
              SET @result = @3car + @digit;
           END;
        RETURN @result;
    END;

避免在任何地方使用循环。它们导致性能下降。