Select Distinct
Select Distinct
  • Home
  • Services
  • About
  • Tips and Timesavers
  • Resources
  • Careers
  • Contact Us
  • Blog
  • Management Info Survey
  • More
    • Home
    • Services
    • About
    • Tips and Timesavers
    • Resources
    • Careers
    • Contact Us
    • Blog
    • Management Info Survey
  • Home
  • Services
  • About
  • Tips and Timesavers
  • Resources
  • Careers
  • Contact Us
  • Blog
  • Management Info Survey

Split a Column in SQL

More SQL Tips

How to split a column in SQL Server Management Studio

How to split a column in SQL

Have you ever needed to be able to split a column in SQL?


This simple example walks you through the steps involved using a column with a First and Last name and shows you how to split these into two separate columns


Here is the final SQL code


select name 

, Charindex(' ', name) as [Space Position] , len(name) as [Name Length] 

, left(name,(Charindex(' ', name)-1)) as [First Name] 

, substring(name,(Charindex(' ', name)+1), (len(name) -Charindex(' ', name))) as [Last Name] 

from [dbo].[names] 


and here is the SQL to add a sample table of names


create table names (Name varchar(100)) 

insert names select 'Ivan Yu' 

insert names select 'Kenya Villarreal' 

insert names select 'Simeon Hancock' 

insert names select 'Mercedes Vaughan' 

insert names select 'Frank Smith' 

insert names select 'Lee Mooney'



Subscribe

Sign up to hear more tips

Copyright © 2022 Select Distinct Limited - All Rights Reserved.

  • Privacy Policy

This website uses cookies.

We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.

DeclineAccept