Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with Date Time Query

Author  Topic 

joe8079
Posting Yak Master

127 Posts

Posted - 2011-11-10 : 21:01:33
I'm trying to return just the year in a date so I can use it as a constant within a variable, but i'm running into problems and i've searched all over the internet.

Declare @mydate as datetime
set @mydate = current year

I want @mydate to return only the current year and nothing else because I need to use that variable in the where statement of a select clause. I tried setting @mydate = '2011', but this did not work.

any help would be greatly appreciated.

thanks,

sql-programmers
Posting Yak Master

190 Posts

Posted - 2011-11-10 : 21:46:47

If you want only the year then declare the variable as INT, not as datetime.

Declare @mydate as INT
set @mydate = YEAR(GETDATE())

SELECT @mydate

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

joe8079
Posting Yak Master

127 Posts

Posted - 2011-11-10 : 21:51:03
interesting, I will try that tommorow.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-11 : 03:35:26
you mean to say that you're storing year,month etc seperately in your table rather than storing date itself?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -