Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 bad data to fix

Author  Topic 

freedg
Starting Member

2 Posts

Posted - 2012-05-28 : 19:32:22
I have a column "FirstName" that has bad data. It contains "Michael A." instead of "Michael". The are 1400 rows of similar data. The first name is always followed by a space but the first names are variable lengths.

How can I fix this? SQL server 2008

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-28 : 22:40:36
how do you want to fix this ? what is the rule ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-29 : 01:27:17
if you just want only part below first space you can use logic like

SELECT LEFT(Name,CHARINDEX(' ',Name + ' ')-1) AS FirstName FROM Table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

freedg
Starting Member

2 Posts

Posted - 2012-05-29 : 07:55:54
thanks Visakh16

That is exactly what I needed!!

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-29 : 12:02:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-05-29 : 12:52:14
What's this give you?

LEN(FirstName)-LEN(REPLACE(FirstName),' ','') AS FirstName_Spaces, COUNT(*) AS Spaces_Rows
FROM Table
GROUP BY LEN(FirstName)-LEN(REPLACE(FirstName),' ','')
Go to Top of Page
   

- Advertisement -