ASCII function?

  • Hi, I have the following sql (PS_INSTALLATION table has only one record):

    /*-----------------------------

    SELECT ASCII(1) ASCII_1 , ASCII(14) ASCII_14, ASCII(4) ASCII_4, ASCII(1) + ASCII(4) ASCII_1_4 FROM PS_INSTALLATION

    -----------------------------*/

    ASCII_1     ASCII_14    ASCII_4     ASCII_1_4  

    ----------- ----------- ----------- -----------

    49          49          52          101

    (1 row(s) affected)

    My question : Is there any function or way to get ascii(14) equal to ascii(1) + ascii(4). As I have big number to get the ascii for, so don't want to keep adding for all digits.

    Thanks for taking time.

    Vijay

     

     

     

     

  • Not that I know of. A UDF will do this fairly easily - something like this:

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_NULLS ON

    GO

    create function dbo.ascii_big (@string varchar(1000))

    returns int

    as

    begin

    declare @position int, @total int

    SET @position = 1

    set @total = 0

    WHILE @position <= DATALENGTH(@string)

    BEGIN

    set @total = @total + (SELECT ASCII(SUBSTRING(@string, @position, 1)))

    SET @position = @position + 1

    END

    return(@total)

    end

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS ON

    GO

    select dbo.ascii_big(1) ascii_1, dbo.ascii_big(14) ascii_14

    Regards

    Phil

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply