A column contains both CHAR and INTEGER

  • Hello, I have a column contains both CHAR and INTEGER.

    I need to write a stored procedure to handle them differently :

    IF the column contains CHAR, eg. 'A01', 'B02', 'C03'

    Display it as : 'A01'

    IF the column contains integer numbers (but their data types are still CHAR, of course)

    Display it as : integer number x 3

    How should I write this program ?

  • -- *** Test Data ***

    DECLARE @t TABLE

    (

    YCol varchar(10) NOT NULL

    )

    INSERT INTO @t

    SELECT 'A01'

    UNION ALL SELECT 'B02'

    UNION ALL SELECT 'C03'

    UNION ALL SELECT '2'

    -- *** End Test Data ***

    SELECT

    CASE

    WHEN YCol LIKE '%[^0-9]%'

    THEN YCol

    ELSE CAST(YCol * 3 AS varchar(10))

    END AS YCol

    FROM @t

  • Thanks

  • isnumeric(column name) to check whether the column has any numeric values in it.

  • Kalyan, no, ISNUMERIC cannot be used as a detector to check if a column contains only numerics

    Try this:

    DECLARE @t TABLE

    (

    YCol varchar(10) NOT NULL

    )

    INSERT INTO @t

    SELECT 'A01'

    UNION ALL SELECT 'B02'

    UNION ALL SELECT 'C03'

    UNION ALL SELECT '2'

    UNION ALL SELECT '1,' -- this will be a good numeral according to ISNUMERIC, but is it really?

    UNION ALL SELECT CHAR(10) -- this is line feed

    SELECT YCol FROM @t

    WHERE ISNUMERIC(YCol) = 1

Viewing 5 posts - 1 through 4 (of 4 total)

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