Problem:
Sometime we get the data in one column with some kind of delimiter( such as comma,pipe, space etc) and also this string contains some special characters such as (.,./,$,#,@ etc).
The current problem is related to the string that has space separator and some special characters. Our goal is to split this string into FirstName, MiddleName and Last Name.
Solutions:
--Create Test Table
Create table dbo.ColSplit ( FullName Varchar(300))
go
insert into dbo.ColSplit values ( 'CHARLES. ROSAMILIA.')
go
insert into dbo.ColSplit values ( 'CHARLES. .cc ROSAMILIA. @')
go
insert into dbo.ColSplit values ( 'CHARLES. HANS. ROSAMILIA /')
go
insert into dbo.ColSplit values ( 'CHARLES ROSAMILIA')
go
Select * from Dbo.ColSplit
--This function removes special characters from string. The code is available on below link
http://www.tainyan.com/codesnippets/entry-63/sql-function-to-remove-special-characters.html
CREATE FUNCTION [dbo].[ufn_RemoveSpecialChars]
(
@Input VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @Output VARCHAR(MAX )
IF (ISNULL(@Input,'')='')
SET @Output = @Input
ELSE
BEGIN
DECLARE @Len INT
DECLARE @Counter INT
DECLARE @CharCode INT
SET @Output = ''
SET @Len = LEN(@Input)
SET @Counter = 1
WHILE @Counter <= @Len
BEGIN
SET @CharCode = ASCII(SUBSTRING(@Input, @Counter, 1))
IF @CharCode=32 OR @CharCode BETWEEN 48 and 57 OR @CharCode BETWEEN 65 AND 90 OR @CharCode BETWEEN 97 AND 122
SET @Output = @Output + CHAR(@CharCode)
SET @Counter = @Counter + 1
END
END
RETURN @Output
END
--Final Query
--Used function to remove special characters.
--Used ParseName builtin function to split the FullName depending upon ''( space).
--Wrote Case Statement to adjust middle name to last name where there is no last name.
;WITH CTE
AS (SELECT Reverse(Parsename(Replace(
Reverse(dbo.[Ufn_ removespecialchars](FullName)) , ' ', '.'), 1)) AS [FirstName],
Reverse(Parsename(Replace( Reverse(dbo.[Ufn_ removespecialchars](FullName)) , ' ', '.'), 2)) AS [MiddleName],
Reverse(Parsename(Replace( Reverse(dbo.[Ufn_ removespecialchars](FullName)) , ' ', '.'), 3)) AS [LastName]
FROM dbo.ColSplit)
SELECT FirstName,
CASE
WHEN LastName IS NULL THEN NULL
ELSE MiddleName
END AS MiddleName,
CASE
WHEN LastName IS NULL
AND MiddleName IS NOT NULL THEN MiddleName
ELSE LastName
END AS LastName
FROM CTE
The above query can be changed according to the requirement. We can also write a user function to split string string into multiple columns. Here we were required to split FirstName,MiddleName and LastName and that can be handled with built in function.
Sometime we get the data in one column with some kind of delimiter( such as comma,pipe, space etc) and also this string contains some special characters such as (.,./,$,#,@ etc).
The current problem is related to the string that has space separator and some special characters. Our goal is to split this string into FirstName, MiddleName and Last Name.
Solutions:
--Create Test Table
Create table dbo.ColSplit ( FullName Varchar(300))
go
insert into dbo.ColSplit values ( 'CHARLES. ROSAMILIA.')
go
insert into dbo.ColSplit values ( 'CHARLES. .cc ROSAMILIA. @')
go
insert into dbo.ColSplit values ( 'CHARLES. HANS. ROSAMILIA /')
go
insert into dbo.ColSplit values ( 'CHARLES ROSAMILIA')
go
Select * from Dbo.ColSplit
--This function removes special characters from string. The code is available on below link
http://www.tainyan.com/codesnippets/entry-63/sql-function-to-remove-special-characters.html
CREATE FUNCTION [dbo].[ufn_RemoveSpecialChars]
(
@Input VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @Output VARCHAR(MAX )
IF (ISNULL(@Input,'')='')
SET @Output = @Input
ELSE
BEGIN
DECLARE @Len INT
DECLARE @Counter INT
DECLARE @CharCode INT
SET @Output = ''
SET @Len = LEN(@Input)
SET @Counter = 1
WHILE @Counter <= @Len
BEGIN
SET @CharCode = ASCII(SUBSTRING(@Input, @Counter, 1))
IF @CharCode=32 OR @CharCode BETWEEN 48 and 57 OR @CharCode BETWEEN 65 AND 90 OR @CharCode BETWEEN 97 AND 122
SET @Output = @Output + CHAR(@CharCode)
SET @Counter = @Counter + 1
END
END
RETURN @Output
END
--Final Query
--Used function to remove special characters.
--Used ParseName builtin function to split the FullName depending upon ''( space).
--Wrote Case Statement to adjust middle name to last name where there is no last name.
;WITH CTE
AS (SELECT Reverse(Parsename(Replace(
Reverse(Parsename(Replace(
Reverse(Parsename(Replace(
FROM dbo.ColSplit)
SELECT FirstName,
CASE
WHEN LastName IS NULL THEN NULL
ELSE MiddleName
END AS MiddleName,
CASE
WHEN LastName IS NULL
AND MiddleName IS NOT NULL THEN MiddleName
ELSE LastName
END AS LastName
FROM CTE
The above query can be changed according to the requirement. We can also write a user function to split string string into multiple columns. Here we were required to split FirstName,MiddleName and LastName and that can be handled with built in function.