SQL case substring problem. please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ciqeane
    New Member
    • Aug 2010
    • 6

    SQL case substring problem. please help

    here is the sample data

    Code:
    CREATE TABLE Boo (
    keycol INT PRIMARY KEY,
    datacol VARCHAR(20));
    
    INSERT INTO Boo VALUES(1, 'DM/B19S/7281520');
    INSERT INTO Boo VALUES(2, 'NW/L11U/BALL 10/300');
    INSERT INTO Boo VALUES(3, 'RK/H11S/65-2511RK');
    INSERT INTO Boo VALUES(4, 'FI/G21U/0004/BN5-12');
    INSERT INTO Boo VALUES(5, 'FN/199U/FABRICATE');
    Here is my query, please help me figure out what is the error

    Code:
    SELECT keycol,
    CASE
    WHEN SUBSTRING(datacol, 1, 2) IN ('FI','FN') THEN REG
    WHEN SUBSTRING(datacol, 1, 2) IN ('FV') THEN VALV
    WHEN SUBSTRING(datacol, 1, 2) IN ('RK') THEN RIKE
    ELSE NULL
    END AS col2
    FROM Boo;
    this error appear when i execute.

    Code:
    Msg 207, Level 16, State 1, Line 6
    Invalid column name 'REG'.
    Msg 207, Level 16, State 1, Line 7
    Invalid column name 'VALV'.
    Msg 207, Level 16, State 1, Line 8
    Invalid column name 'RIKE'.
    whats that mean?
    helpp. thanks in advance:-)
    Last edited by NeoPa; Aug 19 '10, 01:05 PM. Reason: Please use the [CODE] tags provided
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    the query is treating REG, VALV, AND RIKE AS Column names in the table. Use single quotes.

    Thanks

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32653

      #3
      It looks like you are intending to set the values as strings. As it is, there is nothing there to indicate what REG, VALV and RIKE are. Try instead :
      Code:
      SELECT keycol,
      CASE
      WHEN SUBSTRING(datacol, 1, 2) IN ('FI','FN') THEN 'REG'
      WHEN SUBSTRING(datacol, 1, 2) IN ('FV') THEN 'VALV'
      WHEN SUBSTRING(datacol, 1, 2) IN ('RK') THEN 'RIKE'
      ELSE NULL
      END AS col2
      FROM Boo;

      Comment

      • ciqeane
        New Member
        • Aug 2010
        • 6

        #4
        oh my God, it works when i use the single quote!!
        thanks a bunch all.:-)

        Comment

        Working...