views:

734

answers:

4

Hello group, I am trying to create a WHERE clause that says

WHERE column_1 = TRIM(LEADING '20' FROM(DATEPART(year, GETDATE())))

Basically column_1 contains the fiscal year but the fiscal year is in the database as 8 or 9 not 2008 or 2009.

So I need to figure a way to trim off at least the '20' so that the query will run correctly... The more dynamic the better because I need to set this up to run in an SSIS pkg and the less hard coding the better.

Any suggestions? Thanks in advance! Jon

+1  A: 

depending on the datatype of the column_1

print right(datepart(year,getdate()),2) --string with leading zero print convert(int,right(datepart(year,getdate()),2))--int

EDIT

based on @Richard's answer
print datepart(year,getdate()) % 100

KM
You beat me to it! Hey Jon, what mike said! :P
SirDemon
+4  A: 

Rather than trim, use modulus:

...
WHERE column_1 = (YEAR(GETDATE()) % 100)
Richard
I like the % 100 idea, but that is not T-SQL and will not work in a sql server where clause
KM
@mike: This is perfectly valid T-SQL.
Tomalak
"==" does not work on my sql server
KM
Use just "="
Rashack
@mike: Your comment was referring to the %, not to the "==", which is very obviously a typo and no reason to down-vote.
Tomalak
@mike: I explicitly checked that it is (SQL Server 2005 BOL at least).
Richard
@Tomalak, I said "I like the % 100 idea", however I can see your point that it is not clear. I was referring to the "where clause", should have said the "==" is not T-SQL. I did not down vote. @Richard, +1, I like your answer best, YEAR() is nicer than DATEPART()
KM
A: 

Isn't there a convert format that will only print the last 2 digits of the year?

Arnshea
A: 

If your column is guaranteed to refer to years >= 2000 and < 2100, then you would get away with the rather obvious:

SELECT YEAR(GETDATE()) - 2000

But... Honestly, it would make me weep to see such code in a production environment. That's just a bit too much WTF, imo.

Tomalak