Using Left or Contains

  • I am getting a value from a select statement. What I want to really grab from that value is everything to the left of a period(.). If the period is not there then I want to grab whatever value is there. So if the field value is (Testing.Test) I want to grab just Testing. If the field value is (ABCDETest Test2) then I want to grab ABCDETest Test2. I'm new to the SQL language so any help would be appreciated.

  • You might try the CASE statement and the CHARINDEX function like so:

    declare @X varchar(100)

    declare @Y varchar(100)

    set @X = 'Testing.Test'

    set @Y = 'ABCDETest Test2'

    select case when charindex('.',@X) > 0

    then substring(@X,1,charindex('.',@X) -1)

    else @X end,

    case when charindex('.',@Y) > 0

    then substring(@Y,1,charindex('.',@Y) -1)

    else @Y end

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Thanks Greg, Got it working perfectly. Thanks for your help.

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

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