Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get/Select YY out of DATE

Status
Not open for further replies.

MichaelLewis

Programmer
Feb 5, 2011
12
FR
How do I get the YY out of my Database trdate (defined as DATE)?

i.e., I want to
SELECT * FROM transactions WHERE user = '$UserID' AND trdate = '$yy'

($yy is a 2 digit year)

Thanks,
Michael
 
Try something like that:

Code:
SELECT * FROM transactions WHERE user = '$UserID' AND Convert(char(2), trdate, 2) = '$yy'

I hope this helps.

Casas a venda em Suzano
 
oh wait... i just realized my solution (above) will not work unless $yy is a 4-digit year

can you arrange that?

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
This solution
...AND Convert(char(2), trdate, 2) = '$yy'
does not work.

I used the first solution, YEAR(trdate)='$yy' (with quotes) but I first added 2000 to my $yy. My application's data will not go back to the 1900's and I'm sure it wont be used past the expiration of the 2000's.

Many thanks for your help,
Michael
 
YEAR() produces an integer, not a string

no sense forcing a string-to-integer conversion -- i still think you should remove the quotes from $yy

:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top