Connecting Tech Pros Worldwide Help | Site Map

Concatenate float data type

  #1  
Old October 16th, 2008, 04:05 PM
Zamdrist
Guest
 
Posts: n/a
I have a need to make a group of records 'unique' by concatenating a
foreign key value to another field, the two are float and varchar
type. Now, I'll have to cast the float column, but then I end up with
something like this:

'2.20824e+006 02316.PENDING00097840-CC'

The float value actually is: 2208239

How can I get a straight up number to text conversion of this value? I
tried Cast to varchar, that doesn't work.

Any help would be greatly appreciated, thank you!
  #2  
Old October 16th, 2008, 04:15 PM
Plamen Ratchev
Guest
 
Posts: n/a

re: Concatenate float data type


You can cast first to DECIMAL and then to character data type, or use
the STR function:

CREATE TABLE Foo(
keycol INT PRIMARY KEY,
datacol FLOAT);

INSERT INTO Foo VALUES(1, 2208239);

SELECT LTRIM(STR(datacol)),
CAST(CAST(datacol AS DECIMAL(10, 0)) AS VARCHAR(20))
FROM Foo

--
Plamen Ratchev
http://www.SQLStudio.com
  #3  
Old October 16th, 2008, 04:25 PM
Zamdrist
Guest
 
Posts: n/a

re: Concatenate float data type


On Oct 16, 10:12*am, Plamen Ratchev <Pla...@SQLStudio.comwrote:
Quote:
You can cast first to DECIMAL and then to character data type, or use
the STR function:
>
CREATE TABLE Foo(
* keycol INT PRIMARY KEY,
* datacol FLOAT);
>
INSERT INTO Foo VALUES(1, 2208239);
>
SELECT LTRIM(STR(datacol)),
* * * * CAST(CAST(datacol AS DECIMAL(10, 0)) AS VARCHAR(20))
FROM Foo
>
--
Plamen Ratchevhttp://www.SQLStudio.com
Thank you, that worked famously.
  #4  
Old October 17th, 2008, 06:15 PM
--CELKO--
Guest
 
Posts: n/a

re: Concatenate float data type


>I have a need to make a group of records [sic: rows are not records] 'unique' by concatenating a foreign key value to another field [sic], the two are float and varchar
type.<<

NO! Keys are discrete values by definition. FLOAT is an approximate
data type and cannot be a key. Please read any article on flaotign
point math and the IEEE Standards. When you test floats for equality,
there is this epsilon; a small value which matches two values when
they are "close enough" to each other. Since floats were only meant
to be used for computations, this works fine.

You need to use an exact numeric type or CHAR(n) for keys. Or decide
that you never want data integrity.
  #5  
Old October 21st, 2008, 03:45 PM
Philipp Post
Guest
 
Posts: n/a

re: Concatenate float data type


I have a need to make a group of records 'unique' by concatenating a foreign key value to another field, <

Without having more details, it sounds to be a candidate for a
compound primary key.

CREATE TABLE Foo
(bar1 DECIMAL(10,5) NOT NULL, -- something instead of FLOAT
bar2 VARCHAR(30) NOT NULL
REFERENCES Foo2(bar2), -- the foreign key
PRIMARY KEY(bar1, bar2)); -- compound key, no duplicates

brgds

Philipp Post
  #6  
Old October 22nd, 2008, 06:05 AM
--CELKO--
Guest
 
Posts: n/a

re: Concatenate float data type


>Without having more details, it sounds to be a candidate for a compound primary key <<.

No! Think about it. They are an approximate computation numeric.
Entities are discrete by their nature. There is no such thing as a
approximate key. This is a problem with (longitude, latitude) pairs
for a location and why GIS uses a grid system of triangles instead.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implicit Data Type Converson RN1 answers 3 February 26th, 2008 10:35 AM
compiling perl 5.8.7 on Solaris 8 Kirt Loki Dankmyer answers 0 November 22nd, 2005 04:05 AM
python-dev Summary for 2004-06-16 through 2004-06-30 Brett Cannon answers 0 July 18th, 2005 01:25 PM