문제

I'm working on a simple SSIS package for myself; I'm a newbie to all of this.

I have a flat file that has MALE or FEMALE values for Gender.

I want this to be only M or F in the Gender column on SQL Server when I load it. I have the below expressions:

REPLACE ( [Gender], "Female", "F") REPLACE ( [Gender], "Male", "M")

I'm not sure how to club them together.

Any help/tips would be highly appreciated.

Thank you!

도움이 되었습니까?

해결책

Chain them:

REPLACE (REPLACE ( [Gender], "Female", "F" ), "Male", "M") 

Breaking it down, the inner REPLACE replaces all instances of "Female" with "F" but does nothing to any other values. The output of that function call ( which should either be "F" or "Male") is then passed to the outer REPLACE which transforms "Male" to "M". So, you should get "M" or "F" as appropriate.

다른 팁

Try CASE instead:

CASE
   WHEN [Gender] = 'Female' THEN 'F'
   WHEN [Gender] = 'Male'   THEN 'M'
   ELSE NULL
END

Use a derived column - If the allowable values for that column are only Male and Female then this expression would suffice- ([Gender]=="Male"?"M":"F") In case the column holds values other than Male/Female you might want to do something additional, here I am just passing the [Gender] value as is if it does not fall into Male/Female - ([Gender]=="Male"?"M":([Gender] == "Female"? "F":[Gender]))

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top