How to display the value for a measure in thousands.
I would assume to do it as follows:
FORMAT_STRING="#,,"
But the data returns: 10000000.001
while I would expect: 10,000
The format follows the format as described in the documentation of FORMAT_STRING :
FORMAT_STRING="#,###" or "#,##0" or "#,##0.0"
when defining the format you should not use your locale, but use the ',' separator for the thousands and the '.' for the decimal.
Separators will be automatically converted to your locale language in the output.
hope it helps
Related
I need this type of decimal number format because what I show refers to money. SP SQL Server 2019
I need to transform from this $25,000.00 a $25.000,00.
Someone could help me?
REPLACE(REPLACE(CONVERT(varchar,CAST(#MontoFinal AS money),1),'.',','),',','.') = 117.692.05
Formatting is usually best handled by the UI, but sometimes you have to do it in the database for various reasons. Sometimes you have to live with mandates, and if you have to do it in the database, it can be done.
Looking at your desired output, you want periods as separators and a comma as a decimal point, and you would also like a dollar sign. Luckily for you, this is how Argentina formats its currency, so you can just use the built-in FORMAT function. The culture string for Argentina is es-AR, so you can use that along with FORMAT:
SELECT FORMAT(123456789, 'C', 'es-AR') AS [Example];
This yields:
Example
$ 123.456.789,00
If your data is a number, you are done. If it is stored as text like in your example, you can CAST it as you were doing:
SELECT FORMAT(CAST('$25,000.00' AS MONEY), 'C', 'es-AR') AS [Example];
Which gives your desired output:
Example
$ 25.000,00
As a final note, the FORMAT function in this scenario puts a space between the dollar sign and the leading number - if you don't want the space, you can remove it:
SELECT REPLACE(FORMAT(CAST('$25,000.00' AS MONEY), 'C', 'es-AR'), ' ', '') AS [Example];
And that gives you:
Example
$25.000,00
There are other ways to get a similar result, but this is probably the easiest (I said easiest, I did not say anything about performance).
If your data is actually text and already has the correct commas and decimal points along with the dollar sign, you could use REPLACE - first replace . with something (I used _), then REPLACE the , with ., and finally REPLACE the _ with . :
SELECT REPLACE(REPLACE(REPLACE('$25,000.00', '.', '_'), ',', '.'), '_', ',') AS [Example];
And again it gives your desired output:
Example
$25.000,00
If your data is not in a consistent format to start with correct commas and decimal points, the FORMAT approach will probably work better for you.
The below query is giving me out put in correct format
select to_char(999.00,'9,99.00') from dual
output = 9,99.00
what if I don't have a specific size to the number?
999999999999999999999999999999999.00
should be converted as
9999999999999999999999999999999,99.00
Will it be possible to do that?
Is there any formats for that ?
use the FM prefix in your formatting string. It trims leading spaces from the result of the conversion:
select ''''||to_char(123456.00, 'FM999999999999999999,99.00')||''''
, ''''||to_char(123456.00, '999999999999999999,99.00')||''''
from dual
;
produces
'1234,56.00'
' 1234,56.00'
Note:
You have to specify as many digits as the largest number to be converted will have in its shortest representation matching the format. Turning a complicated phrasing into a simple example, to_char(123456.00, 'FM9,99.00') won't work.
I am using ISNUMERIC to get all non numeric rows in my table - but all I get in return is the following example 1.437.230,61 or 3.511.980,00. I really dont know how to get these few rows converted to numeric! I have conveted about 2,5 mil rows without problem but I am getting about 9000 rows that are not numeric - but as displayed above they are numbers. I have tried to trim my coloumn with no luck!
You should always name the dbms you are using. Many dbms have problems converting proper numbers such as 1.437.230,61 due to the thousand separators. So isnumeric works fine here, but the conversion function doesn't.
Use a string replace function to remove the thousand separators from the string before converting. Such as:
to_number( replace(numstr, '.', '') )
I have the following query:
SELECT
CONVERT(DECIMAL(11,1),SUM(Column/1000*-1)) AS NAME,
FROM
Table
The reason i have "/1000*-1" is that I would like the results to be displayed in units of thousands and inverted (negative values as positive and vice versa) with only one decimal place.
How can I get the positive values have a plus sign (+) in front of them just like the negative values have a dash sign (-) ?
You can use semicolon-separated multi-part strings with the FORMAT function (kind of like you would with custom number formats in Microsoft Excel).
A number format can have up to three sections of formatting code, separated by semicolons. These code sections define the format for positive numbers, negative numbers, and zero values, in that order:
<POSITIVE>;<NEGATIVE>;<ZERO>
example:
FORMAT(#YourValue,'+0.0;-0.0')
(Adapted from this)
I usually also hide zeros when displaying +/- signs so I use formatting string: '+0;-0;'''
SELECT FORMAT(+5,'+0;-0;''') --returns: +5
SELECT FORMAT(-5,'+0;-0;''') --returns: -5
SELECT FORMAT(-5,'+0;-0;''') --returns: <empty string>
To display zero's as well you could use formatting string: '+0;-0;0'
Applies to: tsql, azure-sql-database, sql-server-2012, sql-server-2014, sql-server-2016
More Information:
Microsoft Docs : FORMAT (Transact-SQL)
Microsoft Docs : Formatting Types
(Doc is for .net but also applies to the FORMAT function)
Microsoft Docs : SIGN (Transact-SQL)
SELECT
case
when CONVERT(DECIMAL(11,1),SUM(Column/1000*-1)) >= 0
then concat('+', CONVERT(DECIMAL(11,1),SUM(Column/1000*-1)))
else CONVERT(DECIMAL(11,1),SUM(Column/1000*-1))
end AS NAME
FROM Table
In MYSQL, this works ...
CONCAT(IF(TimeZone>0,'+',''),TimeZone)
SELECT
REPLACE(CONCAT('+', CONVERT(DECIMAL(11,1),SUM(Column/1000*-1))), '+-', '-')
FROM
Table
This approach will show +0.0, though.
sp_helpdb returns strings like '50000.255 MB' in the db_size column.
These strings are culture-dependent; the above string will mean 2 different things in US and Germany (in the latter, the dot char is used as a group separator, similar to the comma in US).
Is there another method which returns a numeric value, culture-independent?
Use YourDB;
SELECT SUM(Size / 128.0) As FileSize from sys.database_files;
This returns the size in MB as a numeric, you should be able to do what you like with it from there.
Note: size returns the number of 8KB pages in a given database file.
http://msdn.microsoft.com/en-us/library/ms174397.aspx
I do not think it is possible. The SP sp_helpdb uses str to convert the numeric size to varchar and there is nothing in the documentation (that I can find) that can make str use , instead of . as decimal symbol. Using set language does not help.
Workaround as suggested by Martin in comment
select replace(str(sum(convert(dec(17,2),size)) / 128,10,2) +' MB', '.', ',')
from sys.database_files