I don't dabble in SQL queries much and rely on google when I need something more than the basics, and have come up with a problem.
I am trying to calculate a value and it returns a result rounded down to the nearest integer.
To test this out, I wrote the following query:
select ELAPTIME AS "ELAPSEC", ELAPTIME/60 AS "ELAPMIN" from CMR_RUNINF
The result is:
+-----------+-----------+
|ELAPSEC |ELAPMIN |
+-----------+-----------+
|258 |4 |
+-----------+-----------+
|0 |0 |
+-----------+-----------+
|2128 |35 |
+-----------+-----------+
|59 |0 |
+-----------+-----------+
I'm trying to do a bit more than this, but I've simplified it to make it easier to explain the problem. How do I ensure that this calculation returns the decimal point?
postgres=# SELECT 258/60::float;
?column?
----------
4.3
(1 row)
Your SQL product performs integral division because both operands are integers. ELAPTIME's integer type is determined be the table structure and 60 is automatically assumed to be integer because it has no decimal point.
There are two methods of resolving the issue:
Convert either operand to a non-integer numeric type explicitly:
CAST(ELAPTIME AS float) / 60
Write 60.0 instead of 60 so that the parser can see you are not dividing an integer by an integer:
ELAPTIME / 60.0
Simply try this
SELECT ELAPTIME AS "ELAPSEC", ELAPTIME/60 :: Float AS "ELAPMIN"
FROM CMR_RUNINF
Or:
SELECT ELAPTIME AS "ELAPSEC", ELAPTIME/60 :: Real AS "ELAPMIN"
FROM CMR_RUNINF
Fiddle Demo
Related
Right now I am using numeric(4,3) data type to represent a percentage in decimal form (ex. 57.2% = .572). The problem is, when I save it's ALWAYS displaying these decimals with a digit to the left of the decimal separator. So in other words it is displaying as 0.572 when I want it to display as .572 .However, there are occasions when I want a digit to the left of the separator(like when displaying the 1.000 ,meaning 100%).
What is the data type that I should use to accomplish this? Any help would be appreciated. Thanks.
SELECT to_char(0.157, '0.99%');
| to_char |
| :------ |
| 0.16% |
SELECT to_char(0.157, '9.99%');
| to_char |
| :------ |
| .16% |
dbfiddle here
Numbers are numbers. If you want to format them in a particular way, you can use to_char(). This is particularly true if you want to format them in different ways under different circumstances.
The documentation covers the situations you describe.
In mariadb or maybe others, decimal values in numeric type columns are not included in calculation. For example,
MariaDB [mydb]> create table test (a decimal(5,0),b double(5,0));
MariaDB [mydb]> insert into test values (1.5,1.5);
MariaDB [mydb]> insert into test values (1.5,1.5);
MariaDB [mydb]> select format(sum(a),3) from test;
+------------------+
| format(sum(a),3) |
+------------------+
| 4.000 |
+------------------+
MariaDB [mydb]> select sum(b) from test;
+--------+
| sum(b) |
+--------+
| 2 |
+--------+
Why don't they return "3" what I want?
And why are their returns different?
The (m,0) says to store 0 decimal places. So, 1.5 is rounded up (or down?) to 2 (or 1?).
The SUM() function works on what is stored, and knows nothing about the original 1.5.
DECIMAL(5.0) says to keep only 5 digits to the left of the decimal place, and none to the right.
DOUBLE(5.0) says even worse things; do not use it. Use plain DOUBLE.
I am having SSIS data flow task where I need to transfer data from one sql database to another sql database.
My source Database column datatype in SQL is float and in ssis its double-precision float [DT_R8].
Destination column datatype is Varchar(50) . When i execute the data flow , data transferred to destination table differs from source data eg:
+------------+---------------------------------+
| SourceValue| DestinationValue After execution|
+------------+---------------------------------+
| 0.579 | 0.578999999999999965 |
| 0.637 | 0.63700000000000001 |
| 0.503 | 0.503 |
+------------+---------------------------------+
So for some values its same but different for other.
I have tried 3 approaches to solve this issue :-
Tried to cast in query as cast(col as varchar)
Tried to cast as float cast(col as float)
Tried to change datatype in SSIS destination column same as of source ie. double-precision float [DT_R8] . though i am getting warning that datatype needs to change while executing.
But none of above steps solved my problem.
I have found solution that in source flow I have changed datatype from double-precision float [DT_R8] to DT_DECIMAL . Its working fine now , values are transferred to destination table as it is .
eg:
+------------+---------------------------------+
| SourceValue| DestinationValue After execution|
+------------+---------------------------------+
| 0.579 | 0.579 |
| 0.637 | 0.637 |
| 0.503 | 0.503 |
+------------+---------------------------------+
This is the way float works. I assume that you are using some tool to view the source values: most tools will trim float values (round them for display), so I would bet that 0.579 is actually 0.5789999 etc etc. If you don't know it already, float values are not precise anyway. Have a look at the Wikipedia article, especially section 7: Accuracy Problems.
We are facing a weird problem with one of our query.
Below is the query we are running
INSERT into test
SELECT
member.name as mem_name,
CASE WHEN ( member.dob>0 AND length (member.dob)=8 ) THEN (DATEDIFF(year,to_date("dob",'YYYYMMDD'), to_date(20140716,'YYYYMMDD'))) WHEN ( member.dob=0 ) Then 0 END As Age,
20140716021501
FROM
member
Below is the sample data present in our table.
|name |dob
|Ajitsh |0 |
|rk |51015 |
|s_thiagarajan |19500130 |
|madhav_7 |19700725 |
|1922 |0 |
|rekha |25478 |
|vmkurup |0 |
|ravikris |19620109 |
|ksairaman |0 |
|sruthi |0 |
|rrbha |19630825 |
|sunilsw |0 |
|sunilh |0 |
|venky_pmv |19701207 |
|malagi |0 |
|an752001 |0 |
|edsdf |19790201 |
|anuanand |19730724 |
|fresh |19720821 |
|ampharcopharma |19590127 |
|Nanze |19621123 |
The date of birth is stored in bigint as YYYYMMDD format.
In the data there are some rows, in which date is invalid like 0, 51015.
On some instances this query raises the following error.
INSERT INTO test not successful
An error occurred when executing the SQL command:
INSERT into test
SELECT
member.name as mem_name,
CASE WHEN ( member.dob>0 AND length (member.dob)=8 ) THEN (DATEDIFF(y...
ERROR: Data value "0" has invalid format
Detail:
-----------------------------------------------
error: Data value "0" has invalid format
code: 1009
context: PG ERROR
query: 92776
location: pg_utils.cpp:2731
process: query1_30 [pid=1434]
-----------------------------------------------
Execution time: 3.99s
1 statement failed.
But the strange thing is, it raises the error randomly and not all the time.
Many times it works without any change in query or dataset.
Sometime it also works in second or third attempt.
My doubt is that to_date function is giving this error. But why randomly
and not gives error on every run.
To support my assumption I also tried this small query.
SELECT to_date(20140716,'YYYYMMDD'), to_date(0,'YYYYMMDD');
But this also creates the same scenario. It raises error randomly, while
runs smoothly rest of the times.
If is it fine to ignore this type of values and just convert this to Date format you can follow the below way.
SELECT to_date('20140716','YYYYMMDD'), to_date('0','FMYYYYMMDD');
Here FM suppresses leading zeroes and trailing blanks that would otherwise be added to make the output of a pattern be fixed-width.
Based on the following SQL in Access...
TRANSFORM Sum([Shape_Length]/5280) AS MILES
SELECT "ONSHORE" AS Type, Sum(qry_CurYrTrans.Miles) AS [Total Of Miles]
FROM qry_CurYrTrans
GROUP BY "ONSHORE"
PIVOT qry_CurYrTrans.QComb IN ('1_HCA_PT','2_HCA_PT','3_HCA_PT','4_HCA_PT');
... my results returned the following datasheet:
| Type | Total Of Miles | 1_HCA_PT | 2_HCA_PT | 3_HCA_PT | 4_HCA_PT |
| ONSHORE | 31.38 | | 0.30 | 7.80 | |
This result is exactly what I want except I want to see zeroes in the cells that are null.
What are some options for doing this? If possible, I'd like to avoid using a subquery. I'd also prefer the query to remain editable in Access' Design View.
I think you have to use the Nz function, which will allow you to convert NULLs to another value. In this case, I used the (optional) part of the function to say, "If Sum([Shape_Length]/5280) is NULL, set it to 0". You may have to use quotes around the 0, I can't recall.
TRANSFORM Nz(Sum([Shape_Length]/5280), 0) AS MILES
SELECT "ONSHORE" AS Type, Sum(qry_CurYrTrans.Miles) AS [Total Of Miles]
FROM qry_CurYrTrans
GROUP BY "ONSHORE"
PIVOT qry_CurYrTrans.QComb IN ('1_HCA_PT','2_HCA_PT','3_HCA_PT','4_HCA_PT');