Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql function help

Status
Not open for further replies.

sarakmo

IS-IT--Management
Sep 4, 2007
4
US
Hello All:

I'm trying to select all the characters that appear before a hyphen. For example, I'd like to retrieve 'Sports' from a field with the value 'Sports - rating'

Any help!?

Thanks
 
A starting point:
SUBSTRING(columnName FROM 1 FOR POSITION('-' IN columnName)-1)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks you..what is 1? It creates an error:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'FROM'.

Your help is greatly appreciated
 
Which RDBMS ?
My guess is MS SQL Server
I suggest you post in a more specific forum.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
>> is that bad?

Depends on who you ask. [smile]

Try this...

Code:
Select Left(ColumnName, CharIndex('-', ColumnName)-1)

-George

"the screen with the little boxes in the window." - Moron
 
When you ask additional questions in the future, you will likely get an acceptable answer faster if you post your question in the correct forum. The correct forum for this question is:

forum183



-George

"the screen with the little boxes in the window." - Moron
 
Depends on who you ask
I'd say Depends on WHERE you ask
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top