How to convert number to dollar format - sql

I wrote sql to convert number to dollar format by referring other stackoverflow questions.
select decimal(sum_price, 9, 2) from order_tbl
The data type of column sum_price is Decimal(10,0).
if number in column is 7050, this SQL return 7084.00, but what I want is
70.84.
what should I do?

You can't convert in implicit way this.
If you have stored your field in decimal(10,0) you tell you have only int part stored, so you must apply a math conversion as follow:
SELECT sum_price * 0,01 from order_tbl
But... If you have stored a number as follow: 7050 where you indicate 7,050 (US separator for thousand), how query understand this situation?
I strongly advice to change your field definition

try this
Select Str(float_field, 25, 5)
This is the link for your refrence.
https://msdn.microsoft.com/en-us/library/ms189527.aspx

Related

Whole number to comma separated price - SQL Server & SSMS

I couldn't find any answer to my question and I hope you can help me out. I've been asked to create a revenue report, however, I'm dealing with an old poorly optimized database. It sounds like a simple thing but I cannot find any solution to the below issue.
Basically, some prices are being shown in the following format:
22400
What I need is to show these values in this format format: 22,40
Any suggestions? Thanks in advance.
You can use format function :
select format(prices, '0,000')
from table t;
Try
convert(decimal(38, 2), convert(decimal, 22400) / 1000);
to convert it to a decimal(38, 2). Possibly lower the precision (38) if you don't need that much.
First you have to convert this price(ex: 22400) from one datatype to 'money' datatype using CAST function.
and then CONVERT money to character
references -
1) https://www.techonthenet.com/sql_server/functions/convert.php
2) https://www.techonthenet.com/sql_server/functions/cast.php
Syntax :
CONVERT(nvarchar, CAST(price AS money), 1)

Converting varchar type variable number without having decimal points

I need to convert varchar2 type value 100.0 into 100 (without decimal points) in Oracle SQL. Can you please help me?I used regexp_substr...but it fails in one situation given below.
SELECT REGEXP_SUBSTR(0.1,'(\d*)') FROM DUAL;---it results in Null but i want zero(0).
Note:- in Orcale sql developer
You want to display number in format You wish it to be displayed, so You should use formating functions. No need for rounding here in my opinion - query below should give You what You want.
SELECT
TO_CHAR(TO_NUMBER('100.1'),'FM9999') as res
FROM
dual
;
More on number formats can be found here.
Try to this....
SELECT TRUNC(TO_NUMBER(100.5)) FROM DUAL;

SQL - Convert number to decimal

I'm trying to convert a number to a decimal with two decimals places.
SELECT CONVERT(DECIMAL(10,2),12345)
The above would return 12345.00 but I'm trying to achieve 123.45
You need something like that:
SELECT CONVERT(DECIMAL(15,2),12345/100.0)
SELECT CONVERT(DECIMAL(10,2),CAST(12345 as float)/CAST(100 as float))
Correction: The premise is somewhat flawed, as the data type of a literal number without a decimal point is int, not numeric as implied by the question. In that case, you do need to convert the initial value to either numeric or decimal before dividing:
SELECT CONVERT(DECIMAL,12345)/100
or
SELECT CAST(12345 AS DECIMAL)/100
(cast is the SQL standard, so if you ever want to apply this to other databases, it would be the preferred method.)
Alternately, you can just add a decimal point to the divisor, as SQL server will return the more precise data type when doing arithmetic on heterogeneous types:
SELECT 12345/100.0
According to the documentation, the numeric data type is functionally equivalent to the decimal datatype, so there's really no reason to convert between the two. It seems that all you really want to do is divide the value you have by 100:
SELECT 12345/100

Insert only Month and Year date to SQL table

I am using MS SQLServer and trying to insert a month/year combination to a table like this:
INSERT INTO MyTable VALUES (1111, 'item_name', '9/1998')
apparently, the above command cannot work since
Conversion failed when converting date and/or time from character string.
Because 9/1998 is a bad format. I want to fix this and this column of the table will show something like:
9/1998
12/1998
(other records with month/year format)
...
Can someone help me with this?
thank you
SQL Server only supports full dates (day, month, and year) or datetimes, as you can see over on the MSDN data type list: http://msdn.microsoft.com/en-us/library/ff848733(v=sql.105).aspx
You can use a date value with a bogus day or store the value as a string, but there's no native type that just stores month/year pairs.
I see this is an old post but my recent tests confirm that storing Date or splitting the year and month to two columns (year smallint, month tinyint) results in the overall same size.
The difference will be visible when you actually need to parse the date to the filter you need (year/month).
Let me know what do you think of this solution! :)
Kind regards
You can just use "01" for the day:
INSERT INTO MyTable VALUES (1111, 'item_name', '19980901')
You can:
1) Change the column type to varchar
2) Take the supplied value and convert it to a proper format that sql server will accept before inserting, and format it back to 'M/YYYY' format when you pull the data: SELECT MONTH([myDate]) + '/' + YEAR([myDate]) ...
You may want to consider what use you will have for your data. At the moment, you're only concerned with capturing and displaying the data. However, going forward, you may need to perform date calculations on it (ie, compare the difference between two records). Because of this and also since you're about two-thirds of the way there, you might as well convert this field to a Date type. Your presentation layer can then be delegated with the task of displaying it appropriately as "MM/yyyy", a function which is available to just about any programming language or reporting platform you may be using.
if you want use date type, you should format value:
declare #a date
SELECT #a='2000-01-01'
select RIGHT( convert (varchar , #a, 103), 7) AS 'mm/yyyy'
if you want make query like SELECT * FROM...
you should use varchar instead date type.

format decimals and comma to numbers retrieved

I have a column in my table which showing an amount. The amount is varying from one column to another and they are more than 15 digits.
What is the best way to format the number to show commas and decimal points?
My query is
select amount from ccamounts
How can I format the number
205511892078
to show as
205,511,892,078
and if there is a radix point it will also appear.
I believe you can use TO_CHAR to do this, the issue is that this is just a formatting function within SQL. It requires that your number is always going to be in the same format.
taking the example above you could do
TO_CHAR('205511892078', '999,999,999,999')
and this would format the number as you have specified, with a decimal place this can be done aswell but the decimal needs to be specified:
TO_CHAR('20551189207842', '999,999,999,999.99')
which would give you 205,511,892,078.42
I think if the field length is going to vary sql will just ignore anything that doesn't fit into the format string (It's a mask). Perhaps you want to consider formatting the number in this case on whichever front end you may be using?
I would format the number in the UI / Reporting tool / Presentation layer not Oracle
but if you MUST format it in oracle try:
SELECT
CASE WHEN INSTR( TO_CHAR(205511892078),'.')>0 THEN
TO_CHAR(205511892078 ,'999,999,999,999.99')
ELSE
TO_CHAR(205511892078 ,'999,999,999,999')
END
FROM DUAL
this will return the number as a string.
declare #d3 decimal (10, 2)
set #d3 = 12309809.5494
SELECT convert(varchar(15),cast(CAST(ROUND(#d3,2,1) AS DECIMAL (30,2)) as money),1) as Value
SELECT CAST(ROUND(convert(varchar(30), cast(#d3 as money),2),2,1) AS DECIMAL (30,2)) as Value
Output:
12,309,809.55
12309809.55