如何从表中包含日期的Sql表中获取不同年份 [英] How to Get the Distinct Years from Sql Table having Dates in the Table

查看:96
本文介绍了如何从表中包含日期的Sql表中获取不同年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期的表。

从这个日期表中包含

的日期,从过去的5年到现在。

我有经验Asp加载DropDownBox的要求

表中可用的年份

我想从
$ b加载DropDownBox与年份不同$ b表按升序排列。问题是我表中没有年数

而现在还有交易日期。

如何实现这一目标。

我希望Sql Query从表格中的日期获得

表中的所有明显年份

请协助。

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.

推荐答案

select distinct year(column) from TableName order by ASC


您好,



请尝试以下查询



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



问候,

GVPrabu


Regards,
GVPrabu


这篇关于如何从表中包含日期的Sql表中获取不同年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆