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)
Related
I am looking to put a comma after two digits from right. For example i have 250$ and in SQL I want the select to return 2,50$. Example2: 500$ should be 5.00$. For 1000$. It should be 10,00$. How can it be done in select. I have a column that holds those value in my SQL database.
Thanks you in advance for your help.
My column data type is bigint
Ironically, you can do this the same way in almost any database:
select cast( (col / 100.0) as decimal(20, 2))
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
I have a column loaded into SQL Server with these nvarchar values:
ColumnName
==========
6.19e+014
.....
6.19e+014
Now, what would be the easiest way to convert this value into numbers again.
Kindly suggest. Thanks!
declare #String varchar(25)='6.19e+014'
Select cast(#String as float)
Returns 619000000000000
This will cast to FLOAT without issue:
SELECT CAST('6.19e+014' AS FLOAT)
If what you really want is the specific value that got converted to scientific notation in the first place, you'll have to go back in the process to before it got converted and fix that.
Also, try this
Select convert(numeric(15,0),ltrim(rtrim(str(column_name,15))))
from table_name
I have a column called TotalArea and its format is numeric (12,2).
I want it to display the numbers with a thousand separator so when I
select TotalArea from table
to show me a format like 1,234.00.
How could I do that? Thanks!
SELECT FORMAT(12345,'#,0.00');
SELECT FORMAT(TotalArea,'#,0.00') from table;
Reference:
https://msdn.microsoft.com/en-us/library/ee634206(v=sql.105).aspx
Try this way:
SELECT REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, TotalArea), 1), '.00', '')
FROM table
or
SELECT CAST(CONVERT(VARCHAR, CAST(123456 AS MONEY), 1) AS VARCHAR)
FROM table
Formatting numbers for display is something that should be done in the display layer, and not within the database. So, in whatever application this data ends up being used, you should format it there. Management Studio, unfortunately, does not offer much control in this regard.
I know the question is for sql server 2008 but if you have sql server 2012+, you can use format like so:
SELECT FORMAT(12345.5634, 2);
Reference: https://www.w3schools.com/sql/func_mysql_format.asp
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.