Query to convert exponential number to float in SQL Server - sql-server-2005

I am using
SELECT CAST('5E-05' AS float)
result = 0.00005
and it is not working. Help.
https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=5E-05+to+decimal

Provide correct output for this so that we test and throw few more sample data.
Try this,
SELECT CONVERT(decimal(18,8), CAST('5E-05' AS FLOAT))

Related

Return AVG as FLOAT in MS-Access

I am trying to return the Average as a FLOAT, but when I use the code bellow, I get an error every time in Access saying MissingOperator. What am I missing in my code?
SELECT AVG(CAST(Quantity AS FLOAT))
FROM Orders
SELECT AVG(CDBL(Quantity))
FROM OrderDetails;
I often just write:
SELECT AVG(1.0 * Quantity)
FROM Orders;
However, this might return the value as a decimal rather than a float.

MS Access Date Format and Compare yields wrong result

SELECT Format('06-12-2018','dd-MM-yyyy') > Format('31-07-2018','dd-MM-yyyy')
why i am getting output as '0' meaning false, while running this query on ms access ?
As dd-mm-yyyy is your local format, use DateValue:
SELECT DateValue('06-12-2018') > DateValue('31-07-2018')
or, if the values are hardcoded, though this makes little sense, simply:
SELECT #2018/12/06# > #2018/07/31#
Try this:
SELECT cdate(#06-12-2018#) > cdate(#31-07-2018#)
for datediff i think you need below type function
example
SELECT DateDiff("yyyy", #13/01/1998#, #09/05/2017#);

Percentage and Decimals

I am trying to convert a varchar to a percentage with a decimal. For example, my report is returning 13590 as a result for a rate, which I would like to have a result of 13.590%. I can't seem to get this to work, any help would be appreciated.
You can use Arithmetic operator inside your query, e.g.
SELECT (report / 100) as rate FROM MyTable;.
If the % is needed, look at the CONCAT function.
Not a very elegant solution, and will drop trailing zeros
SELECT cast(cast(columnA as float) / cast(1000 as float) as varchar(20)) + '%'

control in display Decimal separators in sql server

I have problem in display the decimal numbers, it has many number.
My sql statement :
Select sum(HS$totalMoney)
the result :
12132.123444343
I want to display as 12132.12 without the another number
Thanks.
If your logic is for money you should first round the values not truncate
select CONVERT(decimal(18,2),round(12132.123444343 ,2)) gives 12132.12
select CONVERT(decimal(18,2),round(12132.125944343 ,2)) gives 12132.13
Try this one -
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
if you are using mysql, use code blew
SELECT TRUNCATE(sum(HS$totalMoney), 2);
this query slove your problem
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
or you can use
select CONVERT(decimal(18,2),round(12132.1255555 ,2))
SELECT CONVERT(decimal(21, 2), sum(HS$totalMoney))
-- This one will round in SQL Server but truncate in ASE 15 (which was available to me at the time)
SELECT CONVERT(decimal(21, 2), round(sum(HS$totalMoney), 2, 1))
-- This one uses a variant of ROUND supported by SQL Server, but not ASE 15 (and will truncate the third and subsequent decimal places).
The round function has a function parameter to truncate instead of round:
select round(12132.123444343 , 2, 1)
From here:
http://msdn.microsoft.com/en-us/library/ms175003.aspx

SQL Server 2008 : Avg. timing in milliseconds

I have this query
select
substring((convert(varchar, PostTime, 120)), 1, 11),
AccountNumber,
sum(Cast(datediff(ss, TranTime, AuthProcessedTime) as money)) / COUNT(1) AverageTime,
COUNT(1) NumberOfCount
from
AuthTransactions WITH (NOLOCK)
group by
substring((convert(varchar, PostTime, 120)), 1, 11), AccountNumber
In this query AverageTime comes in sec. but I want to see it in the following format HH:MM:SS.msmsms because most of the time results come in milliseconds. So is it possible to see the result in this format?
Thanks in advance.
Try this:
SELECT
Convert(date, PostTime) PostDay
AccountNumber,
Avg(Datediff(ms, TranTime, AuthProcessedTime) * 1.0) AverageTime,
Count(*) NumberPerDay
FROM
dbo.AuthTransactions
GROUP BY
Convert(date, PostTime),
AccountNumber
There were several problems with the query that I eliminated or corrected:
Always specify a length for the char data types.
Don't convert dates to strings to remove the time portion. Use DateDiff and DateAdd, or since you're using SQL 2008 you can convert to date.
Don't use WITH (NOLOCK) unless you really know what you're doing. It will eventually bite you.
Don't convert to money. That's just weird. It has 2 decimal places, and if you really want that, then convert to decimal([something], 2). Otherwise someone is going to see your code and go "what the heck?" and 'fix' it for you, even if that's what you wanted. You can get a sufficiently-precise decimal conversion just by multiplying by 1.0 like I showed.
Do specify the schema of objects. It is small, but it helps performance and (if I am not remembering incorrectly) not doing so can cause unnecessary plan recompilation.
In your datediff(ss,TranTime,AuthProcessedTime)
change the ss to ms.
ms is used for milli second.
like this
select substring((convert(varchar,PostTime,120)),1,11),
AccountNumber,
sum(Cast (datediff(ms,TranTime,AuthProcessedTime) as money)) / COUNT(1)
AverageTime,COUNT(1) NumberOfCount from AuthTransactions
WITH(NOLOCK) group by
substring((convert(varchar,PostTime,120)),1,11),AccountNumber