Question

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!

Était-ce utile?

La solution

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.

Autres conseils

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]))

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top