The below SQL statement working for all the values like 40000,99 but for 0 the expected value is 0.00 but the result is .00
trim(to_char(value,'9,999,999.99')
Could you please suggest any possible solution for this,
The format should be x,xxx,xxx.xx and 0 should be displayed as 0.00
Eg:40,000 , 1,250,000.00 , 0.00
You should explicitly indicate the zero before decimal point:
trim(to_char(value,'9,999,990.99')
Related
For example: if I do a select preferences from stores I get this outcome:
|preferences |
|----------------------------------------------------------------------|
|"debit_rate"=>"0.00", "credit_rate_1"=>"0.01", "credit_rate_2"=>"0.02"|
|"debit_rate"=>"0.03", "credit_rate_1"=>"0.04", "credit_rate_2"=>"0.05"|
|"debit_rate"=>"0.06", "credit_rate_1"=>"0.07", "credit_rate_2"=>"0.08"|
|"debit_rate"=>"0.09", "credit_rate_1"=>"0.10", "credit_rate_2"=>"0.11"|
Is there a way for me to get this outcome?
debit_rate
credit_rate_1
credit_rate_2
0.00
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
0.10
0.11
It looks like you are small change from making these sting into valid json. redshift has json functions that will allow for more intelligent parsing of these strings. See https://docs.aws.amazon.com/redshift/latest/dg/json-functions.html
If you just change the '=>' to ':' and wrap the whole thing in curly braces '{}' you should be there.
Then you can cast these strings to be type SUPER and access the data by key value. See: https://docs.aws.amazon.com/redshift/latest/dg/query-super.html
In SQL, I have two interger columns and I want to exclude rows where BOTH columns have a value of zero.
So in the below example, I would want rows 2 and 3 excluded only.
Col 1 Col 2
1 0.00 1.53
2 0.00 0.00
3 0.00 0.00
4 6.84 0.00
I have tried the below to test if it brings me just rows where one column holds 0.00 but the other does not, and there are no rows. Which means, my first argument is not working.
WHERE
(COL1 > 0.00 AND COL2 > 0.00)
AND (COL1 = 0.00
OR COL2 = 0.00)
You would use AND with a NOT to filter results so that if both columns contain 0, they are filtered:
SELECT * FROM mytable WHERE NOT(col1 = 0 AND col2 = 0)
If you want numbers to appear that have NO 0 in them such as a row: 12,12 the Exclusive OR XOR / <> will not work.
Your best practice is to use the WHERE NOT example I have listed.
Typically it is best to include example code showing what you have tried along with what is happening versus what you expect to happen. Based on the wording of this question it looks like all you would need in your WHERE clause is something like this. If this doe snot answer your question please be more specific as to why and what you have tried.
Proposed WHERE clause:
WHERE
-- This is going to only include rows
-- where one or both column values are non-zero
Col1 <> 0
OR Col2 <> 0
I'm trying to print
Condition 1: If currency appears as 0.00, then remove the decimal points (.00)
Ex: 0.00 -> 0
Condition 2: If currency appears as greater than 0.00, then keep the decimal places to 2 places (so, ######.00)
12.46 -> 12.46
0.00 -> 0
13.96 -> 13.96
I have tried the following but the CAST will not work:
CASE
WHEN CAST([Money] AS NUMERIC(10,2)) = 0.00 THEN CAST([Money] AS INT)
ELSE CAST([Money] AS NUMERIC(10,2)) END BankPile,
If you care about such details, then you need to cast the result as a stirng:
(case when CAST([Money] AS NUMERIC(10,2)) = 0.00 then '0'
else cast(cast([Money] as numeric(10, 2)) as varchar(255))
end)
You can't have a column that contains both decimal and integer types. I'd recommend just returning decimal values and setting the format in the display layer (form, report, web page, etc.)
I have a formula in my awk script which outputs non-integer numbers with variable number of decimals. So, I was wondering how I can save the outputs with a certain number of decimals, say for example 2, in an array. As an example:
awk 'BEGIN{for(i=1;i<10;i++){array[3/i]}}'
You can use sprintf():
awk 'BEGIN{for(i=1;i<10;i++){array[sprintf("%.2f", 3/i)]}}'
This will create an array with the following indexes:
1.00
0.50
0.33
0.60
0.43
1.50
3.00
0.38
0.75
When I run this query below,
SELECT clientid,
CASE WHEN D.ccode = '-1' Then 'Did Not Show' ELSE D.ccode End ccode,
ca,
ot,
bw,
cshT,
dc,
dte,
approv
FROM dbo.emC D
WHERE year(dte) = year(getdate())
I get the correct results.
It is correct result because ccode shows 'Did Not Show' when the value on the db is '-1'
However, when I do a UNION ALL so I can get total for each column, I get the results but then 'Did Not Show' is no longer visible when valye for ccode is '-1'.
There are over 1000 records with valuye of '-1'.
Can someone please help?
Here is the entire code with UNION.
SELECT clientid,
CASE WHEN D.ccode = '-1' Then 'Did Not Show' ELSE D.ccode End ccode,
ca,
ot,
bw,
cshT,
dc,
dte,
approv
FROM dbo.emC D WhERE year(dte) = year(getdate())
UNION ALL
SELECT 'Total',
'',
SUM(D.ca),
SUM(D.ot),
SUM(D.bw),
SUM(D.cshT),
'',
'',
''
FROM emC D
WHERE YEAR(dte)='2011'
I also tried using ROLLUP but the real issue here is that I can't get the 'Did Not Show' text to display when ccode value is -1
ClientID CCODE ot ca bw cshT
019692 CF001 0.00 0.00 1.00 0.00 0.00
019692 CH503 0.00 0.00 1.00 0.00 0.00
010487 AC407 0.00 0.00 1.00 0.00 0.00
028108 CH540 0.00 0.00 1.00 0.00 0.00
028108 GS925 0.00 0.00 1.00 0.00 0.00
001038 AC428 0.00 0.00 3.00 0.00 0.00
028561 Did Not Show 0.00 0.00 0.00 0.00 0.00
016884 Did Not Show 0.00 0.00 0.00 0.00 0.00
05184 CF001 0.00 0.00 4.50 0.00 0.00
This occurs because, each SQL statement within the UNION ALL query must have the same number of fields in the result sets with similar data types. Quoted from MSDN:
The following are basic rules for combining the result sets of two
queries by using UNION:
The number and the order of the columns must be the same in all queries.
The data types must be compatible
Now, your 'Did not show' output, changes the column data type. Try creating a pseudo column in both the queries and have a two column output, or else, give the output for when the case is not found as a numerical value.
EDIT: ccode is of nvarchar(50) type. But when you output 'Did not show', it is of text type, and there occurs a data type mismatch. One may wonder that nvarchar should be able to handle text, but technically it is a datatype mismatch, and it will create such runtime issues. Or atleast this is what I know according to my understanding..
I figured out the problem. The union actually worked. The only problem was that I was querying the wrong table which didn't have any value of '-1'.
So, essentially, I was right that UNION had nothing to do with why the query didn't work. It would have worked if I had the value I was using CASE statement against.