flagging if a field contains a string

  • I want to do the T-SQL equivalent of

    Iif(Instr([SKU],"EC") > 0, "EC", Iif(Instr([SKU],"DD") > 0, "DD",""), "")

    i.e. for a calculated column: If the field SKU contains the string 'EC', then return, 'EC', else if the field SKU contains the string 'DD', then return 'DD' otherwise return an empty string.

    I'm trying to learn TSQL!

  • Use CASE expression

    SELECT CASE WHEN SKU LIKE '%EC%' THEN 'EC'

    WHEN SKU LIKE '%DD%' THEN 'DD'

    ELSE '' END

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • Mark Thank you -I hadn't realised I could us SELECT CASE like that. However on using, the line gets replaced as...

    'SELECT CASE WHEN [SKU] LIKE %EC% THEN EC WHEN [SKU] LIKE %DD% THEN DD ELSE END'

    EDIT:Fixed - It didn't need the SELECT word

    Many thanks!

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

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