SQL and SSIS : Extract part of file name in SQL / SSIS.

kkran 831 Reputation points
2023-07-05T21:29:27.37+00:00

Hi All - I have five SSIS packages and each of these packages source is a csv file. Each packages access the shared folder path respectively and loads the data into the SQL database table. Below are the file naming conventions of the files :

  1. CoreJune2023 - Formatted for 24 month upload_2023-06-01
  2. UpgradeJune_20230701_2023-06-01
  3. July NonCore_0703 23_2023-06-01
  4. July DP_0703 23_2023-06-01
  5. DPlanJuly_20230630_2023-06-01
  6. PlanJune_20230601_2023-06-01

Now the requirement is i need to grab the '2023-06-01' from the file name and store it into data table. sample below.

Could you please help me how to do it in SSIS package at variable level and also in SQL as well using functions ?

In SSIS, I am already the filename in the variable, may be i need to create another variable writing REVERSE ?

And if it is in SQL, i should write a SUBSTRING and REVERSE ?

User's image

When the SQL job runs calling the SSIS packages the file names are stored in the table along with the source path and archive path. Below is the information related to files stored in the separate table.

 ,[file_name]
      ,[file_path]
      ,[archive_file_path]
      ,[load_datetime]
      ,[load_status]
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. ZoeHui-MSFT 41,491 Reputation points
    2023-07-10T01:50:58.2466667+00:00

    Hi @Hi @kkran,

    Use token expression to do that.

    TOKEN([yourcolumn], ".", 1)
    

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.


4 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2023-07-06T13:03:57.4166667+00:00

    Hi @kkran,

    We will use tokenization technique for both cases.

    We need to retrieve last token from a string.

    Here is how to do it via SSIS Expression:

    TOKEN(@[User::fileName], "_", TOKENCOUNT(@[User::fileName], "_"))
    
    

    Here is how to do it via T-SQL:

    -- DDL and sample data population, start
    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, FileName VARCHAR(100));
    INSERT INTO @tbl VALUES
    ('CoreJune2023 - Formatted for 24 month upload_2023-06-01'),
    ('UpgradeJune_20230701_2023-06-01'),
    ('July NonCore_0703 23_2023-06-01'),
    ('July DP_0703 23_2023-06-01'),
    ('DPlanJuly_20230630_2023-06-01'),
    ('PlanJune_20230601_2023-06-01');
    -- DDL and sample data population, end
    SELECT * 
    , PARSENAME(REPLACE(FileName,'_','.'), 1)
    FROM @tbl;
    
    1 person found this answer helpful.
    0 comments No comments

  2. ZoeHui-MSFT 41,491 Reputation points
    2023-07-06T02:49:21.7433333+00:00

    Hi @kkran,

    In SSIS, you may use below expression.

    RIGHT(@[User::Variable],FINDSTRING(REVERSE(@[User::Variable]),"_",1) - 1)
    

    In SQL, you may use the code like below.

    DECLARE @tbl TABLE(FileName VARCHAR(100))
    INSERT INTO @tbl VALUES('CoreJune2023 - Formatted for 24 month upload_2023-06-01'),('UpgradeJune_20230701_2023-06-01')
    
    SELECT REVERSE(LEFT(REVERSE(FileName),10))FROM @tbl 
    
    
    

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.


  3. ZoeHui-MSFT 41,491 Reputation points
    2023-07-07T01:58:04.2466667+00:00

    Hi @kkran,

    Check the settings like below. You may need to set the parameter to match your variable.

    User's image

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.


  4. kkran 831 Reputation points
    2023-07-07T16:03:31.9966667+00:00

    Thanks @ZoeHui-MSFT . One last thing, the value is being captured like this in the table. how do i remove the .csv

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.