• Visitors can check out the Forum FAQ by clicking this link. You 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. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "SQL to restrict rows by first letter / number"

Collapse

  • DimPrawn
    replied
    Originally posted by EvilWeevil View Post
    Have you considered...

    SELECT band_name
    FROM bands
    WHERE band_name like (replace(@p,'#','[0-9]')+'%')
    No. A good concise answer.

    Leave a comment:


  • PRC1964
    replied
    Originally posted by NickFitz View Post
    This is why one should use metasynctactic variables:
    "Say you want to select all the rows in table "foo" having a value in the varchar field "bar" beginning with a specific character from a particular range of characters, or beginning with any character not in that range if some wildcard character is specified..."

    That way people don't get distracted talking about nonsense like "How many bands are there in the world" and suchlike - they remain focused on solving the specific problem for the abstract case.
    I tend to create tables called "Select" with fields called "From" and "Order By".

    It confuses the hell out of everyone else though.

    Leave a comment:


  • EvilWeevil
    replied
    Have you considered...

    SELECT band_name
    FROM bands
    WHERE band_name like (replace(@p,'#','[0-9]')+'%')

    Leave a comment:


  • NickFitz
    replied
    Originally posted by DimPrawn View Post
    Actually it's not bands. I just used that as an example.
    This is why one should use metasynctactic variables:
    "Say you want to select all the rows in table "foo" having a value in the varchar field "bar" beginning with a specific character from a particular range of characters, or beginning with any character not in that range if some wildcard character is specified..."

    That way people don't get distracted talking about nonsense like "How many bands are there in the world" and suchlike - they remain focused on solving the specific problem for the abstract case.

    Leave a comment:


  • DimPrawn
    replied
    Actually it's not bands. I just used that as an example.

    Leave a comment:


  • lilelvis2000
    replied
    How many bands are you tracking? 100K? 1M? wow!

    Besides you could just make a function index in oracle, or calculated column in SQL server and index it.

    Leave a comment:


  • Moscow Mule
    replied
    Originally posted by DimPrawn View Post
    That's in module 2, to be learned next week.
    You are Bob Shawadiwadi AICMFP

    Originally posted by DimPrawn View Post
    Actually, I was thinking that the SQL using the OR and LIKE and SUBSTR is not going to be very efficient and is going to scan the data.

    I am thinking of maintaining another column (via a trigger) to contain the first letter of the band name and if it is not a letter, store # in the field, so the SQL becomes:

    SELECT band_name
    FROM bands
    WHERE band_name_begins = @p

    And put an index on this column, as the data is rarely updated or added to, but read A LOT.
    Steady now, you'll start to look like a professional (and I don't mean Bodie or Doyle).

    Leave a comment:


  • dang65
    replied
    Apart from 3 Mustaphas 3.

    Leave a comment:


  • dang65
    replied
    There are no good bands which start with a number anyway. I'd just dump them all if I was you.

    Leave a comment:


  • DimPrawn
    replied
    Actually, I was thinking that the SQL using the OR and LIKE and SUBSTR is not going to be very efficient and is going to scan the data.

    I am thinking of maintaining another column (via a trigger) to contain the first letter of the band name and if it is not a letter, store # in the field, so the SQL becomes:

    SELECT band_name
    FROM bands
    WHERE band_name_begins = @p

    And put an index on this column, as the data is rarely updated or added to, but read A LOT.

    Leave a comment:


  • DimPrawn
    replied
    Originally posted by Moscow Mule View Post
    But you can't do an OR?
    That's in module 2, to be learned next week.

    Leave a comment:


  • Moscow Mule
    replied
    Originally posted by DimPrawn View Post
    I know SQL very well

    Originally posted by DimPrawn View Post
    Now that's what I'm talking about!

    But you can't do an OR?

    Leave a comment:


  • DimPrawn
    replied
    Originally posted by rsingh View Post
    Assuming that you pass in @P and have validated it, then something like the following should work.

    Code:
    SELECT band_name
    FROM bands
    WHERE 
    (@P = '#' AND LEFT(band_name,1) NOT LIKE [a-z])
    OR 
    (@P != '#'  AND LEFT(band_name,1) = @P)
    Now that's what I'm talking about!

    Leave a comment:


  • rsingh
    replied
    Assuming that you pass in @P and have validated it, then something like the following should work.

    Code:
    SELECT band_name
    FROM bands
    WHERE 
    (@P = '#' AND LEFT(band_name,1) NOT LIKE [a-z])
    OR 
    (@P != '#'  AND LEFT(band_name,1) = @P)

    Leave a comment:


  • DimPrawn
    replied
    Originally posted by expat View Post
    SELECT band_name
    FROM bands
    WHERE UPPER(substr(band_name,1,1)) < 'A'
    OR UPPER(substr(band_name,1,1)) > 'Z'

    This uses Oracle's UPPER(string) to convert to upper case, your flavour of SQL will doubtless have something like it (I presume that you want a case-independent test).
    Great but you haven't read the requirement have you?

    There is one parameter, either the letter I want or everything that does not start with a letter (# passed in) in one simple, efficient, fast SQL statement.

    I know SQL very well and it's pretty obvious how to select everything that's not A to Z.

    Leave a comment:

Working...
X