SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    characters before "_"

    (id) name category

    (1) Tom 12_152
    (2) Mary 3_12
    (3) Jane 52_9
    (4) Robert 7_890
    (5) Jack 862_2
    I have the above data in my table.

    The following would-be code doesn't work correctly, but it will show what I want.

    Code:
    would-be code
    
    select id, name, characters before "_" of category
    from myTable
    
    target result
    
    (1) Tom 12
    (2) Mary 3 
    (3) Jane 52 
    (4) Robert 7 
    (5) Jack 862

  2. #2
    SitePoint Evangelist luxinterior's Avatar
    Join Date
    Aug 2004
    Location
    Here, there and everywhere!
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd suggest rather than hacking away trying to make the database do the work you may want to either re-think the application that puts the data in and/or optimize your database better. This looks like a weired way of doing it to me. I'm not sure what kind of category '12_152' is but it's actually NOT a category.

    Lux

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,330
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    lux, how can you say that? of course it's a category

    joon, have you forgotten all your mysql? you have done this before
    Code:
    select id
         , name
         , left(category,position('_' in category)-1)
      from myTable
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •