12,295,976 members (58,784 online)
Rate this:
See more:
I have a Table Containing Dates .
From this Table of Dates which Contains
dates from previous 5 years till Now.
I have the Requirement to Load a DropDownBox in Classic Asp
with the Years Available in the Table
I want to load the DropDownBox with Distinct years from the
Table in ascending order.the problem is I dont have Years in the table
whereas have the Transaction Dates Till now.
How can I achieve this.
I want the Sql Query to get All the Distinct Years in the
table from the Dates in the Table
Posted 8-Jul-13 3:20am
gvprabu 8-Jul-13 9:24am

Hi You need to show past 5 years in drop down right

Rate this:

## Solution 1

```select distinct year(column) from TableName order by ASC
```
v2
Karwa_Vivek 8-Jul-13 9:46am

select Distinct DATEPART (year, column) FY from TableName order by FY ASC
--thanks I used Like this
Rate this:

## Solution 2

Hi,

Try like below Query

```IF NOT EXISTS(SELECT * FROM table_name)
BEGIN
SELECT YEAR(GETDATE())-4 'YearList'
UNION ALL
SELECT YEAR(GETDATE())-3
UNION ALL
SELECT YEAR(GETDATE())-2
UNION ALL
SELECT YEAR(GETDATE())-1
UNION ALL
SELECT YEAR(GETDATE())
END
ELSE
BEGIN
SELECT TOP 5 T.YearList
FROM (SELECT DISTINCT YEAR(DateColumn) 'YearList' FROM table_name ) T
ORDER BY T.YearList ASC
END
```
Regards,
GVPrabu
v2

Top Experts
Last 24hrsThis month
 OriginalGriff 475 Richard MacCutchan 395 Karthik Bangalore 309 Peter Leow 258 George Jonsson 175
 OriginalGriff 9,060 ppolymorphe 5,402 CHill60 5,260 Karthik Bangalore 4,791 Richard MacCutchan 4,520