0

I've got the following piece of code which exectues within no problem

SELECT (
   CASE WHEN 
       (SELECT DateDiff (Day, (
        SELECT ChildDOB1 FROM MatterDataDef Where ptMatter = $Matter$), 
        GETDATE()))>6574 THEN '(over 18)' 
   ELSE '(' + ChildDOB1 + ')' 
   END)  
FROM dbo.MatterDataDef WHERE ptMatter = $Matter$ 

Howeveer, when i attempt to wrap the code in brackets (so i can use it as part of a longer equation) i get the following error 'Conversion failed when converting datetime from character string'

any help appreciated :)

3
  • what is the format of data in ChildDOB1, is it a datetime? Commented Jan 13, 2011 at 11:12
  • @cyberkiwi - doesn't James state it in his last line ? Commented Jan 13, 2011 at 11:17
  • @Jason Jong - which last line? do you mean the error text? The error could also occur if childdob1 itself is not datetime, because datediff is happening first Commented Jan 13, 2011 at 11:32

2 Answers 2

1
SELECT CASE
           WHEN Datediff (DAY, childdob1, Getdate()) > 6574
           THEN '(over 18)'
           ELSE '(' + convert(varchar,childdob1) + ')'
         END
FROM   dbo.matterdatadef
WHERE  ptmatter = $matter$

This is equivalent to your code. The extra subquery and brackets are all unnecessary. The problem is that you are adding childdob1 (datetime) to the brackets without converting to varchar. But did you actually want the DOB in a particular format, or the age?

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, thts got it working (plus thanks again for striping down my code for me, i'm new to sql and nice to learn new things)... dateformat i've got convert(varchar(10),childdob1, 103) ;)
1

where you have

ELSE '(' + ChildDOB1 + ')' 

you'll need to convert it to a character type.. ie varchar

ELSE '(' + cast(ChildDOB1 as varchar(50)) + ')' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.