How to split a column of text into multiple columns in SQL Server
9 هزار بار بازدید -
پارسال
-
This quick guide shows you
This quick guide shows you how to split a column containing full names to two columns, one with first name and one with the last name
Knowing how to split a single column of values into multiple columns can be a real timesaver
#SQL #CHARINDEX #SUBSTRING
If you want to follow along you can use these code snippets or read our blog
www.selectdistinct.co.uk/2023/04/13/split-a-column…
CREATE TABLE [dbo].[Names_Test](
[Full_Name] [varchar](50) NOT NULL
) ON [PRIMARY];
insert into Names_Test Values ('Ivan Smith');
insert into Names_Test Values ('Kenya Villareal');
insert into Names_Test Values ('Simeon Hancock');
insert into Names_Test Values ('Mercedes Vaughan');
insert into Names_Test Values ('Frank Smith');
insert into Names_Test Values ('Lee Mooney');
select Full_Name
, Charindex(' ', Full_Name) as [Space Position]
, len(Full_Name) as [Name Length]
, left(Full_Name,(Charindex(' ', Full_Name)-1)) as [First Name]
, substring(Full_Name,(Charindex(' ', Full_Name)+1), (len(Full_Name) -Charindex(' ', Full_Name))) as [Last Name]
from Names_Test
music by www.bensound.com
پارسال
در تاریخ 1402/01/24 منتشر شده
است.
9,066
بـار بازدید شده