Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Please Assist.
Posted
Comments
gvprabu 8-Jul-13 9:24am    
Hi You need to show past 5 years in drop down right

SQL
select distinct year(column) from TableName order by ASC
 
Share this answer
 
v2
Comments
Karwa_Vivek 8-Jul-13 9:46am    
select Distinct DATEPART (year, column) FY from TableName order by FY ASC
--thanks I used Like this
Hi,

Try like below Query

SQL
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900