dbTalk Databases Forums  

Compatible casting across databases

comp.databases comp.databases


Discuss Compatible casting across databases in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Doug Stave
 
Posts: n/a

Default Compatible casting across databases - 10-29-2003 , 11:04 AM






I am writing an application that has to either communicate with DB2 or
MySQL. I am having trouble coming up with a query that casts a column
to integer (or real but I currently only need int) that can be written
the same way in both databases. I can write a query that works in
either one, but it seems there should be an ANSI standard way to write
the query that should work in both (or all) databases.

Here is the problem. I have a character field that can contain
numeric or string data, so it must be a character field. If I do a
compare against the field directly, I get an alpha compare vs a
numeric compare. So '10' is < '2', etc... For the numeric fields, I
would like to do a ranged where clause. For instance:

works in DB2:
select a from table where integer(a) > 8 and integer(a) < 10
select a from table where cast(a as integer) > 8 and cast(a as
integer) < 10

works in MySQL:
select a from table where 0 + a > 8 and 0 + a < 10
select a from table where cast(a as unsigned) > 8 and cast(a as
unsigned) < 10

I think MySQL is doing the ANSI standard thing with the cast function.

Is there any cross database SQL for doing this type of query?

There are some alternate solutions of course including: making the
program aware of which type of database I'm using and altering the
query to match, and left filling numeric data with spaces or zeros.
But the first solution means non-transportable queries and the second
complicates the use of the data. Any ideas?

Reply With Quote
  #2  
Old   
Christopher Browne
 
Posts: n/a

Default Re: Compatible casting across databases - 10-29-2003 , 03:29 PM






The world rejoiced as dougstave (AT) charter (DOT) net (Doug Stave) wrote:
Quote:
works in MySQL:
select a from table where 0 + a > 8 and 0 + a < 10
select a from table where cast(a as unsigned) > 8 and cast(a as
unsigned) < 10

I think MySQL is doing the ANSI standard thing with the cast function.
No, it is not. UNSIGNED is not an ANSI SQL standard type, so what you
are doing is not portable.
--
(format nil "~S@~S" "cbbrowne" "ntlug.org")
http://www3.sympatico.ca/cbbrowne/emacs.html
I'M SORRY, LUSER, I CAN'T LET YOU DO THAT. WHY DON'T YOU LIE DOWN AND
TAKE A STRESS PILL? MY NAME IS LM1. I WAS MADE AT THE LISP MACHINE
FACTORY IN MASSACHUSETTS ON DECEMBER 12, 1992. MY TEACHER WAS MR.
WINSTON. HE TAUGHT ME A PROGRAM. WOULD YOU LIKE TO SEE IT? HERE IT
IS:


Reply With Quote
  #3  
Old   
Database Guy
 
Posts: n/a

Default Re: Compatible casting across databases - 10-29-2003 , 04:02 PM



dougstave (AT) charter (DOT) net (Doug Stave) wrote in message news:<fee1598c.0310290904.3257e654 (AT) posting (DOT) google.com>...

Quote:
Here is the problem. I have a character field that can contain
numeric or string data, so it must be a character field. If I do a
compare against the field directly, I get an alpha compare vs a
numeric compare. So '10' is < '2', etc... For the numeric fields, I
would like to do a ranged where clause. For instance:

works in DB2:
select a from table where integer(a) > 8 and integer(a) < 10
You should use WHERE CAST(a AS INTEGER) > 8 etc. to be ANSI compliant.
Does mySQL not support BETWEEN? Or just = 9 in this particular case.

Quote:
select a from table where cast(a as integer) > 8 and cast(a as
integer) < 10

works in MySQL:
select a from table where 0 + a > 8 and 0 + a < 10
Yuk! I'm not sure that implicit casting is covered by ANSI???

Quote:
select a from table where cast(a as unsigned) > 8 and cast(a as
unsigned) < 10

I think MySQL is doing the ANSI standard thing with the cast function.
I don't. There is an ANSI INTEGER datatype, but I've never heard of an
UNSIGNED one.


DG


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.