Dynamic value as an alias name

  • Hi All,

    How can i use the dynamic value as an alias name. Please refer the below example.

    DECLARE @a varchar(10) -- declaration of variable

    SET @a = DATEPART(YEAR,DATEADD(YEAR,0,'01/01/2009'))

    SELECT

    CASE WHEN 2009 = 2009 THEN 1

    ELSE 0

    END AS @a --> i want to use this @a value here

    ---

  • DECLARE @a varchar(10) -- declaration of variable

    SET @a = DATEPART(YEAR,DATEADD(YEAR,0,'01/01/2009'))

    DECLARE @SQL NVARCHAR(200)

    SELECT @SQL = 'SELECT CASE WHEN 2009 = 2009 THEN 1

    ELSE 0

    END AS '+ QUOTENAME(@A) +''

    EXECUTE( @SQL)

    John Smith

  • it works.

    thanks mangal.

  • Just curious... why do you need to do this?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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