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 2008 Forums
 Transact-SQL (2008)
 how to get the value before hyphen from left side

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2014-04-15 : 16:50:26
I have address table glfac column.

select glfac from address;

want to get all values before hyphen from left.

900-SAN DIEGO CLINIC
1-BAY CARE Hospital

want to get

900
1

Thanks a lot for the helpful info.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-04-15 : 16:55:05
See this example:
DECLARE @x VARCHAR(32) = '900-SAN DIEGO CLINIC';
SELECT LEFT(@x,CHARINDEX('-',@x+'-')-1);
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-04-17 : 02:29:40
[code]
DECLARE @Temp VARCHAR(MAX)= '1000-SAN DIEGO CLINIC'

Method 1 : SELECT LEFT(@Temp,CHARINDEX('-',@Temp)-1)

Method 2 : SELECT SUBSTRING(@Temp,0,CHARINDEX('-',@Temp))
[/code]

---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page
   

- Advertisement -