Oracle PL/SQL number to_char ignores 0's even when decimal part exist [duplicate] - sql

This question already has an answer here:
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
Im using the following code to convert a number(38,2) to the format "XXX.XXX.XXX,XX":
TO_CHAR(number, '999G999G999G999G999G999G999D99')
Although, when the number is 0 or 0.XX the 0 is eaten up :
Input: 0
Result: ",00"
Expected Result: "0,00"
Input: 0.23
Result: ",23"
Expected Result: "0,23"
Why does this happen, is there a way of solving this without manipulating the result string ?

Use 0 instead of 9
TO_CHAR(0.00, '999G999G999G999G999G999G990D99')
0 ensures that if there is no digit in that place it'll display 0.

Related

Split a vector into parts separated by zeros and cumulatively sum the elements in each part

I want to split a vector into several parts separated by the numeric value 0. For each part, cumulatively calculate the sum of the elements encountered so far. Negative numbers do not participate in the calculation.
For example, with the input [0,1,1,1,0,1,-1,1,1], I expect the result to be [0,1,2,3,0,1,1,2,3].
How to implement this in DolphinDB?
Use the DolphinDB built-in function cumPositiveStreak(X). Treat the negative elements in X as NULL values.
Script:
a = 1 2 -1 0 1 2 3
cumPositiveStreak(iif(a<0,NULL,a))
Execution result:
1 3 3 0 1 3 6

SQL syntax - division and ceiling functions [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 2 years ago.
I am trying to calculate how many packs of sweats a kid needs based on the amount of individual sweets required. I am trying to figure out the SQL syntax to do this.
I have used ceiling with divide but still not working
select CEILING(NoOfSweets / SweetsPerPack) AS NoOfPacks
Scenarios:
NoOfSweets SweetsPerPack RequiredOutCome NoOfPacks
--------------------------------------------------------
10 10 1 1
5 10 1 0
20 10 2 2
8 10 1 0
7 5 2 1
If the values are integers, then SQL Server does integer division. So, 1/2 is 0 rather than 0.5. I find that the simplest way to get a number with decimal points is to multiply by 1.0:
CEILING(NoOfSweets * 1.0 / SweetsPerPack)

Dataframe countif function issue [duplicate]

This question already has an answer here:
counting the amount of True/False values in a pandas row
(1 answer)
Closed 5 years ago.
Return
0.0000
-0.0116
0.0000
0.0100
I have a dataframe of the format above and I am trying to count >0 and <0 with the following code
print ("Positive Returns:")
print((df['Return']>0.0).count())
print ("Negative Returns:")
print((df['Return']<0.0).count())
However both return 5119 which is my whole dataframe length
It is not counting correctly.. can anyone advise please?
Thankyou
*not really a duplicate since I am not asking for true/false value it can be >0.1 for example
Use sum for count boolean Trues which are processed like 1s:
print((df['Return']>0.0).sum())
print((df['Return']<0.0).sum())

Convert String into digit in SQL [duplicate]

This question already has answers here:
Converting words to numbers in PHP
(6 answers)
Closed 8 years ago.
How to convert any string into digit in SQL without CASE and Decode function.
eg. THREE to 3
FOUR to 4
FIVE to 5
SIX to 6
Range is not decided.. can be vary upto N.
Well, I'm not sure whether this is what you need, but what about defining a table, say digits, like this:
digit: text | value: int
------------+-----------
one | 1
two | 2
three | 3
etc.
Then use a query, for example, like this one:
SELECT value FROM digits WHERE digit = 'FIVE'
Sure, it's pretty weird (to say the least), but nonetheless the use of CASE is avoided.

How to display the numeric numbers

Here's the content of my DataGrid
id
1
2
3A
4
5
6A
..
...
10V1
I want to get the max number from the datagrid. Then, I want to
display the next number (In this case: 11) in the textbox beside the grid
Expected Output
id
1
2
3A
4
5
6A
..
...
10V1
11
I tried the following code:
textbox1.text = gridList.Rows(gridlist.RowCount() - 1).Cells(1).Value + 1
It works if the previous row values is entirely numeric. However, if the value is alpahnumeric, I am getting the following error:
Conversion from string "10V1" to type 'Double' is not valid.
Can someone help me solve this problem? I am looking for a solution in VB.Net
You may want to look into Regex to do that (based on what I understand from your question)
Here's a related question on this.
Regex.Match will return the part of the string that will match the expression... In your case, you want the first number in your string (Try "^\d+" as your expression, it will find any serie of numbers at the beginning of your string). You can then convert the result string into an int and add 1 to it.
Hope this helps!
Edit: Here's more info on regex expressions.