Originally posted by EvilWeevil
View Post
- 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!
Collapse
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.
Logging in...
Previously on "SQL to restrict rows by first letter / number"
Collapse
-
I tend to create tables called "Select" with fields called "From" and "Order By".Originally posted by NickFitz View PostThis 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.
It confuses the hell out of everyone else though.
Leave a comment:
-
Have you considered...
SELECT band_name
FROM bands
WHERE band_name like (replace(@p,'#','[0-9]')+'%')
Leave a comment:
-
This is why one should use metasynctactic variables:Originally posted by DimPrawn View PostActually it's not bands. I just used that as an example.
"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:
-
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:
-
You are Bob Shawadiwadi AICMFPOriginally posted by DimPrawn View PostThat's in module 2, to be learned next week.
Steady now, you'll start to look like a professional (and I don't mean Bodie or Doyle).Originally posted by DimPrawn View PostActually, 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:
-
There are no good bands which start with a number anyway. I'd just dump them all if I was you.
Leave a comment:
-
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:
-
-
Now that's what I'm talking about!Originally posted by rsingh View PostAssuming 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:
-
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:
-
Great but you haven't read the requirement have you?Originally posted by expat View PostSELECT 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).
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:
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers

Leave a comment: