5

In SSIS package I have derived column in which I want to format phone like below:

CASE
    WHEN TRY_CONVERT(BIGINT, phone) IS NULL THEN
        NULL
    ELSE
        phone
END

How can I use the SSIS expression to achieve same result as above?

Hadi
  • 36,233
  • 13
  • 65
  • 124
Jay Desai
  • 821
  • 3
  • 15
  • 42

1 Answers1

5

Derived Column

You have to use the following expression:

(DT_I8)[Phone] == (DT_I8)[Phone] ? [Phone] : NULL(DT_WSTR,50)

Note that you have to replace (DT_WSTR,50) with the data type of column [Phone]. Click here for more information

And in the derived column error output change the on error option to Ignore Failure

enter image description here

Script Component

You can also achieve this using a script component:

  1. Add a script component to the data flow task
  2. Select [Phone] as Input Column
  3. Create a new Output Column of the same type of [Phone] example [outPhone]
  4. Use a similar code

    if(!Row.Phone_IsNull && !String.IsNullOrEmpty(Row.Phone) && Int64.TryParse(Row.Phone, out long number)){
    
        Row.OutPhone = Row.Phone;
    
    }else{
    
        Row.OutPhone_IsNull = true;
    
    }
    
Hadi
  • 36,233
  • 13
  • 65
  • 124
  • It's making NULL for all the values of Phone. – Jay Desai Apr 02 '19 at 20:49
  • What is the type of the source column? – Hadi Apr 02 '19 at 20:50
  • Check that [Phone] doesn't contains unwanted characters or additional spaces – Hadi Apr 02 '19 at 20:52
  • @JayDesai the try convert logic is 100% correct you must check the data – Hadi Apr 02 '19 at 20:53
  • Phone column does contain some values with unwanted characters or additional spaces. And that's the reason I want to make it NULL. If it is convertible to BIGINT it should pass it as it is. Source column Phone is coming from csv file and Destination column has BIGINT data type. – Jay Desai Apr 02 '19 at 21:17
  • TRY_CONVERT function is working correctly I need the same functionality in SSIS. – Jay Desai Apr 02 '19 at 21:24
  • @JayDesai what is the column data type in SSIS? Check it from the flat file connection manager – Hadi Apr 02 '19 at 21:26
  • @JayDesai does numbers contains a seperator in csv? – Hadi Apr 02 '19 at 21:29
  • @JayDesai i updated my answer and provided a new method check it out – Hadi Apr 02 '19 at 21:39
  • @JayDesai also it is better to provide sample data and to add some screenshot of the flat file connection manager – Hadi Apr 02 '19 at 21:40