If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > substring problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-11, 00:01
reeson reeson is offline
Registered User
 
Join Date: May 2011
Posts: 24
substring problem

hi all,

i have a string = '26453|20110411'.

how do i select first 6 character AFTER the pipe (|)?
So the output would be 201104.

Length of character before | might varies.

Any ideas?

Last edited by reeson; 07-18-11 at 00:03. Reason: more info
Reply With Quote
  #2 (permalink)  
Old 07-18-11, 00:26
BrianSteffens BrianSteffens is offline
Registered User
 
Join Date: Jul 2011
Posts: 14
There's no function to do this that I know of, but it's possible - albeit convoluted - with something like the following:

Code:
select

  replace(
    substring(
      substring_index(val, '|', 2),
      length(substring_index(val, '|', 1)) + 1
    ),
    '|', ''
  )

from sometable;
Where val is a char/varchar type in sometable.

I confirmed this worked with 26453|20110411 and 264531|20110411.
Reply With Quote
  #3 (permalink)  
Old 07-18-11, 00:47
reeson reeson is offline
Registered User
 
Join Date: May 2011
Posts: 24
Seems like it works. Thanks so much

Will look up what substring_index do, never use it before.

Cheers BrianSteffens!
Reply With Quote
  #4 (permalink)  
Old 07-18-11, 00:56
BrianSteffens BrianSteffens is offline
Registered User
 
Join Date: Jul 2011
Posts: 14
You may have already figured this out, but for your uses it can actually be a lot simpler. The code I posted can be used to get any value in a list of delimiter-separated values.. but since you only have the 2 values separated by one delimiter, the following should work as well:

substring_index(val, '|', -1)

Much shorter
Reply With Quote
  #5 (permalink)  
Old 07-18-11, 01:20
reeson reeson is offline
Registered User
 
Join Date: May 2011
Posts: 24
After going through the manual, think I got it. Pretty neat function.

I almost going to write my own function for this issue

Thanks again mate
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On