concatenating smallint and bit into a select

  • Is it possible to concatenate a small int field and a bit field

    i have two fields as Id and isactive id is smallint and ISactive is a bit field, i have to query these two but want to produce the result in a single column with concatenation

    any ideas

    ** it doesn't work with CAST

  • Well ya, convert both to varchar(x) and then concat.

    But why do you need to do this? Never seen that before...

  • Here's a quick demo SQLTestUser to show the technique:

    IF OBJECT_ID(N'tempdb..#temp_table') > 0

    DROP TABLE #temp_table ;

    CREATE TABLE #temp_table

    (

    type_smallint SMALLINT,

    type_bit BIT

    ) ;

    INSERT INTO #temp_table

    (type_smallint, type_bit)

    VALUES (0, 0),

    (0, 1),

    (1, 0),

    (1, 1) ;

    GO

    SELECT CAST(type_smallint AS VARCHAR(5)) +

    CAST(type_bit AS CHAR(1)) AS my_concat_value

    FROM #temp_table ;

    GO

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanks it worked like a charm

  • Sure, HTH 🙂

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Just an FYI, trying to concat anything numerical tends to add it, not to concat it. So you will always need to convert numbers to strings before concateonating to make sure they don't decide to sum themselves up.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

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

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