Testing String Length TSQL

doco

Power User
Local time
Yesterday, 21:48
Joined
Feb 14, 2007
Messages
482
Code:
/*
    Function receives and concatenates map, tax lot, code area and undivided
    interest data from AB_AccountMaster and returns alt_pin in the format
    
        00S00E0000-00000-00-000
        1.  the first ten are map number
        2.  the next non dash five are tax lot number
        3.  the next non dash two are code area code
        4.  the last three are undivided interest alpha and numeric

    Which creates a string of 23 char static of 26 chars possible in the
    alt_pin field and would be unique to any property in the system.  It is
    hoped to use this number for Geodatabase purposes as well as mapping 
    photos - eventually - to ProVal
 
    Donald R. Cossitt
    03/14/2007
*/

alter function fn_alt_pin
(
      @twnshp         numeric(2,0)
    , @twnshpdir      char(1)
    , @range          char(4)
    , @rangedir       char(1)
    , @section        numeric(2,0)
    , @qtrsctn        char(2)
    , @lot            numeric(5,0)
    , @district       char(6)
    , @specalpha      char(1)
    , @specnum        numeric(2,0)
)
returns char(26)
as

begin
    declare 
          @townstring char(2)
        , @rangestring char(2)
        , @sctnstring char(2)
        , @lotstring char(5)
        , @qtrsctnstring varchar(2)
        
--  set township, township direction, range, range direction
--  section and quarter-quarter section
--  probably a better way of doing this but this works
    set @district = rtrim(@district)
    set @townstring =  right( cast( @twnshp + 100 as char(3) ), 2 )
    set @twnshpdir = upper( @twnshpdir )
    set @rangestring = right( cast( cast( rtrim( @range ) as numeric(2,0) ) +
                                     100 as char(3) ), 2 )
    set @rangedir =upper( rtrim( @rangedir ) ) 
    set @sctnstring = right( cast( @section + 100 as char(3) ), 2 )
    set @qtrsctn = rtrim( @qtrsctn )

--  test for quarter section length and set accordingly
--  had to make @qtrsctnstring a variable character string  
--  or when populated @qtrsctn tests for length == 2 regardless 

/*  THIS IS THE AREA THATS GIVEN ME FITS. THIS WORKED PERFECTLY ON 
    MY LAPTOP AGAINST A COPY OF THE ProvalWheelerCommon DB
    TODAY IT IS NOT RECOGNIZING THE IF LEN(STRING) = 1
*/
    set @qtrsctnstring =  @qtrsctn 
    if len( @qtrsctnstring ) = 1
        set @qtrsctnstring = @qtrsctnstring + '0'
    -- end if 
    
    if len( @qtrsctnstring ) = 0 
        set @qtrsctnstring = '00'
    -- end if 
 
-- set tax lot number and code area code
    set @lotstring = cast( right( @lot + 100000 , 5 ) as char(5) )
    set @district = right( cast( rtrim( @district ) as numeric(2,0) ) + 100 , 2 )

--  test for undivided interest alpha and set accordingly
--  special interest alpha is length 0 or 1 so char(1) 
--  works ok
    if len( rtrim( @specalpha ) ) = 0 
        begin
            set @specalpha = '0'
        end
    --  end if 

    
-- return concatenated string finished off with 
-- special interest number 
    return @townstring + @twnshpdir + @rangestring + @rangedir + 
           @sctnstring + @qtrsctnstring + '-' +
           @lotstring + '-' + rtrim( @district ) + '-' + @specalpha + 
           right( cast( @specnum + 100 as char(3) ) , 2 ) 

end

Lines 65 - 75 are the offenders here. I have looked everywhere trying to understand why LEN(CHAR()) or LEN(VARCHAR) defaults to upper bound regardless of the string length at runtime.

results
Code:
lrsn        AcctNmbr  altpin                     
----------- --------- -------------------------- 
       1.00         1 06S21E0000-02600-01-000   
       2.00         2 06S21E32D -00100-01-000  <-- 
       3.00         3 06S21E32D -01000-01-000  <--
       4.00         5 06S21E33BD-06900-01-000   
       5.00         6 06S21E0000-04200-01-000   
       6.00         7 06S21E32AC-00200-01-000   
       7.00         8 06S21E32AC-00300-01-000

TIA
doco
 
Code:
set @qtrsctnstring = left( rtrim( @qtrsctn ) + '00', 2 )

I tried an entirely different approach - a better one I think. But I still do not understand why the LEN(string) = 1 did not work

Results
Code:
lrsn        AcctNmbr  altpin                     
----------- --------- -------------------------- 
       1.00         1 06S21E0000-02600-01-000   
       2.00         2 06S21E32D0-00100-01-000   <--
       3.00         3 06S21E32D0-01000-01-000   <-- 
       4.00         5 06S21E33BD-06900-01-000   
       5.00         6 06S21E0000-04200-01-000   
       6.00         7 06S21E32AC-00200-01-000   
       7.00         8 06S21E32AC-00300-01-000   
       8.00         9 06S21E32AC-00400-01-000   
       9.00        10 06S21E32AC-00500-01-000   
...
 

Users who are viewing this thread

Back
Top Bottom