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
 General SQL Server Forums
 New to SQL Server Programming
 Best Approach

Author  Topic 

Rayman
Starting Member

39 Posts

Posted - 2010-09-22 : 15:03:01
I have the following table with the data:

Database_date

11/30/08 1:52 AM
12/31/08 9:22 AM
3/17/08 11:22 AM
1/4/09 11:22 PM

I would like to determine how to produce the sql code to display the information by the following output;

Month AM PM
1 November YES NO
2 December NO YES
3 March YES NO
4 January NO YES

Any suguestions would be appreciated. Thank you

Terry L King

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-09-22 : 15:09:36
DECLARE @Table TABLE (database_date datetime)

insert into @Table


SELECT '11/30/08 1:52 AM' UNION
SELECT '12/31/08 9:22 AM' UNION
SELECT '3/17/08 11:22 AM' UNION
SELECT '1/4/09 11:22 PM'


SELECT datename(month,database_date) as [Month]
,CASE WHEN datepart(hour,database_date) >=12 THEN 'YES' ELSE 'NO' END as PM
,CASE WHEN datepart(hour,database_date) <12 THEN 'YES' ELSE 'NO' END as AM

FROM @table

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-23 : 12:37:20
I assume the december values in sample is a typo as i see time as AM but it has just opposite values in output

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -