In this article I'll tell you how to find year, month and day from a date separately and sort them.
Introduction
This article explains how to find year, month and day from date separately and how to order them.
If you have a column in which date has been entered but you don't want to fetch the complete date and want to get only the part of it that might be it's year, month or day then you can follow this article because here I explain how to do this.
YEAR
SQL provides a predefined function for finding the year. Now for example if you have a column named "age" then write this code to find it's year:
Now suppose you want to get the result in ascending order then you might use this code:
But this code will not provide you the expected result because you are finding the year from the date but you are applying the order by on the complete column, so to get the right result you need to modify your code to this:
MONTH
SQL provides a predefined function for month as well, now I'll show you how to use this.
Now again for finding the result in a specific order you need to use order by and this time also it should be applied on the month of the column.
DAY
To find the day, use this code:
And for ordering use this code:
Building Standalone Data-Driven Applications using SQL Server Compact 4.0