Convert Text To Float In SQL Statement
Feb 20, 2004
Hi,
Can you guys help me out?
I m trying to sum up some varchar-typed field. I need to convert it to float before doing the summing up so I m using "Cast".
I do get the answer but its not the correct figure. My SQL statement is as follow:
SELECT Sum((Cast(Qty1 as float)) + (Cast(Qty2 as float))) as intAnswer FROM TableName
Please help.
View 6 Replies
ADVERTISEMENT
Apr 9, 2007
I can't take full credit for this. I want to share this with Jeff Moden who did the important research for this calculation here.
All I did was just adapting some old code according to the mantissa finding Jeff made and optimized it a little
Some test codeDECLARE@SomeNumber FLOAT,
@BinFloat BINARY(8)
SELECT@SomeNumber = -185.6125,
@BinFloat = CAST(@SomeNumber AS BINARY(8))
SELECT@SomeNumber AS [Original],
CAST(@SomeNumber AS BINARY(8)) AS [Binary],
dbo.fnBinaryFloat2Float(CAST(@SomeNumber AS BINARY(8))) AS [Converted],
@SomeNumber - dbo.fnBinaryFloat2Float(CAST(@SomeNumber AS BINARY(8))) AS [Error]
And here is the code for the function.CREATE FUNCTION dbo.fnBinaryFloat2Float
(
@BinaryFloat BINARY(8)
)
RETURNS FLOAT
AS
BEGIN
DECLARE@Part TINYINT,
@PartValue TINYINT,
@Mask TINYINT,
@Mantissa FLOAT,
@Exponent SMALLINT,
@Bit TINYINT,
@Ln2 FLOAT,
@BigValue BIGINT
SELECT@Part = 1,
@Mantissa = 1,
@Bit = 1,
@Ln2 = LOG(2),
@BigValue = CAST(@BinaryFloat AS BIGINT),
@Exponent = (@BigValue & 0x7ff0000000000000) / EXP(52 * @Ln2)
WHILE @Part <= 8
BEGIN
SELECT@Part = @Part + 1,
@PartValue = CAST(SUBSTRING(@BinaryFloat, @Part, 1) AS TINYINT),
@Mask =CASE WHEN @Part = 2 THEN 8 ELSE 128 END
WHILE @Mask > 0
BEGIN
IF @PartValue & @Mask > 0
SET @Mantissa = @Mantissa + EXP(-@Bit * @Ln2)
SELECT@Bit = @Bit + 1,
@Mask = @Mask / 2
END
END
RETURNSIGN(@BigValue) * @Mantissa * POWER(CAST(2 AS FLOAT), @Exponent - 1023)
END
Thanks again Jeff!
Peter Larsson
Helsingborg, Sweden
View 3 Replies
View Related
Jun 21, 2007
Hi,
why does converting integer to float take so long? Its a column with about 5 Million rows.
I want to avoid cast(inumber1 as float) / cast(inmuber2 as float), thats why converting them. Queries should be a bit faster after that.. hope so :)
Thanks a lot
View 14 Replies
View Related
May 23, 2008
hi, how can I convert a float to a char so I can do:
'£' + convert(char,amount) AS [money]
When I do the above the number is like 1.07848e+006
rather than 1078480
thans for any help.
jamie
View 20 Replies
View Related
Apr 11, 2006
I have a sp that receive a rango of float values,and I need to convert this values to a varchar(20).
I am trying the next but I got a strange result.
@intNoTarjIni = 121456790
set @strTarjeta=cast(@intNoTarjIni as varchar(20))
returns: 1.21457e+008
set @strTarjeta=convert( varchar(20),@intNoTarjIni)
returns:1.21457e+008
How can I convert sucessfully a float to varchar?
View 2 Replies
View Related
Jun 26, 2007
Hi -
My field TDMergerVotePercent is defined as a float field. I want it to return 'NA' when the value is -1 but I'm getting the error message
'Error converting data type varchar to float' in my aspx page. Is there where the CAST function can be used? Thanks
CASE
WHEN TDMergerVotePercent = -1 THEN 'NA'
WHEN TDMergerVotePercent IS NULL THEN ''
ELSE TDMergerVotePercent
END AS TDMergerVotePercent
View 4 Replies
View Related
Feb 15, 2008
I have a data type float with a value of 10000487930 that I'm trying to insert into a data type nvarchar and am getting the result of '1.00005e+010'. I've tried cast(field as nvarchar) however this is not working. What might fix this? I cannot change the insert table data type.
View 3 Replies
View Related
Jul 23, 2005
Hello,I would like to convert a field from ntext field found in one databasetable to float field found in another database table. The reason why Iwant to do this is a long one.I have tried the following and playing around with the following:declare @valuePointer varbinary(16)<Row cursor logic to initialize @valuePointer to be a pointer to thesource ntext field>update TargetFloatTable set TargetFloatTable.TargetFloatValue =CAST(CAST(@valuePointer AS nvarchar) AS float)where TargetFloatTable.Id = @Idbut is not working for me.Hoping someone out there can help.Thanks,Cally
View 5 Replies
View Related
Aug 26, 2005
Select Cast('100.1234' as float)give me the result 100.1234Now when I convert it back to char I want exactly 100.1234Select Convert(char(100),Cast('100.1234' as float))Gives me 100.123 (Here I was expecting 100.1234)When I doSelect STR(Cast('100.1234' as float),25,4)I get back the result as 100.1234However here I am not sure how many digits do I have after the decimalpoint. If I put some value likeSelect STR(Cast('100.1234' as float),25,8)I get 0's appended to it, which is again not desired.Thanks in advance,Jai
View 4 Replies
View Related
Mar 7, 2008
Is this possible? I'm trying to user a lookup task and the data I want to compare is a varchar to float. How can I do this? I tried using the data conversion task and it didn't work and also tried cast and convert. Is this even possible or is there a way around it?
thanks,
View 14 Replies
View Related
Feb 12, 2007
I've three columns:
Length Width Height
1.5 2.5 10
2 3.7 19
in Query I want to display Like 1.5 X 2.5 X 10 (Length, Width, Height).....
???
View 1 Replies
View Related
Jan 23, 2002
I'm running the following statement...
The column is currently float (8). Need to convert to numeric. I've tried cast, convert. no go for either.
Any help on this would be greatly appreciated.
..... select ... convert(numeric(38,0), colname)
TIA,
Jeremy
View 1 Replies
View Related
Jan 4, 2006
Hello,
Is there a way to convert varchar or char to float?
Thank you
View 6 Replies
View Related
Jan 10, 2012
I have a field in my database that is stored as varchar. The values are usually contain a decimal, and should have really been a float or decimal. In order for me to do analytics in my BI environment, I need to convert this to a float or decimal.
eg of values.
10.00
20.00
0.00
15.00
or could be missing when I use cast(value as float) or cast(value as decimal(9,2)) or convert(float, value) I get an error
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to numeric.
View 2 Replies
View Related
Apr 2, 2014
What actually happens when I convert date to float? Select convert(float,getdate(),112)
View 1 Replies
View Related
Feb 11, 2015
I have the following CASE statement:
SELECT CASE WHEN [TBL_whole_rock_geochem].au = 0 THEN '<' + [TBL_ActLab_codes].[Au] ELSE [TBL_whole_rock_geochem].[Au] END AS [Au det]
It errors on the highlighted code.
Error converting data type nvarchar to float
the code should return :
<0.5
<1
I have tried '<' + ISNUMERIC([TBL_ActLab_codes].[Au]) but received this error
Conversion failed when converting the varchar value '<' to data type int.
How I can concatenate the < with a float value?
View 5 Replies
View Related
May 16, 2008
Hi,
how to convert nvarchar datatype to float?
Regards
Prashant
View 10 Replies
View Related
Jul 20, 2005
Hello I have some problems with converting varbinary to float in T-SQLstored procedure. In my C# application i have table of structures withdouble type fields. Size of this table is variant. I have to send thistable to SP. Because of performance i want to send it only once andwhole. So I have to cut this varbinary blob in my Stored procedure toreceive structure field values. But I have problems with double fields- conversion from varbinary to float in T-SQL is not allowed. Pleasehalp me - thx for any advise.Maciej
View 1 Replies
View Related
Oct 6, 2015
I have a column type of float and How to convert it show like this
default value =39260.80 MY db use this ',' seperator instead of '.'
wanna convert 39,260
This code is working if using '.' seperator
SELECT parsename(convert(varchar,CAST(floor('39260.80') AS MONEY),1),2)
not work using ',' seperator
SELECT parsename(convert(varchar,CAST(floor('39260,80') AS MONEY),1),2)
View 12 Replies
View Related
May 18, 2006
I'm trying to move records from a SQL table with a float column to a DB2 database that has the column defined as Decimal (8,2) It keeps crashing saying it has a type mismatch problem. I tried changing my source command to pass in the column already converted and it still crashes on this. I also tried doing a data conversion task to do the conversion and I still get the same error. Any ideas?
View 3 Replies
View Related
Apr 2, 2014
I seem to always get the "Fun Stuff" to try and figure out. I have an entire table that was pumped out of Oracle. I even hate saying that word!
There are a couple columns that are Float data type, and they are storing phone numbers as a Float data type. I am not able to CAST these into anything that is legible.
This is one of the values that I made up that look like some of the others.
9.72732e+009
View 2 Replies
View Related
Mar 18, 2015
how to convert float to timestamp in single select query..for exp. i have float as 1.251152515236 ,i want to convert this to datetime and from datetime to timestamp... i.e. 26:11:00
View 6 Replies
View Related
Dec 28, 2007
Hi there, I've a question regaarding datatype conversions... I can't convert the above mentioned datatype. I always get an error message that the conversion fails. Doesn't matter If convert or cast is used.
How would you convert the above mentioned variable into float???
View 8 Replies
View Related
Mar 8, 2007
I have been given some data from a Mainframe (AS400?) which has some fields coded in Packed Decimal. I have been able to load the data into a SQL2005 database table, but I now need to convert the Packed Decimal data in the binary(6) field to the appropriate integer (or float) value.
The field contains values such as the following:-
0x20202020200C
0x202020022025
0x20202020DFFA
I don't know how to interpret these. Has anyone got a function that can do this for me?
I've read several articles online that explain how packed decimal works, but none tell me how to interpret the last of my three examples. Can you help?
Thanks!
View 2 Replies
View Related
Mar 18, 2015
How to convert float to timestamp in single select query
E.g., i have float as 1.251152515236 ,
I want to convert this to datetime and from datetime to timestamp... i.e. 26:11:00
View 1 Replies
View Related
Apr 27, 2015
There is a column in a table that has values like '23 + 45 + 63', '2 + 54 - 22'... and so on. I want to get the result of this formula as float like 131, 34... and so on. Is that possible with a SELECT statement. I tried:
SELECT CAST('23 + 45 + 63' AS FLOAT)
Didn't work.
View 14 Replies
View Related
Apr 29, 2008
Hi,
I receive blanks for a column called value and i need to represent it as a blank or NA into a colum whose datatype is float in the datawarehouse.
how is this possible because in current schenario a blank is being converted to 0
which is not the right thing.
i would like to retain the blank in conversion from char to float ,
please explain me if it can be done or how to overcome this issue.
Thanks
View 1 Replies
View Related
May 29, 2008
Can some one help me convert plain text to reach text format using t-sql
I am on MSSQL 2005
Thanks
View 1 Replies
View Related
Feb 23, 2001
Hello.
I'm wondering how I CONVERT or CAST a column of type TEXT to INT with T-SQL ?
Also, is there anyway to catch an error in a Stored Procedure. For example if you try to convert a CHAR column to INT when the column has an 'a' in it, can I try to convert and if it did't work I do something else ?
Or is there a way to check if a column has only numbers ?
Thanks for any help, the CONVERT Text to Int is the most urgent ...
/ Daniel
View 1 Replies
View Related
May 27, 2008
Hi,
I have a column with records as follows:
5 days
7 days
15 days
20 days
30 days
60 days
100 days
120 days
How can I only take out only the numeric part and make sure that this will be displayed as value as I have to make calculations using that value...
Please help
Thanks!
View 5 Replies
View Related
Jan 11, 2006
I am trying to write a query in access that will pull results that arein the database in a text field and convert to where I can get aaverage including decimals. Any Ideas?-----------------------Start SQL--------------------------------------SELECT AVG(CAST(dbo_sur_response_answer.answer_text AS int)) ASavg_correctFROM (dbo_sur_response_answerINNER JOIN dbo_sur_subitemON dbo_sur_response_answer.subitem_id = dbo_sur_subitem.subitem_id)INNER JOIN dbo_sur_responseON dbo_sur_response_answer.response_id = dbo_sur_response.response_idWHERE (((dbo_sur_response.completed_yn)="Y") AND((dbo_sur_subitem.subitem_id)=478));-----------------------End SQL--------------------------------------
View 5 Replies
View Related
Nov 14, 2006
I have an image data type in a table (it is a digital signature) and one of my users wants me to search the image data and see how many people have a certain attribute in their digital signature. I know the image data is HEX, but how, in query analyzer, can I view it as the XML digital signature data that it really is? So, how can I convert hex to text in sql server?
View 2 Replies
View Related
Mar 18, 2004
Hi all
iam trying to but a varchar variable into a TEXT var but i get this error "The assignment operator operation cannot take a text data type as an argument"
anybody know what shall i do
best regards
View 7 Replies
View Related