SQL CASE command

Asked

Viewed 51 times

0

I’m trying to select only 5 characters from the field description, tried several ways and it doesn’t work, the eating I’m giving is this:

SELECT description = CASE WHEN LEN(description) > 5 THEN LEFT(description,5) ELSE description END AS description FROM contentphp

What’s the mistake I can’t find? (I am using MYSQL)

  • instead of LEN, tried char_lenght()?

  • I tried but he says there is no such command.

  • I don’t have mysql here p/ test.. :(

  • I don’t understand the reason of the case, do you want to select only the first 5? Try: "SELECT LEFT(Description,5) AS Description FROM contentphp"

  • Test here (on oracle) and worked: SELECT (CASE WHEN CHAR_LENGTH(Description) > 5 then LEFT('Description',5) Else 'Description ' end) AS Description FROM contentphp;

  • @Constant worked now that you posted it... the difference is that you put the command between parentheses.

  • 1

    Okay, mark the right answer.. :)

Show 2 more comments

1 answer

1


Well, I got a Mysql here and tested, it worked:

SELECT (CASE WHEN CHAR_LENGTH(description) > 5 then LEFT(description,8) else description end) AS description  FROM contentphp;

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.