Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-07-30 : 12:38:55
|
How can i get the years as a column in my sql query...Years201120122013if i userselect '2012' as year... I get only one value for it..How to accomadate the other values for it as shown above... The years field is not present in any table but i want to get that as separate column from a TSql query for a drop down option in one of my report..Please advise... |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-30 : 13:02:34
|
do you've a calendar table? if yes you can apply YEAR() function over datefield to get itif you dont have any such table you need to either create one table using UDF like belowhttp://visakhm.blogspot.com/2010/02/generating-calendar-table.htmlor add a temporary table like below just for this query likeDECLARE @StartYear intSET @StartYear=1998 -- just a value (you can set any value you want as start value);With YearValues (Yr)AS(SELECT @StartYearUNION ALLSELECT Yr+1FROM YearValuesWHERE Yr+1 <= YEAR(GETDATE()))SELECT YrFROM YearValuesORDER BY Yr DESC this will continue till current year and display it in descending which is how you usually show it on dropdownsi prefer udf approach as its scalable and can even be made a permanent table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-07-30 : 16:23:49
|
thank you visakh ..i created a table on the server.. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-30 : 17:09:29
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|