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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Elegant way to remove characters?

Author  Topic 

nathans
Aged Yak Warrior

938 Posts

Posted - 2004-12-30 : 15:07:30
Hey guys,
Im just inetrested how others would approach this issue.

Have a resultset with names, each ending in an integer.

Nathan Skerl 1
Nathan Skerl 2
John Smith 1
...

I want to select the full name, and remove the trailing integer.

Currently:

SELECT REPLACE(REPLACE(partner_name, '1', ''), '2', '')


Feels dirty though... :)

Any thoughts?
Assume the format is [FName][Space][LName][Space][Integer]

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-12-30 : 15:15:26
Lots of ways to do this - here is one:
declare @name varchar(40)
set @name = 'Nathan Skerl 1'
select left(@name,len(@name)-charindex(' ',reverse(@name),1))
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2004-12-30 : 15:19:00
Thats what im talking about... I love this stuff. Well done ehorn!
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-12-30 : 15:31:02
you could also do:

select left(@name, patindex('%[0-9]%', @name)-2)

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -