Cast 2 decimal values only if currency is not 0.0000 - sql

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.)

Related

Filter for a float in pandas results in nothing when I know the value exits

Here's a link to the practice file I'm using
https://1drv.ms/x/s!AnMRr0Q8-EGOg7kRKRCLHCIt1lT0qQ?e=1rOoWF
I am calculating a profit margin using the following code:
The table has several columns but the most important columns for my analysis look like this:
PROCODE
AV_COST
CURSELLPRI
8030106009
.225
.75
1721612503
.327
1.09
df = pd.read_excel('processed inventory.xlsx')
df.insert(6,'Profit',0.0)
df.loc[df['CURSELLPRI'] == 0, 'Profit'] = 0
df.loc[df['CURSELLPRI'] != 0, 'Profit'] = (df['CURSELLPRI'] - df['AV_COST']) / df['CURSELLPRI']
My output looks like this:
PROCODE
AV_COST
CURSELLPRI
Profit
8030106009
.225
.75
0.700000
1721612503
.327
1.09
0.700000
When I filter for Profit = .7 or 0.7 or 0.700000 nothing returns.
I've tried several other values and nothing ever returns when I do
df[df['Profit'] == 0.XX]]
You need np.isclose for filter by floats, because problem of precision of floats:
df[np.isclose(df['Profit'], 0.7)]
For test real values in column try convert to list:
print (df['Profit'].tolist())

Transformation of units in SQL Server

I have a dataset, df, that has a column of values that are in MB. I would like to transform into TB.
MB
10000000
20000000
Desired
TB
9.09
18.18
Doing
select MB AS 'TB', (CONVERT([int],round([MB]/((1024)*(1024)),(0)))) AS TB from df
However, the result I get is
MB
0
0
I am still researching. Any suggestion is appreciated
/ is integer division in SQL Server.
It means, that for example
SELECT 4 / 5
will return 0.
But, if you write
SELECT 4 / 5.0
you'll get 0.8
5.0 is treated as decimal type and all values in the expression are converted to decimal and division is no longer integer.
So, you can use 1024.0 constant in the expression, and all the values in it will be converted to decimal type and division will not be integer.
In the question you say that you want to show results with two decimal places, so you should not convert result to int.
select
[MB]
,round([MB]/(1024.0*1024.0), 2) AS TB
from df

Exclude rows where two columns BOTH have a value of zero, not one or the other

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

SQL NUMBER FORMAT

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')

I can't seem to get my grand Total to calculate correctly

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.