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 2008 Forums
 Transact-SQL (2008)
 Date Help!

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-11-29 : 14:57:35
I'm getting an average number of days in a particular query. However, I want to turn that data into meaningful information. So for example, if my average number of days is 365, I want column_B to be '1 Year', if the average number of days is 7, I want column_B to say '1 Week', if my average number of days is 372, I want column_B to say '1 Year, 1 Week'. Is there any easy way to do this in SQL?

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-11-29 : 18:59:18
[CODE]SELECT
CASE
WHEN AvgNumDays >= 365 then CAST(AvgNumDays / 365 as varchar(10)) + ' Years, ' else '' end + -- Yes, I am ignoring leap years
CASE
WHEN AvgNumDays % 365 >= 7 then CAST((AvgNumDays % 365) / 7 as varchar(10)) + ' Weeks, ' else '' end +
CAST(((AvgNumDays % 365) % 7 as varchqar(10)) + ' Days' as Duration
FROM MyTable[/CODE]

=======================================
Faced with the choice between changing one's mind and proving that there is no need to do so, almost everyone gets busy on the proof. -John Kenneth Galbraith
Go to Top of Page
   

- Advertisement -