• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL return substrings

 
Grazia Lassner
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to
select the first substring before a given character with SQL ?
For instance, if my column ID contains data like ABC_XYZ or BC_Z, how could I select the 'ABC' or 'BC' occurring before the '_' ?

Thanks !
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that your ABC_XYZ is a String, and you're looking for a Java solution ...

 
Grazia Lassner
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant a way with a SQL select statement
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I think that greatly depends on the database that you're using. So this probably isn't the right forum for your question. Try finding the forum for the database you're using.
 
Grazia Lassner
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to find an Oracle forum here, but I guess it does not exist
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try instr function
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On MS SQL Server:

On PostgreSQL:


String functions seem to differ largely between vendors...
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link to Oracle forum, try in the 'SQL and PL/SQL' section
 
Grazia Lassner
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
select distinct Substr(column,1,instr(column,'_')) from ....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic