I am attempting to query a database and have spent more time on this relatively simple query statement than I had planned. The table stores the BIRTH_DATE value as a CHAR data type, so I need to convert it to a SMALLDATETIME data type in order to perform any calculations on it. I am new to Transact SQL so am having a significant amount of trouble working through this. I'd appreciate any help you could throw my way!! Here's what I am trying to do....I am going to develop a series of SELECT statements which I will join via UNION statements that will show members who are '18 years of age and under'; '18-25 years of age'; '25-35 years of age'; etc etc etc. I am having a difficult time trying to figure out how to code the end of this statement that ascertains the number of members who fall into the age group. Here's what I have so far:
Code:
Declare @total Decimal (8,2)
------
SELECT CASE WHEN Member.Birth_Date = '00000000'
THEN 'Member Birthdate Unknown'
ELSE 'Under 18 years of age'
END AS Age,
COUNT(*) AS 'NumberOfMembers',
'1' AS 'orderby',
(CONVERT( decimal(8, 2), COUNT(*)) / @Total * 100) AS 'Percentage'
FROM dbo.Member_Association, dbo.Member -- unknown how you are joining these two tables...
WHERE (Status = 'A')
AND (Primary_Indicator = 'P')
AND( Member_Type_Code = 'R' OR Member_Type_Code = 'RA')
AND( SELECT CONVERT( smalldatetime, Member.Birth_Date) ???????? < DATEADD( year, - 1, GETDATE()) AND GETDATE()
ate = '00000000')