Extract middle part of the string in SSIS

  • Hi,

    I need to extract a part of the string which is the date. For. e.g. -  to extract '310818' from the string 'RTBHY_BH_DK_Monthly_Physical_SalesDia_OrdRen_010818_310818.xlsx
    the logic is to read the full length of the string and then go backwards to extract the 6 characters before '.xlsx'
    I have tried doing this through FindString but its not working as desired.
    Could somebody know how to achieve this in SSIS ? Thanks.

  • pwalter83 - Tuesday, October 9, 2018 5:08 AM

    Hi,

    I need to extract a part of the string which is the date. For. e.g. -  to extract '310818' from the string 'RTBHY_BH_DK_Monthly_Physical_SalesDia_OrdRen_010818_310818.xlsx
    the logic is to read the full length of the string and then go backwards to extract the 6 characters before '.xlsx'
    I have tried doing this through FindString but its not working as desired.
    Could somebody know how to achieve this in SSIS ? Thanks.

    Try this:
    replace(lower(TOKEN( @[User::TestString] , "_", TOKENCOUNT( @[User::TestString] , "_")) ),".xlsx","")

    replacing @[User::TestString] with your variable/column name.

    It returns the part of the string after the final underscore and then removes .xlsx from that.

    It uses the LOWER() function to avoid potential issues with capitals, because REPLACE is case-sensitive in SSIS.

    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.

  • Phil Parkin - Tuesday, October 9, 2018 5:23 AM

    pwalter83 - Tuesday, October 9, 2018 5:08 AM

    Hi,

    I need to extract a part of the string which is the date. For. e.g. -  to extract '310818' from the string 'RTBHY_BH_DK_Monthly_Physical_SalesDia_OrdRen_010818_310818.xlsx
    the logic is to read the full length of the string and then go backwards to extract the 6 characters before '.xlsx'
    I have tried doing this through FindString but its not working as desired.
    Could somebody know how to achieve this in SSIS ? Thanks.

    Try this:
    replace(lower(TOKEN( @[User::TestString] , "_", TOKENCOUNT( @[User::TestString] , "_")) ),".xlsx","")

    replacing @[User::TestString] with your variable/column name.

    It returns the part of the string after the final underscore and then removes .xlsx from that.

    It uses the LOWER() function to avoid potential issues with capitals, because REPLACE is case-sensitive in SSIS.

    Hi Phil, Thanks a lot, your code worked perfectly ! 

    Do you know if its possible to convert the extracted string - 310818 to a date format within the same logic to read as 2018-08-31 ?
    thanks again.

  • pwalter83 - Tuesday, October 9, 2018 5:35 AM

    Phil Parkin - Tuesday, October 9, 2018 5:23 AM

    pwalter83 - Tuesday, October 9, 2018 5:08 AM

    Hi,

    I need to extract a part of the string which is the date. For. e.g. -  to extract '310818' from the string 'RTBHY_BH_DK_Monthly_Physical_SalesDia_OrdRen_010818_310818.xlsx
    the logic is to read the full length of the string and then go backwards to extract the 6 characters before '.xlsx'
    I have tried doing this through FindString but its not working as desired.
    Could somebody know how to achieve this in SSIS ? Thanks.

    Try this:
    replace(lower(TOKEN( @[User::TestString] , "_", TOKENCOUNT( @[User::TestString] , "_")) ),".xlsx","")

    replacing @[User::TestString] with your variable/column name.

    It returns the part of the string after the final underscore and then removes .xlsx from that.

    It uses the LOWER() function to avoid potential issues with capitals, because REPLACE is case-sensitive in SSIS.

    Hi Phil, Thanks a lot, your code worked perfectly ! 

    Do you know if its possible to convert the extracted string - 310818 to a date format within the same logic to read as 2018-08-31 ?
    thanks again.

    Of course it is, though it's not pretty.

    In the following, if you replace @[User::DateString] with your entire expression from the previous step you should get to the answer you require in one hit.

    "20"+right( @[User::DateString] ,2 ) + "-" + SUBSTRING( @[User::DateString] , 3,2 )+"-" + left( @[User::DateString],2)

    If you are using variables to do this, there's no shame in using an intermediate variable to break apart the logic. Makes it easier to maintain.

    --Edit: those smileys crack me up!

    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.

  • Phil Parkin - Tuesday, October 9, 2018 5:46 AM

    pwalter83 - Tuesday, October 9, 2018 5:35 AM

    Phil Parkin - Tuesday, October 9, 2018 5:23 AM

    pwalter83 - Tuesday, October 9, 2018 5:08 AM

    Hi,

    I need to extract a part of the string which is the date. For. e.g. -  to extract '310818' from the string 'RTBHY_BH_DK_Monthly_Physical_SalesDia_OrdRen_010818_310818.xlsx
    the logic is to read the full length of the string and then go backwards to extract the 6 characters before '.xlsx'
    I have tried doing this through FindString but its not working as desired.
    Could somebody know how to achieve this in SSIS ? Thanks.

    Try this:
    replace(lower(TOKEN( @[User::TestString] , "_", TOKENCOUNT( @[User::TestString] , "_")) ),".xlsx","")

    replacing @[User::TestString] with your variable/column name.

    It returns the part of the string after the final underscore and then removes .xlsx from that.

    It uses the LOWER() function to avoid potential issues with capitals, because REPLACE is case-sensitive in SSIS.

    Hi Phil, Thanks a lot, your code worked perfectly ! 

    Do you know if its possible to convert the extracted string - 310818 to a date format within the same logic to read as 2018-08-31 ?
    thanks again.

    Of course it is, though it's not pretty.

    In the following, if you replace @[User::DateString] with your entire expression from the previous step you should get to the answer you require in one hit.

    "20"+right( @[User::DateString] ,2 ) + "-" + SUBSTRING( @[User::DateString] , 3,2 )+"-" + left( @[User::DateString],2)

    If you are using variables to do this, there's no shame in using an intermediate variable to break apart the logic. Makes it easier to maintain.

    --Edit: those smileys crack me up!

    Thanks a ton again !!! It worked perfectly.

    Loved the smileys as well 🙂 !!!

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

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