views:

59

answers:

3

how to take the year using date stored in DataBase:

in Sql query ?? ????

eg date stored as

2010-04-29 15:53:09.577

how to get the year in stored procedure to check whether the year i pass and the year stored in database are same

+3  A: 

That depends on the database. Try using the YEAR function.

E.g.,

IF YEAR(MyDateColumn) = @year ...
RedFilter
+1  A: 

To check a single value you can use the year function to extract the year from a date.

If you are comparing a year to a date in all records in a table, you should instead convert the year to a span of dates:

select SomeFields
from TheTable
where TheDate >= '2010-01-01' and TheDate < '2011-01-01'

It's a lot faster to compare the date values, as an index can be used. If you do a year function call on each value from the table, it can't use the index.

Guffa
A: 

As Guffa points out, applying a function to a column is a performance killer that crops up time and time again. An index maybe used, in so much that the function would be applied to data read from the index rather than the base table, but the net effect is the same. 1 million rows in the table and you get 1 millon date values read and YEAR() applied.

Mark Storey-Smith