Jump to content
Nathan

MYSQL Select Everything Before a Character

Recommended Posts

So I could do this in MSSQL, but how is this done in MYSQL?

 

I data in field "order_items" like:

 

121x1

134x4

1001x2

163x1

 

What I want to do is select everything before the "x".

Share this post


Link to post
Share on other sites

Here we go, just figured this one out:

 

select replace(left(order_items,LOCATE('x',order_items)),'x','')

You should post a tutorial explaining this whole thing ;) what it does and what it is used for. I see you are good at coding stuff :) wish I had that knowlage :)

Share this post


Link to post
Share on other sites

You should post a tutorial explaining this whole thing :) what it does and what it is used for. I see you are good at coding stuff :P wish I had that knowlage :)

 

try:

left(order_items,instr(order_item,'x')). This should be a little more efficient. Basically, left() in MSSQL takes the characters in a string from starting position on the left to an integer. INSTR determines the integer location of a character (x). So it takes all the characters on the left from beginning to first X

 

This assumes that there is only one x as well. Do you need to trim the string or anything?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×