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