Newbie help: sql string conversion

  • Thread starter Thread starter steve
  • Start date Start date
S

steve

Hi,
I am trying to avoid a horrendous amount of coding and see if i can get away
with a complex sql statement.

I have data values (measurements) which I have stored in the database in the
form of STRING so that i can keep the original format.

They look like this:

000078 -> 7.8 degrees
-99999M -> Missing data
000345 -> 34.5 degrees
-00993 -> -99.3 degrees
000011 -> 1.1 degrees

you get the idea.
They represent numbers (positive or negative) the last place is the decimal
, they all have 6 characters except the missing data which is -99999M (7
places).

How would I construct an SQL querry to be able to allow the user to retrieve
temperature between e.g. > -2.5 and <12.4 ?

TIA
-steve
 
Why is the data stored in this format? If these are numeric measurements you
will be much better off storing them with a numeric datatype. Storing
numbers as strings will just make your queries difficult and slow and also
make it hard to maintain any data integrity. Fix the design and convert the
data to numeric form is my advice.

If you've no other choice you could try something like this:

SELECT col
FROM Measurements
WHERE CAST(LEFT(col,6) AS INTEGER) > -2.5
AND CAST(LEFT(col,6) AS INTEGER) < 12.4
 
steve,

SELECT * FROM Table1
WHERE CAST(CASE RIGHT(Measurement, 1) WHEN 'M' THEN NULL ELSE Measurement
END AS decimal) * .1
BETWEEN -2.5 AND 12.4

-Andy
 
hmm i see your point and thanx for your answer.
As I said I'd rather keep them in this format for now.
However!
I was thinking is there a fast way that through an sql querry that I can
duplicate a table with a different name of course that will have the same
data but on the "proper" format?
give me a couple of keywords and I'll look google them if you can

Thanx again!
 
Create a new table, then use the query I gave you to INSERT into it:

INSERT INTO NewTable (...)
SELECT ...
FROM OldTable

But if the data is changing, maintaining two copies of it is hard work and
unnecessary. The usual practice is to validate and transform data once and
then maintain it in a consistent, strongly-typed relational format in the
database. If you validate the data properly once then you won't need the
original format again. If you don't then you pay the price every time you
query the table.
 
David said:
Why is the data stored in this format? If these are numeric measurements
you will be much better off storing them with a numeric datatype.
Storing numbers as strings will just make your queries difficult and
slow and also make it hard to maintain any data integrity. Fix the
design and convert the data to numeric form is my advice.

And store the missing values as NULL.
 
I think i *will* loose my mind!
Thanks both of you for your help.

You see, the problem is that i can have a few flags at the end of the string
which mean somehing. e.g. M means missing, T means Trace, E means estimated,
etc.Information that i should have. If i split each column into two ,
well....possible but a lot of work and too much overhead since most
measurements are "clean".

That's the reason i kept the data in their original format.

Now if i dont convert to strings how the heck am i going to create the sql
string to send to the database with things like:
temperature between so and so, humidity bigger than 50, blah blah blah....
The string is created by an interface in VB where the user scrolls down
various controls and selects things. But how would you enter the bounds of
temperature in a textbox since its not stored as an integer AND it might
have a stupid letter at the end of its value!!!

I could do the coding after the results are returned.
I think My Biggest problem is string comparisons!!! If the user wants a
temperature between -7 and +15 how well and reliably can I create code to
compare "-00007" and "000015".

Just some thoughts from my brainstorming....
 
steve said:
I think i *will* loose my mind!
Thanks both of you for your help.

You see, the problem is that i can have a few flags at the end of the string
which mean somehing. e.g. M means missing, T means Trace, E means estimated,
etc.Information that i should have. If i split each column into two ,
well....possible but a lot of work and too much overhead since most
measurements are "clean".

That's not a "problem"! Basic database design says you shouldn't keep
multiple pieces of information in the same column. Doing it right
wouldn't be that difficult code wise. You simply have a column with your
valid codes in a check constraint and have the default be the "clean"
code if that is the most common entry.
That's the reason i kept the data in their original format.

Now if i dont convert to strings how the heck am i going to create the sql
string to send to the database with things like:
temperature between so and so, humidity bigger than 50, blah blah blah....
The string is created by an interface in VB where the user scrolls down
various controls and selects things. But how would you enter the bounds of
temperature in a textbox since its not stored as an integer AND it might
have a stupid letter at the end of its value!!!

You can do all that manipulation on the front in via code.
I could do the coding after the results are returned.
I think My Biggest problem is string comparisons!!! If the user wants a
temperature between -7 and +15 how well and reliably can I create code to
compare "-00007" and "000015".

Just some thoughts from my brainstorming....

Honestly, your brainstorming is confusing the heck out of me. Or maybe
you don't understand the numeric data type. A query of a numeric column
for all values between -7 and +15 would be very simple: WHERE Temp
BETWEEN -7 and 15. Numeric data is not stored with leading 0's.

Zach
 
You see, the problem is that i can have a few flags at the end of the
string which mean somehing. e.g. M means missing, T means Trace, E means
estimated

Then you have a non-atomic column, which is a violation of the most
fundamental relational design principles. This information belongs in a
separate column.
 
But how would you enter the bounds of temperature in a textbox since its
not stored as an integer AND it might have a stupid letter at the end of
its value!!!
I think My Biggest problem is string comparisons!!! If the user wants a
temperature between -7 and +15 how well and reliably can I create code to
compare "-00007" and "000015".

Yep, it's lousy... So why waste time on it when you could just redesign the
table properly :-)
 
Back
Top