Cast VARCHAR as INT keeping NULLs and setting text value to number

Good morning,

I'm creating an int field from a varchar field in a staging table in SQL Server 2012.  

The field contains NULL values, positive and negative integers, and the text value "Unknown".

I would like to create a new int field and update it with the numeric values, set the text value of "Unknown" to 88888 and keep the NULL values as NULL.

I "tried" TRY_CAST but I really need the text "Unknown" set to the 88888 value because in this instance "Unknown" has a different meaning than NULL.  

Any help will be greatly appreciated.

Thanks!
LVL 1
ttist25Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
If your description is precise, then you just missed the next step: Just a IIF().

IIF(yourTextColumn = 'Unknown', '88888', TRY_CAST(yourTextColumn AS INT))

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ttist25Author Commented:
Awesome!  THanks ste5an!
Ryan ChongSoftware Team LeadCommented:
or

case
     when yourTextColumn  is null then null
     when yourTextColumn  = 'Unknown' then 88888
     else cast(yourTextColumn as int)
end

Open in new window


then values of yourTextColumn only contain Unknown, Nulll or integer values in varchar.

you need to tell us if there is exceptional cases, whereas the varchar values not equal to "Unknown"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.