0

Possible Duplicate:
Split string in SQL

I have a column called LastName in the data base that has the following value:

Johnson  (Ext. 12345)

I only want to return the Last name without the extension.

Any idea what command to use in T-SQL? (for SSRS 2008 version)

thanks

Community
  • 1
  • 1
user1860212
  • 555
  • 3
  • 7
  • 8

3 Answers3

2

give this a try,

SELECT RTRIM(SUBSTRING(LastName, 1, CHARINDEX('(', LAstName) - 1)) LastName
FROM table1
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

You can;

left(fld, charindex(' ', fld + ' ', 1) - 1)
Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

First allow me to say that such composite values in a single field are against normalization, present various problems regarding the integrity, duplication etc of data and also present problems on simple queries as the one you are trying to perform.

If you can you should split Last name and Ext in two different columns.

For answers to you question check this

Community
  • 1
  • 1
B11
  • 223
  • 3
  • 12
  • Yes but naming conventions vary across cultures. Some people have four, five six or more names: how many columns is enough? – APC Jan 13 '13 at 05:12
  • well in the case that you do not know the number of columns to associate, you can take a different approach, you can keep such values as rows of a different table, and relate to them via a foreign key, where one row on the main table can be associated to many rows on the secondary table. But this is just one approach, depending on the domain there can be various solutions. – B11 Jan 13 '13 at 20:04