Trimming inconsistent values in a column (SQL)

  • I have such an issue.

    On column namely t.DC has sometimes other values after the number for example

    01 ROYAL

    40 ABC

     

    Trying to see if there is anything that can erase those names after digits leaving just the numbers there.

    `

    select t.Chain, t.DC, t.Item#

    FROM t

    `

     

    When I try to write like this " CASE WHEN t.DC IN (01, 02, 08, 21, 22, 30, 32, 40, 55, 62) THEN t.DC ELSE '01' END AS DC," I am getting an error " Conversion failed when converting the nvarchar value '21 CE' to data type int."

    • This topic was modified 2 years, 8 months ago by  JeremyU.
    • This topic was modified 2 years, 8 months ago by  JeremyU.
  • Try this:

    CASE WHEN LEFT(t.DC,2) IN ('01', '02', '08', '21', '22', '30', '32', '40', '55', '62') 
    THEN LEFT(t.DC,2)
    ELSE '01'
    END AS DC

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • select t.DC, substring(t.DC,1,charindex(' ',t.DC)) FROM t

    this will remove anything after the space

    t.DC     modifiedt.DC

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

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

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