How to extract numbers appearing after decimal from a money format column in tsql? - tsql

I have an amount column which is in money format. I have tried using parsename, converting it to varchar to use substring function but unable to extract exact values appearing after decimal. Attaching the screenshot for reference.
select home_currency_amount,
cast(home_currency_amount as varchar(50)) as amt_varchar,
parsename(home_currency_amount, 1) as amt_prsnm
from #temptbl;
---Below is the output:
home_currency_amount amt_varchar amt_prsnm
39396.855 39396.86 86
1112.465 1112.47 47
5635.1824 5635.18 18
E.g. if value is 39396.855, desired output would be 855.
Thanks in advance for the help.

First perform the mod operation on value with 1 to get the decimal part. We will be getting as '0.decimal_part' as we require only decimal part without '0.' so we are replacing it and finally casting as integer. Hope it helps in your case..
select cast(replace(cast( 1.23 % 1 as varchar),'0.','') as int)
enter image description here

Related

Convert string field to decimal in Altova Mapforce

I have a column with value in string format i.e 00000036, i want to convert it to decimal i.e it should show 36.000000 in the output or let say if i have 00000006 then it should show 6.0000000, The value always needs to be 8 characters
Please let me know how can i achieve this. I used decimal function but it did not help me get the desired result
try format-number and pad-string-right:

Format postgres numeric like money ($0.20)

I have a numeric column that I'm trying to format like currency, but I can't seem to get the format right. I currently have:
to_char(my_column, 'fml9999999999999999999D9999999999999999999')
but it outputs
$.2
If I remove the 'fm' modifier, it outputs:
$ .2000000000000000000
How would I go about getting it to preserve at least 1 digit on the left, and at least 2 digits on the right while removing all the rest of the trailing 0's?
Figured it out: the trick is to use 0's where you want it to preserve the digits:
to_char(my_column, 'fm9999999999999990D00')

Get substring form an integer column

I had a quick question, how can I go about using SUBSTRING on an integer? I currently have field labeled "StoreID" that contains a 5 digit integer (60008). I am trying to use SUBSTRING to remove the 6 when I query out this information. When I use something like:
SUBSTRING('StoreID', 2, 6)
I get an error returning back saying that SUBSTRING(integer,integer,integer) does not exist.
Is there another function I can use in postgres to accomplish what I am trying to do?
You can cast the integer
SUBSTR(cast (StoreId as text), 2,6)
If you are interested in the number 8, use the modulo operator %
SELECT 60008%10000
If you want the string '0008', the function right() is right for you (added with Postgres 9.1):
SELECT right(60008::text, -1)
Or with modulo and to_char():
SELECT to_char(60008%10000, 'FM0000')
The FM modifier removes space padding.

Crystal report issue with int to string conversion

I want to convert int to string and then concatenate dot with it. Here is the formula
totext({#SrNo})+ "."
It works perfectly but not what i want. I want to show at as
1.
but it shows me in this way
1.00.
it means that when i try to convert int to string it convert it into number with precision of two decimal zeros. Can someone tell me how can i show it in proper format. For information i want to tell you that SrNo is running total.
ToText(x, y, z, w) Function can use
x=The number to convert to text
y=The number of decimal places to include in result (optional). The value will be rounded to that decimal place.
z=The character to use as the thousands separator. If you don’t specify one, it will use your application default. (Optional.)
w=The character to use as the decimal separator. If you don’t specify one, it will use your application default. (Optional.)
Examples
ToText(12345.678) = > “12345.678″
ToText(12345.678,2) = > “12345.67″
ToText(12345.678,0) = > “12345″
You can try this :
totext({fieldname},0)
Ohhh I got the answer it was so simple.
totext takes 4 parameters
First parameter is value which is going to be converted
Second parameter is number of decimal previsions.
Third parameter is decimal separator. like (1,432.123) here dot(.) is third parameter.
Forth parameter is thousand separator. like (1,432) here comma(,) is forth parameter.
Example{
totext("1,432.1234",2) results 1,432.12
totext("1,432.1234",2,' " ') results 1,432"1234
totext("1,432.1234",2,' " ', ' : ') results 1:432,1234
}
Although i think this example may be not so good but i just want to give you an idea. This is for int conversion for date it has 2 parameters.
value to be converted and format of date.

Postgresql, treat text as numbers for getting MAX function result

Still didnt fix issue with dates written as strings here comes another problem.
I have text column where only numbers as writen (like text).
By using function MAX I get incorrect result because there 9 is bigger than 30.
Is here any inline function like VAL or CINT or something that I can compare and use textual data (only numbers) like numbers in queries like SELECT, MAX and other similar?
How than can look like in following examples:
mCmd = New OdbcCommand("SELECT MAX(myTextColumn) FROM " & myTable, mCon)
You need to use max(to_number(myTextColumn, '999999'))
More details are in the manual: http://www.postgresql.org/docs/current/static/functions-formatting.html
If all "numbers" are integers, you can also use the cast operator: max(myTextColumn::int)
If your text values are properly formatted you can simply cast them to double, e.g.: '3.14'::numeric.
If the text is not formatted according to the language settings you need to use to_number() with a format mask containing the decimal separator: to_number('3.14', '9.99')
To get the MAX works poterly you need to first convert your text field in numeric format
mCmd = New OdbcCommand("SELECT MAX(TO_NUMBER(myTextColumn, '99999')) FROM " & myTable, mCon)