error in select when converting to number and compare

  • I have a table1 with it's fields all stored as text

    Name lastname Age

    ----- -------- ----

    John Doe 23

    Mary Doe 34

    James Doe 27

    Jerry Doe 29

    My select statement needs to compare the age of an employee to a number and if it

    is greater than 25 to return 1 otherwise a blank.

    I am getting an error running the following query "Incorrect Syntax near '>' ",

    what am I doing wrong?

    Thanks in advance

    SELECT NAME, LASTNAME,

    CASE AGE

    WHEN (CAST(AGE as int) > 25 then 1

    ELSE ''

    END AS QUALIFIED

    FROM TABLE1

  • No need to CAST.

    SELECT

    NAME, LASTNAME,

    CASE WHEN AGE > 25 THEN '1' ELSE '' END AS QUALIFIED

    FROM

    TABLE1

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • But to address the syntax error, you have an open parentheses with no matching close parentheses.



    Colleen M. Morrow
    Cleveland DBA

  • Jason Selburg (4/28/2011)


    No need to CAST.

    SELECT

    NAME, LASTNAME,

    CASE WHEN AGE > 25 THEN '1' ELSE '' END AS QUALIFIED

    FROM

    TABLE1

    Just a bit more information, if you want to review/save a chart of implicit conversions, follow the link and scroll down to the middle of the displayed page.

    http://msdn.microsoft.com/en-us/library/ms187928.aspx

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply