SQL Server Performance

Dynamic Alias

Discussion in 'General Developer Questions' started by matrix, May 28, 2003.

  1. matrix New Member

    This one is crazy....

    I need to dynamically give an alias to a field of my select. Something like this:

    SELECT NAME, BIRTHDATE AS MONTH(BIRTHDATE) FROM TABLE WHERE MONTH(BIRTHDATE)=1


    The tricky part is the alias for DATEBIRTH, Is this possible?
  2. matrix New Member


    declare @mes smallint, @extenso as varchar(255)
    set @mes=datepart(mm,getdate())
    set @extenso = DATENAME(month, getdate())
    EXEC('select fdata as '''+@extenso+''' from ft where month(fdata)='+@mes)
  3. rushmada New Member

    U can write the statement like this.

    SELECT A.NAME, B.BIRTHDATE FROM TABLE A,
    (SELECT MONTH(BIRTHDATE) AS BIRTHDATE FROM TABLE WHERE MONTH(BIRTHDATE)=1) B



    quote:Originally posted by matrix

    This one is crazy....

    I need to dynamically give an alias to a field of my select. Something like this:

    SELECT NAME, BIRTHDATE AS MONTH(BIRTHDATE) FROM TABLE WHERE MONTH(BIRTHDATE)=1


    The tricky part is the alias for DATEBIRTH, Is this possible?

    Rushendra
  4. rushmada New Member

    U can write the query in this way.

    SELECT NAME, 'BIRTHDATE'= MONTH(BIRTHDATE) FROM TABLE WHERE MONTH(BIRTHDATE)=1


    Thanks

    Rushendra
  5. matrix New Member

    You havn't got it:

    I don´t whant the alias BIRTHDATE on the column.
    If the month of the field birthdate is january, I want the alias to '1', ... if it is feb it should be '2'.

    Look at my code (first reply).

  6. Chappy New Member

    Dynamic SQL is the only way to do what you want. You seem to have found your solution in your second post?
    Admittedly I cant think of a reason why you want to structure the result set like this...
  7. rushmada New Member

    Matrix,

    This query has developed on the query which was given by u in the first reply.

    declare @mes smallint, @extenso as varchar(255)
    set @mes=datepart(mm,getdate())
    set @mes=datepart(mm,getdate())
    set @extenso = DATENAME(month, getdate())
    declare @cmd varchar(8000)
    select @cmd='select BIRTHDATE as ' + "'"+ @extenso +"'" + 'from dob where month(BIRTHDATE)='+ltrim(rtrim(str(@mes)))
    exec(@cmd)

    Here my understanding is Birthdate will be aliased as 'January','February'..etc.
    I hope this query will works out for u

    Thanks.






    quote:Originally posted by matrix

    You havn't got it:

    I don´t whant the alias BIRTHDATE on the column.
    If the month of the field birthdate is january, I want the alias to '1', ... if it is feb it should be '2'.

    Look at my code (first reply).



    Rushendra

Share This Page