Oracle TO_CHAR Format Mask for displaying both integral numbers and floating point numbers - sql

I'm trying to find the correct Oracle format mask to display numbers on an Apex page in a report in a certain way.
Most of the times these numbers are integers but sometimes these numbers can be floating point numbers.
Let's say I have the following three queries:
Query 1
SELECT TO_CHAR(1, '<Format Mask>', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
Query 2
SELECT TO_CHAR(0.1, '<Format Mask>', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
Query 3
SELECT TO_CHAR(0.01, '<Format Mask>', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
Now I want to use one single format mask which will give me the following results:
Result 1
1
Result 2
0,1
Result 3
0,01
Can anyone provide me with the correct format mask to achieve this?
I've tried a format mask like FM990D999 but it leaves me with a comma trailing the 1 in Query 1.

There are ways to alter your column value in the query while still retaining (some) of the functionality in the report(s). However, having multiple such columns and multiple report you might find there is a lot of overhead for little gain.
Look at this post on the OTN forums: order by date in IR
The issue is much the same: the data in the column represents a date but is actually not a date. This post contains a solution to use in apex < 4.2.
From 4.2 onwards you have a better option called the HTML expression.
Again, linked from OTN: Re: Report formatting/sorting issue
Quoted from linked post, user fac586
Include both variance and abs(variance) in the query:
SELECT
region,
estimate,
actual,
(estimate - actual) AS variance,
ABS(estimate - actual) AS abs_variance,
(CASE
WHEN (estimate - actual)>=0 THEN 'green'
WHEN (estimate - actual)<0 THEN 'red'
ELSE NULL
END) AS variance_color
from
expenses
And the HTML Expression for the "variance" column is:
<span style="color: #VARIANCE_COLOR#; font-weight: bold;">#ABS_VARIANCE#</span>
Hide the #VARIANCE_COLOR# and #ABS_VARIANCE# columns.
#ABS_VARIANCE# is the value shown in the column, but the sort is
performed in the underlying SQL using the original variance value.
This is much like Alex suggested but is a bit more work: formatting in the source, adding an html expression, hiding the other column.
I suppose it depends on how far you want to drive it. Why not just apply the format to the column through its attributes?
Also be aware it is possible to use string substitution syntax in those fields. You could have a couple application items containing format masks, and then reference the correct mask in the format mask field.
Eg:
Application item AI_FORMAT_MASK1 has a value FM9990D00.
In the format mask field you can then use &AI_FORMAT_MASK1.

Related

How do I subtract these two columns in SQL and make an extra column that shows the difference?

Begin_Term
End_Term
2018
Current
-select-
Current
2015
2019
-select-
2018
I used using the case when SUM('End_Term') - SUM ('Begin_Term) then 1 else 0 end but it only shows 0's and 1's for the results. I'm also using DOMO so the SQL formatting is alot different.
Does selecting a new column that merely subtracts the two columns (e.g. SELECT 'End_Term' - 'Begin_Term' AS Diff) mostly meet your needs?
As comments indicated, 3 of the 4 sample rows you gave are not reasonable to expect to process, since the columns do not both contain numbers.
If you have special case strings like "Current" and "-select-" that you wish to convert to numbers, then I suggest you will want to do this first, and then do your math second.
For example:
SELECT CASE WHEN 'End_Term' = "Current" THEN year(CURDATE()) ELSE 'End_Term' as 'End_Term'
My understanding is that DOMO handles type casting / conversion transparently behind the scenes so there's no need to actually worry about whether the column itself is a varchar.
I'm not sure what "-select-" is meant to be. It seems to me like erroneous placeholder form data that shouldn't have ended up submitted or inserted into your database in the first place.

SQL to powerBI expression?

How to write this expression in PowerBI
select distinct([date]),Temperature from Device47A8F where Temperature>25
Totally new to PowerBI. Is there any tool that can change the query from sql to PowerBI expression?
I have tried so many type of different type of expressions but getting error, Most of the time I am getting this:
The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
Need help, Thanks.
After I posted my answer, wondered if your expected result is get only one date by temperature, In other words, without repeated dates in your result set.
A side note: select distinct([date]),Temperature from Device47A8F where Temperature>25 returns repeated dates since DISTINCT keyword evaluate distinct columns values specified in the SELECT statement, it doesn't return distinct values in a specific column even if you surround it with parenthesis.
Now what brings us here. What I can see in your error is that you are trying to use a table-valued (produces a table with multiple columns) expression in a measure which only accepts scalar-valued (calculate only one value).
Supposing you have a table like this:
Running your SQL query you will get the highlighted in yellow rows:
You can see 01/09/2016 date is repeated. If you want to create a measure you have to define what calculation you want to show for temperature. i.e, average, max or min etc.
In the below expression is being calculated the maximum temperature greater than 25 per date:
MaxTempGreaterThan25 =
CALCULATE ( MAX ( Device47A8F[Temperature] ), Device47A8F[Temperature] > 25 )
In this case the measure MaxTempGreaterThan25 is calculated per date.
If you don't want to produce a measure but a table. In the Power BI Tool bar select Modeling tab and click New Table icon.
Use this expression:
MyTemperatureTable =
FILTER ( Device47A8F, Device47A8F[Temperature] > 25 )
It should produce a new table named MyTemperatureTable like this:
I recommend you learn some basics about DAX, it is pretty different from SQL / T-SQL and there are things you can't do depending on your model and data.
Let me know if this helps.
You probably don't need to write any code if your objective is to show the result in a Power BI visual e.g. a table. Power BI naturally aggregates data if the datatype is numeric (e.g. Temperature).
I would just add a Table visual on a Report page and add the Date and Temperature columns to it. Then in Visualizations / Fields / Values I would click the little down-arrow on the Temperature field and set the Aggregation e.g. Maximum. Then in Visualizations / Fields / Filters I would click the little down-arrow on the Temperature field and set the Filter e.g. is greater than: 25
Hard-coded solutions are unlikely to survive the next question from your users e.g. "but what if I want to see Temperature > 24? Or 20? Or 30?"

How to normalise inconsistent category labels in teradata

Table has text labels for category identification
The spelling has changed over time
I want to normalise the text labels when I count the records in each category
So for example I have The category labels:
'Ready to go' and 'Readytogo'
But I also have another text value abc that I want to replace with Abcd
The rest Inwant to keep in my Groupby and Count
How can I count these in the new group names in Teradata?
At the moment I'm using case statements for the ones I'm okay with, then using OREPLACE to switch one of the values but how do I nest it so that I can OREPLACE 2 or more values with 2 or more new ones? Is OREPLACE the best function to use here?
Thanks
Apologies I have managed to solve it using multiple WHEN, THEN clauses as part of 1 case statement
This changes the text value ax to bn along with Fx to BM for example, as required
The OREPLACE function was not recognised in the release of Teradata that I am using
I will specify question better next time
Thanks

Counting the no. of commas across multiple fields using SQL

I've 3 fields which contain only text. However, i want to add a calculated field which counts the number of commas in each of these 3 fields and displays it separately in the adjacent column. The snippet of SQL i use is shown below. How can i build the calculated field?
SELECT week, client_I, client_II, client_III
FROM quality_control_test;
Please advise!
well, you can "count" the number of a given character in a string, by using this:
length(c) - length(replace(c,',',''))
I'm assume you can figure out how to leverage that for your own query ;)

Microsoft Access - SQL-generated field evaluated as Text instead of Single data type

I have a SQL statement (saved as "LocationSearch" in Access) that calculates distance between two points and returns the "Distance" as a generated field.
SELECT Int((3963*(Atn(-(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)))*10)/10 AS Distance, *
FROM Locations
ORDER BY (3963*(Atn(-(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)));
All the nasty math code you see is what calculates the distance (in miles) in the SQL statement using Latitude and Longitude coordinates.
However, the problem is that the Distance field that is generated by the SQL statement seems to be returned as a string. If I then add SQL code that asks for locations between a distance of 0 and 45 miles, it returns ANY Distance value that starts between "0" and "45". This includes a location with a distance of "1017" miles. Apparently, the Distance field is a text field, not a number field. So I can't use the "BETWEEN" statement. I also can't evaluate using "<" and ">" because it has the same problem.
I saved the SQL query above as a saved query called "LocationSearch". This way I can run secondary queries against it, like this:
SELECT * FROM LocationSearch WHERE Distance < #MaxDistance
Access will ask for the #lat, #long and #MaxDistance parameters, then the locations will be returned in a recordset, ordered by distance. However, the problem that occurs is when I enter a MaxDistance of 45. With a table containing locations on the West Coast of the US, and a #lat of 47 and a #long of -122 (near Seattle), Access returns the following:
Notice also that the "Distance" field is right-formatted so it appears to be a numeric field, yet for some reason the query returns a location in San Diego, which is 1,017 miles away. My guess is that it was evaluating the Distance field as a text field, and in an ASCII comparison, I believe that "1017" lies between "0" and "45".
One other thing: I'm using ASP 3.0 (classic) to access this query using JET OLEDB 4.0.
Anyone know how to define the Distance field as a number?
Thanks!
--- EDIT ---
Using HansUp's idea from his answer below, I tried this query to force Access to consider the Distance field as a Single precision number:
SELECT * FROM LocationSearch WHERE CSng(Distance) < #MaxDistance
Even this returned the exact same results as before which included the location in San Diego, 1017 miles away.
If you can't find a way to return numerical values instead of text from that Duration field expression, use your query as a subquery, then cast Duration in the containing query.
SELECT CSng(sub.Duration) AS Duration_as_single
FROM
(
-- your existing query --
) AS sub
WHERE CSng(sub.Duration) BETWEEN 0 AND 45
ORDER BY 1;
That approach also makes for a nicer ORDER BY ... if that counts for anything. :-)
I tried your query without the select *, and without the FROM and ORDER BY clauses.
I added in an extra column into the SELECT to prove that strings return as left-justified in access's grid.
SELECT Int((3963*(Atn(-(
Sin(LATITUDE/57.2958)*Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*
Cos([#lat]/57.2958)*Cos([#lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
Sin([#lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([#lat]/57.2958)*
Cos([#lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)))*10)/10 AS Distance,
'test' as test
I was prompted for four parameters, but in the end, I got back a two-column table:
Since the first column in right-justified, and the second (clearly a string) is left-justified, it appears that access is indeed returning it as a numeric for me. This was in Access 2010.
--EDIT--
I just created a new two-column table called Locations. It has a field id (autonumber) and a field Field1 (text). I ran the original query provided by OP and it works fine (distance is returned as a number).
This leads to wonder... Does the OP's Locations table have it's own Distance field, that is a string? Otherwise, the problem has got to be in the code calling the SQL statement, not in the statement or the jet engine itself.
Okay, solved it!
HansUp, your idea turned out to be the solution. I tried adding the CSng() function on the #MaxDistance parameter in the SQL query and that was what fixed it.
Here's the modified secondary SQL query:
SELECT * FROM LocationSearch WHERE CSng(Distance) < CSng(#MaxDistance)
Thanks for your help, everybody! You all rock.
Happy New Year.