Leo Loki
Leo Loki

Reputation: 2575

How select unique years from table

I have rows in a table in SQL Server 2008

rows

Tell me please how select only unique years from table?

P.S.: in this table unique year is 2013

Upvotes: 2

Views: 202

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79939

Use the YEAR function, with DISTINCT like this:

SELECT DISTINCT YEAR([date])
FROM Tablename;

SQL Fiddle Demo

This will give you:

| YEAR |
--------
| 2013 |

To use the order by clause, give it an alias and order by this alias not the original name like this:

SELECT DISTINCT YEAR([date]) AS Year
FROM Tablename
ORDER By Year;

Upvotes: 5

Related Questions