SQL question

Status
Not open for further replies.
I'm writing a collection of SQL queries on SQL Server 2000 as a part of a web application, and I need to enforce case-sensitivity in the "where" clause.

The query is essentially a SELECT query, and I need to enforce the case sensitivity for the username and password the user enters.

code:<HR>
Code:
SELECT UserID
FROM User
WHERE Username = 'blah'
AND Password = 'blah2'

I've never had to do this on the SQL Server, so I'm unsure how to do this. If anyone could help me out I would much appreciate it!
 

HL7

Ars Praefectus
3,523
Subscriptor++
Converting it to binary should work:

code:<HR>
Code:
SELECT UserID
FROM User
WHERE CONVERT(binary,Username) = CONVERT(binary,'blah')
AND CONVERT(binary,Password) = CONVERT(binary,'blah2')

This works fine in SQL Server 7.0 (I'm not familiar with 2000) Without conversion, you'd have to change the sort order for the entire server.
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by reboot:
Huh ? Data in SQL Server isn't case sensitive ?

Well, it's obviously stored in its original case. But the default sort order doesn't consider case when sorting or comparing (this can be changed). In most database applications, you want case insensitivity, just like a dictionary -- you don't want 'Apples' and 'apples' to sort differently.
 

reboot

Ars Legatus Legionis
10,818
In most database applications, you want case insensitivity, just like a dictionary -- you don't want 'Apples' and 'apples' to sort differently.

It makes sense for sorts, but comparisons ???

When you want that you uppercase both sides of the equation.

It seems silly to have case insensitivity on because that means that if data is stored with original case, then the server is always uppercasing everything which is major overhead.

It could be useful if statistically you need case insensitivity more often than not, but I doubt that's a majority of cases.

Anyways , I don't know the particulars of SQL Sever and was just surprised that this would be on by default.

Oh well , I guess I'm more a Unix kinda of guy ;) :D
 

TheJet

Ars Praefectus
4,598
Subscriptor++
You can also do this by doing what you should do with passwords, and that's not store them in plain text ;) You should probably always store them as binary, and if not, you'll need to come up with an encoding algorithm that is case-sensitive and outputs in a non-case sensitive manor. Probably the easiest encoding which doesn't violate case sensitivity, yet leaves the field in text format is hex encoding.

TheJet

P.S. Case sensitivity will actually cause many more pains than case insensitivity on a database server, especially since 90% of the time you'll be using things like UPPER(search_field) = UPPER(@search_input), which actually increases the overhead of the server in general. After all, why do you think all those old mainframe programs have all their data in uppercase?? ;) (which ends up being a huge PITA for conversion, btw).
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by reboot:
Quote: "In most database applications, you want case insensitivity, just like a dictionary -- you don't want 'Apples' and 'apples' to sort differently."

It makes sense for sorts, but comparisons ???

Yep, makes perfect sense. Most everyday comparisons are case-insensitive. If I search for "apple" on Google, should it exclude "Apple" or "APPLE" as possible matches?

_When you want that_ you uppercase both sides of the equation.

It would be a burden on SQL programmers to use UPPER or LOWER on _every_ text data type in _every_ comparison, except that odd comparison where case sensitivity is desired. Out of every database I've ever created, _none_ have required case-sensitive comparisons.

It seems silly to have case insensitivity on because that means that if data is stored with original case, then the server is always uppercasing everything which is major overhead.

Major? Compared to the index scan and everything else that goes on during a search operation, the penalty is (practically) zero.

It could be useful if statistically you need case insensitivity more often than not, but I doubt that's a majority of cases.

Most searches are case insensitive. Remember, this is a database, not a programming language, and most databases store "everyday stuff," not variables names, filenames, and passwords. Most everyday searches are case-insensitive. If a user types in someone's name in lowercase, they still want it to match the name which may be stored with an initial capital.
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by _klamath_:
No, but Google stores everything case insensitively: that is smart. It would be dumb of them to store everything with case and then convert the case on every query, which is what SQL server apparently does.

Storing data the way it was entered is dumb?

Indexes, etc. in SQL Server would obviously be stored in a case-insensitive manner if you chose a case-insensitive sort order. I'm guessing that the only time it would actually need to convert case as you describe is if it needed to do a table scan.

But honestly, I'm not well-versed on the internal workings of SQL server. I only know that case-insensitive is the desired mode of operation on most searches.
 

Wozster

Smack-Fu Master, in training
56
Originally posted by _klamath_:
Quote: "If I search for "apple" on Google, should it exclude "Apple" or "APPLE" as possible matches?"

No, but Google stores everything case insensitively: that is smart. It would be dumb of them to store everything with case and then convert the case on every query, which is what SQL server apparently does.

How do you know what google does? Have you seen their code? I'm just curious. Thanks!
 

Wozster

Smack-Fu Master, in training
56
Originally posted by _klamath_:
Quote: "How do you know what google does?
"

There is a paper online by the Stanford grads you started google (it started as a student project in data mining); I dunno if it's changed since then though. You can find the paper online.

That looks like great reading, could you please provide a link? Thanks!
 

reboot

Ars Legatus Legionis
10,818
Originally posted by HL7:
Most searches are case insensitive. Remember, this is a database, not a programming language, and most databases store "everyday stuff," not variables names, filenames, and passwords.

Actually, Databases is what I do.

The reason I'm saying that case sensitivy should be the predominant case is because of all the large transactional system and datawarehouses I've worked on, I've only had once to consider cases insensitive comparisons.

That was when looking for a supplier name in one of the transactional systems I've worked on.
(arstechnica would obviously be the same as ArsTechnica)

I think that case insensitivity is mostly needed for comparing user input.
That would be the only 'general' case I can think of off the top of my head.

Under Oracle and DB2
where field = 'Bla'
isn't the same as
where field = 'bla'
At least by default, Hell I don't even know if the option exists to set it otherwise and I'm certified :eek: ;)

I mean, it's great that the option exists to set it to case insensitive, I'm just surprised it's the default...


Can someone tell me if
table1.field1 = table2.field2 is true in SQL Server
If field1 contains 'Bla' and field 2 contains 'bla' ?

[This message was edited by reboot on February 12, 2002 at 21:53.]
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by reboot:
Actually, Databases is what I do.

The reason I'm saying that case sensitivy should be the predominant case is because of all the large transactional system and datawarehouses I've worked on, I've only had _once_ to consider cases insensitive comparisons.

I find that surprising. What sort of data was being stored?

By contrast, I've _never_ had to consider case-sensitive comparisons. And databases is what I do too.

I think that case insensitivity is mostly needed for comparing user input. That would be the only 'general' case I can think of off the top of my head.

In my experience, that's the primary use of comparison on char and varchar fields. Most 'internal' (e.g., non-user-input) comparisons seem to end up being on int or other number fields.

I mean, it's great that the option exists to set it to case insensitive, I'm just surprised it's the default...

Well .... it is. But it's easy to change.

Can someone tell me if
table1.field1 = table2.field2 is true in SQL Server
If field1 contains 'Bla' and field 2 contains 'bla' ?

It is.
 

reboot

Ars Legatus Legionis
10,818
I find that surprising. What sort of data was being stored?

hmm.. in retrospect, data not entered by the user.

It's usually on machine generated data , or when user input was needed, they were presented with choices, not free text fields which isn't always possible but in my world it is.


I guess it makes sense in the Windows world. That's good to know.
I'm sure a couple Unix people went :eek: like I did though ;)
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by reboot:
hmm.. in retrospect, data not entered by the user.

It's usually on machine generated data , or when user input was needed, they were presented with choices, not free text fields which isn't always possible but in my world it is.

A lot of what I work with includes machine generated or precapitalized data (e.g. patient names, employee names, etc. that come from payroll or financial systems), but these fields are often searched by users. It would be a real pain in the ass to have to UPPER() ever WHERE clause. When comparing char or varchar fields, how often does case really matter? With case-sensitive passwords or filenames, it might. But on everyday data, most of your comparisons are going to be case insensitive.

I guess it makes sense in the Windows world. That's good to know.
I'm sure a couple Unix people went :eek: like I did though ;)

I've spent plenty of time in the Unix world, and case sensitive filenames still don't make sense. Why should program.c and program.C be two completely different files? It makes sense from a programmer's perspective, and it's certainly easier to code a case-sensitive system than a case-insensitive one, but it's a pain for us humans who have to use it. I'm sorry, but 'C' and 'c' are not two different letters, only two different ASCII codes.
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
By contrast, I've _never_ had to consider case-sensitive comparisons. And databases is what I do too.

Ditto that.

Generally, my machine-generated fields are numeric codes, not fields in which A could be different than a. All user-viewable/enterable data was case-insensitve with the exception of passwords/usernames, which aren't stored as CHAR anyways.

On rare occasions character codes will be used, such as when using a CHAR(1) flag that can be either machine or human readable: P = Pass, F = Fail, not translated by all reports to the word, but even there I would never issue (or allow to be issued) something like p = possible and P = probable.
 

TheJet

Ars Praefectus
4,598
Subscriptor++
storing passwords in binary gets you very marginal additional security (if any).

I never said that it did, it provides enough security that people randomly browsing the data (i.e. through an ad-hoc reporting tool) can't just see the passwords, they would have to work to get it. Additionally, it allows the passwords to be case sensitive by default. Like I said, if you really want security you need to encrypt the data, and then certainly store it in binary, storing it in text makes even less sense once the data is encrypted.

Then again, if you were designing a truly secure system, you probably wouldn't be storing the usernames/passwords in the regular database anyway, you'd be using some other external security system, including user certs, cards, etc. My comment was only that passwords should _never_ be stored as plain text, not ever. Even switching to binary encoding gives a modicum (albeit a very small one) of added security.

TheJet
 

petrov

Ars Tribunus Militum
2,714
Originally posted by TheJet:
Quote: "
storing passwords in binary gets you very marginal additional security (if any).
"

I never said that it did, it provides enough security that people randomly browsing the data (i.e. through an ad-hoc reporting tool) can't just see the passwords, they would have to work to get it. Additionally, it allows the passwords to be case sensitive by default. Like I said, if you really want security you need to encrypt the data, and then certainly store it in binary, storing it in text makes even less sense once the data is encrypted.

that's just my point. You can't encrypt the data securely. You can do it insecurely, but at that point, why not just use XOR or something equally simplistic (like no encryption)?

It's impossible to securely store the passwords in any reversible encryption scheme (i.e. where you can later decrypt them and get them back, e.g. to give a password back to a user that forgot his).

--sam
 
Status
Not open for further replies.