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 2005 Forums
 Transact-SQL (2005)
 Return portion of date field

Author  Topic 

kpgraci
Yak Posting Veteran

68 Posts

Posted - 2010-09-16 : 16:26:31
Hi all,

Perhaps a little odd, but...

I need to return a unique list of date parts, like the year or month, from a set of records.

So for example if I have a record with fields: name, date, etc

john, 12/31/2009, somedata
fred, 2/15/2010, somedata
sam, 5/21/2010, somedata

I would get back:

Year
2009
2010

and also (in another query for year 2010)

Month
2
5

Possible?

If not, I know I can add these fields in the db as Year, Month
and then query with DISTINCT. Even if there were a way to do what I want, would this second method be better for query performance (at the expense of data size)?

Thanks,
kpg


kpg

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-16 : 16:39:13
month(your_dateColumn)
year(your_dateColumn)
are your friends


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

kpgraci
Yak Posting Veteran

68 Posts

Posted - 2010-09-16 : 17:35:05
quote:
Originally posted by webfred

month(your_dateColumn)
year(your_dateColumn)
are your friends



That's it. Thanks!

kpg
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-17 : 03:16:41
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -