Select GetDate() Just the month or year
-
Thursday, December 21, 2006 5:26 PM
I wonder if someone can help me with getting JUST the month and year from SQL DB. Using the Select Getdate() Statement. If I need to use another one that would work that would be appretiated as well.
Thanks!
All Replies
-
Thursday, December 21, 2006 5:48 PMModerator
There are several ways to accomplish your task. In Books Online, look up [Functions], then [Date and Time Functions].
To retreive the current month as a number, use:
SELECT month( getdate() )
SELECT datepart( month, getdate() )To retreive the current month as a name, use:
SELECT datename( month, getdate() )And to retreive the current year, use:
SELECT year( getdate() )
SELECT datepart( year, getdate() ) -
Thursday, December 21, 2006 5:49 PMModerator
select month (getdate()),
year(getdate()) -
Thursday, December 21, 2006 6:09 PM
Thank you so much guys! This will help a bunch!!! Is there a way to get just the year number (06) instead of (2006)?
I am looking for a date that should look like this: 1206 If it is possible.
Thanks!!
-
Thursday, December 21, 2006 6:15 PMModerator
select year(getdate()) % 100 -
Thursday, December 21, 2006 6:22 PMThanks Waldorp!
-
Thursday, December 21, 2006 6:24 PMModerator
Here is one option:
SELECT replace( right( convert( varchar(8), getdate(), 3 ), 5 ), '/', '' ) -
Thursday, December 21, 2006 6:47 PMModerator
Waldrop wrote: select year(getdate()) % 100 Careful with this one -it will drop the leading zero and return only

-
Thursday, December 21, 2006 6:48 PMModerator
Arnie Rowland wrote: Waldrop wrote: select year(getdate()) % 100 Careful with this one -it will drop the leading zero and return only
That was odd -just put 6 in square brackets...(No negativity meant! -LOL)
-
Thursday, December 21, 2006 6:54 PMModerator
I brought a smile to me also! If you want chars and need the leading zeroes:
select right ('0' + convert (varchar (2), year(getdate()) % 100), 2)
( Thank you for the smiles; they are welcome )Dave :-)
-
Thursday, December 21, 2006 6:57 PM
Thats fine. I realized that it would only give the 6 so this is what I did:
select '0'+CAST((year(getdate()) % 100) AS CHAR(1))
It is long winded, but it gives me what I need.
-
Thursday, December 21, 2006 7:02 PMModerator
This one works relatively cleanly:
SELECT right( year( getdate() ), 2 )

