Find characters before and after _

  • I have data like ABC_Tablename_12345, i just want everything before and after _ also if there is no underscore in the name just return the name. Can somone help me with this please. Thanks

  • You can use CHARINDEX and SUBSTRING to achieve this.

    Give it a try and post it. If you still have problems we can give you a hand.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • SELECT LEFT(column_name, CHARINDEX('_', column_name + '_') - 1) AS before_underscore,

    SUBSTRING(column_name, CHARINDEX('_', column_name + '_') + 1, 2000) AS after_underscore

    FROM table_name

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

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

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