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?
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)
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
U can write the query in this way. SELECT NAME, 'BIRTHDATE'= MONTH(BIRTHDATE) FROM TABLE WHERE MONTH(BIRTHDATE)=1 Thanks Rushendra
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).
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...
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