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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...