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
 SQL Server Development (2000)
 Sql Statement

Author  Topic 

srireddyp
Starting Member

8 Posts

Posted - 2006-06-05 : 15:07:45
Hi,
I have a table called employee with enumber,ename columns
and following data

enumber ename
12321-001 Mike
12322-002 Tom
12333-003 Kevin
23232SP Matt

I need a sql statement to find the enumber
before "-" and before any alphabet. Here
12321, 12322, 12333, 23232 etc
How can I do this ?
Thanks for help
Srini



Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-06-05 : 16:33:48
Not sure if this is what you're looking for but acording to your example it will work:

SELECT LEFT(enumber, 5) AS Something, ename FROM table

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

cmdr_skywalker
Posting Yak Master

159 Posts

Posted - 2006-06-05 : 21:36:27
create table #temp(
enumber varchar(20),
ename varchar(20)
)
go

insert into #temp values('12321-001','Mike')
insert into #temp values('12322-002','Tom')
insert into #temp values('12333-003','Kevin')
insert into #temp values('23232SP','Matt')
insert into #temp values('2451','John')
GO


SELECT
CASE
WHEN CHARINDEX('-',enumber)>0 THEN SUBSTRING(enumber,1, CHARINDEX('-',enumber)-1)
WHEN PATINDEX('%[^0-9]%',enumber)>0 THEN SUBSTRING(enumber,1, PATINDEX('%[^0-9]%',enumber)-1)
ELSE
enumber
END AS ENUMBER,
ENAME
FROM #temp
GO

drop table #temp


May the Almighty God bless us all!
Go to Top of Page

cmdr_skywalker
Posting Yak Master

159 Posts

Posted - 2006-06-05 : 21:37:57
Actually, you can remove the CHARINDEX, the patindex will suffice.

May the Almighty God bless us all!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-06 : 04:22:57
Also refer this
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56713

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -