Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get year in format YY in SQL Server

select Year(Creation_Date) 
from Asset_Creation 
where Creation_Date = @Creation_Date

I am executing this query where I am getting year as 2013 when supplied today's date. I want the query to return only 13 of 2013. How can I achieve that?

like image 201
Ankur Avatar asked Aug 09 '13 07:08

Ankur


People also ask

How do I get the year from a date in SQL?

Use SQL Server's YEAR() function if you want to get the year part from a date. This function takes only one argument – a date, in one of the date and time or date data types.

How do you represent a year in SQL?

MySQL displays YEAR values in YYYY format, with a range of 1901 to 2155 , and 0000 . YEAR accepts input values in a variety of formats: As 4-digit strings in the range '1901' to '2155' .

How do I change a date format to a year in SQL?

You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')


1 Answers

Try

SELECT RIGHT(YEAR(Creation_Date), 2) YY 
  FROM Asset_Creation 
 WHERE ...

Sample output:

| YY |
------
| 10 |
| 11 |
| 13 |

Here is SQLFiddle demo

like image 86
peterm Avatar answered Oct 02 '22 20:10

peterm