Parse out the first 3 characters

  • Hi

    how do I do I pull out the first three chars of a column using a derived column. I need to pull out first 3 chars and use them at a later time in the code

  • You use the LEFT function.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • ok thanks

    and if the string is not all the same length I will probably hit problems ?

  • ronan.healy (8/11/2014)


    ok thanks

    and if the string is not all the same length I will probably hit problems ?

    As long as the string is three or more characters, you should be fine.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I used this

    Left(TaxLotDescription,3)

    and its not working. I then looked into my string function and left is not there right is. why is that

  • Phil Parkin (8/11/2014)


    ronan.healy (8/11/2014)


    ok thanks

    and if the string is not all the same length I will probably hit problems ?

    As long as the string is three or more characters, you should be fine.

    Even if the string is shorter than three characters, it's not an issue. LEFT will just return the original string.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • ronan.healy (8/11/2014)


    I used this

    Left(TaxLotDescription,3)

    and its not working. I then looked into my string function and left is not there right is. why is that

    Ah, LEFT is probably introduced in a later version.

    In that case, use SUBSTRING instead.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • in ssis 2005 you need to use SUBSTRING(ColumnName,1,3)

    ----------------------------------------------------------------------------------------------------------------------------------------------------
    Roshan Joe

    Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • thanks that worked

  • Use below example :

    DECLARE @i TABLE( mychar VARCHAR(50)

    ,deriv as SUBSTRING(mychar,1,3) --Use this

    ,newderiv as left(mychar,3)); --Or use this

    INSERT @i VALUES ('Steve'), ('Stephan'), ('Stephanie')

    , ('Sterling'), ('Steppenwolf'),('Ab')

    SELECT * FROM @i

    Vimal LohaniSQL DBA | MCP (70-461,70-462)==============================The greatest barrier to success is the fear of failure ** Success is a journey not a destination**Think before you print, SAVE TREES, Protect Mother Nature

  • Koen Verbeeck (8/11/2014)


    Phil Parkin (8/11/2014)


    ronan.healy (8/11/2014)


    ok thanks

    and if the string is not all the same length I will probably hit problems ?

    As long as the string is three or more characters, you should be fine.

    Even if the string is shorter than three characters, it's not an issue. LEFT will just return the original string.

    Thanks for the correction!

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I didn't find any error with the code :

    DECLARE @i TABLE( mychar VARCHAR(50)

    ,deriv as SUBSTRING(mychar,1,3) --Use this

    ,newderiv as left(mychar,3));--Or use this

    INSERT @i VALUES ('Steve'), ('Stephan'), ('Stephanie')

    , ('Sterling'), ('Steppenwolf'),('Ab'),('Abc')

    SELECT * FROM @i

    --RESULT :

    (7 row(s) affected)

    mychar deriv newderiv

    -------------------------------------------------- ----- --------

    Steve Ste Ste

    Stephan Ste Ste

    Stephanie Ste Ste

    Sterling Ste Ste

    Steppenwolf Ste Ste

    Ab Ab Ab

    Abc Abc Abc

    (7 row(s) affected)

    Vimal LohaniSQL DBA | MCP (70-461,70-462)==============================The greatest barrier to success is the fear of failure ** Success is a journey not a destination**Think before you print, SAVE TREES, Protect Mother Nature

  • Vimal Lohani (8/17/2014)


    I didn't find any error with the code :

    DECLARE @i TABLE( mychar VARCHAR(50)

    ,deriv as SUBSTRING(mychar,1,3) --Use this

    ,newderiv as left(mychar,3));--Or use this

    INSERT @i VALUES ('Steve'), ('Stephan'), ('Stephanie')

    , ('Sterling'), ('Steppenwolf'),('Ab'),('Abc')

    SELECT * FROM @i

    --RESULT :

    (7 row(s) affected)

    mychar deriv newderiv

    -------------------------------------------------- ----- --------

    Steve Ste Ste

    Stephan Ste Ste

    Stephanie Ste Ste

    Sterling Ste Ste

    Steppenwolf Ste Ste

    Ab Ab Ab

    Abc Abc Abc

    (7 row(s) affected)

    You're running the above script in management studio. The question deals with the derived column SSIS transformation (used inside a data flow task). I also dont have the LEFT function here in SSIS 2008 either. So it must have come later.

    ----------------------------------------------------

  • MMartin1 (10/31/2014)


    Vimal Lohani (8/17/2014)


    I didn't find any error with the code :

    DECLARE @i TABLE( mychar VARCHAR(50)

    ,deriv as SUBSTRING(mychar,1,3) --Use this

    ,newderiv as left(mychar,3));--Or use this

    INSERT @i VALUES ('Steve'), ('Stephan'), ('Stephanie')

    , ('Sterling'), ('Steppenwolf'),('Ab'),('Abc')

    SELECT * FROM @i

    --RESULT :

    (7 row(s) affected)

    mychar deriv newderiv

    -------------------------------------------------- ----- --------

    Steve Ste Ste

    Stephan Ste Ste

    Stephanie Ste Ste

    Sterling Ste Ste

    Steppenwolf Ste Ste

    Ab Ab Ab

    Abc Abc Abc

    (7 row(s) affected)

    You're running the above script in management studio. The question deals with the derived column SSIS transformation (used inside a data flow task). I also dont have the LEFT function here in SSIS 2008 either. So it must have come later.

    Please use substring.

    Vimal LohaniSQL DBA | MCP (70-461,70-462)==============================The greatest barrier to success is the fear of failure ** Success is a journey not a destination**Think before you print, SAVE TREES, Protect Mother Nature

  • MMartin1 (10/31/2014)


    Vimal Lohani (8/17/2014)


    I didn't find any error with the code :

    DECLARE @i TABLE( mychar VARCHAR(50)

    ,deriv as SUBSTRING(mychar,1,3) --Use this

    ,newderiv as left(mychar,3));--Or use this

    INSERT @i VALUES ('Steve'), ('Stephan'), ('Stephanie')

    , ('Sterling'), ('Steppenwolf'),('Ab'),('Abc')

    SELECT * FROM @i

    --RESULT :

    (7 row(s) affected)

    mychar deriv newderiv

    -------------------------------------------------- ----- --------

    Steve Ste Ste

    Stephan Ste Ste

    Stephanie Ste Ste

    Sterling Ste Ste

    Steppenwolf Ste Ste

    Ab Ab Ab

    Abc Abc Abc

    (7 row(s) affected)

    You're running the above script in management studio. The question deals with the derived column SSIS transformation (used inside a data flow task). I also dont have the LEFT function here in SSIS 2008 either. So it must have come later.

    The LEFT string function is first found in SSIS 2012.

Viewing 15 posts - 1 through 15 (of 15 total)

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