Can I declare a year somehow in SQL Server 2008?
I want to delclare a year such as :
DECLARE @Year varchar
and populate this with something such as :
select @Year = year('2011')
Can I do this or must I create an entire DATE and populate it with filler information?
I wish to have a reuseable year vairable rthat I can inject into my queries.
Thanks in adva开发者_如何学Pythonnce for your help and suggestions.
I performed this google search and also this google search
I think I may be on to a solution but, if there is a quick way please let me know. I will update if I figure it out.
I believe I need to add some additional information for clarification.
I need this variable in order to perform some date comparisons on dates that are in my database. I want to check to verify that the variable @Year is equal to the year of the date in the database.
YEAR(EntryDate) = YEAR('2011')
or
my new method
YEAR(EntryDate) = YEAR(cast(@year as varchar))
This is a part of my where statement in my query.
1 - Why make it a varchar
instead of an int
? You want 2011
not Two thousand and eleven
right?
2 - You can always use the YEAR(GETDATE())
function to get the current year.
Syntax for a numerical value would be
DECLARE @Year int = YEAR(GETDATE())
EDIT:
Example of why it is bad to use varchar
for a number comparison:
With int
:
21 < 2010 = TRUE
With varchar
:
21 < 2010 = FALSE
精彩评论