## Case Statement - DateDiff and DatePart

 Author Message wafw1971 Old Hand Group: General Forum Members Points: 356 Visits: 155 I have been asked to come up with a case statement that gives me the date of birth, no I know there are easier ways to do it but its about writing the actual statement which I have done but I have got a bit confused.Can you Help?Declare @DOB DatetimeDeclare @Today DatetimeSet @DOB = '01 November 1971'Set @Today = GETDATE()SELECT Datediff(Year,@DOB,@Today) AS Years, Datepart(Month,@DOB) As DOB_Month, Datepart(Day, @DOB) as DOB_Day,DatePart(Month, @Today) As Current_Month, Datepart(Day,@Today) AS Current_DayCASE WHEN DatePart(Month, @Today) < Datepart(Month,@DOB) THEN Datediff(Year,@DOB,@Today) THEN -1 ELSEDatePart(Month, @Today) = Datepart(Month,@DOB) THEN Datepart(Day,@Today) < Datepart(Day, @DOB) THEN Datediff(Year,@DOB,@Today) THEN -1 ELSE END wafw1971 Old Hand Group: General Forum Members Points: 356 Visits: 155 Don't worry all sorted:Declare @DOB DatetimeDeclare @Today DatetimeSet @DOB = '01 November 1971'Set @Today = '01 November 2011'SELECTCASE WHEN DatePart(Month, @Today) < Datepart(Month,@DOB) THEN (Datediff(Year,@DOB,@Today) - 1) WHEN DatePart(Month, @Today) > Datepart(Month,@DOB) THEN Datediff(Year,@DOB,@Today) WHEN DatePart(Month, @Today) = Datepart(Month,@DOB) THEN CASE WHEN DatePart(Day, @Today) < Datepart(Day,@DOB) THEN (Datediff(Year,@DOB,@Today) - 1 ) WHEN DatePart(Day, @Today) >= Datepart(Day,@DOB) THEN (Datediff(Year,@DOB,@Today) ) END END AS How_Old_Am_I Jason-299789 SSChampion Group: General Forum Members Points: 12427 Visits: 3232 At the moment you have it written like an IF StatementCASE WHEN conditional_a = conditional_b THEN something ELSE conditional_a = conditional_c THEN something_else ELSE Default_SomethingEND With CASE statements you write them like thisCASE WHEN conditional_a = conditional_b THEN something WHEN conditional_a = conditional_c THEN something_else ELSE Default_SomethingENDHope this helps. _________________________________________________________________________SSC Guide to Posting and Best Practices