1

I have a column which contains both string and integer( Eg: 125D , S234,.. ) Now I want to separate the integer and the string. How to do this in SQL server???

1
  • is this a one time operation, or will you need to repeat this as a data cleansing operation? Commented Mar 4, 2014 at 17:20

1 Answer 1

1

This should get you what you need. If it's more than a one time thing, you could make this into a function. Or maybe you could split it a load time, if you have some sort of ETL process.

DECLARE @MyString varchar(1000) = 'A1b2C3';

declare @Counter integer = 1;
declare @currChar char(1) = '';;
declare @charString varchar(1000) = '';
declare @intString varchar(1000) = '';



while (@Counter <= len(@MyString))
begin
 set @currChar = substring(@MyString,@Counter,1)
   if isNumeric(@currChar) = 1
          set @intString = @intString + @currChar
   else
       set @charString = @charString + @currChar

  set @counter = @counter + 1
end

select @charString;
select cast(@intString as integer)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.