I have a number that looks like this:
'0000040000'
How can I turn it into a string that looks like this:
400.00
This should work on numbers like this as well :
1234540067 -> 12345400.67
I suspect it's fair to say you have a string that you would like to format as a number.
If you want to "learn" how to do this, I suggest looking up the convert, cast and format functions for SQL Server and gain some extra knowledge.
I've elected to first convert to a numeric type, divide by 100 and format the output.
This saves the need to trim leading zero's.
select format(convert(numeric(18,2), '0000040001') / 100, '0.00'))
Following SQL expression will first change string to decimal and then convert it back to a string in a required format:
SELECT FORMAT(CAST ('0000040000' AS DECIMAL(12,2))/100,'#.00')
This should work:
select convert(varchar(10), convert(decimal(10, 2), try_convert(int, '0000040000') / 100.0))
Why do you want the value as a string? Does a decimal work for your purposes?
Another method is:
select ltrim(str(try_convert(int, '0000040000') / 100.0, 10, 2))
Or using just string manipulations:
select replace(ltrim(replace(stuff('0000040000', 9, 0, '.'), '0', ' ')), ' ', '0')
This would be more elegant if ltrim() in SQL Server accepted the character to trim (as most other databases allow).
Here are a some possible solutions:
DECLARE #x CHAR(10)='0000040000'
SELECT CONVERT(VARCHAR(11),CONVERT(NUMERIC(10,2),CONVERT(NUMERIC(10),#x)/100))
SELECT CONVERT(VARCHAR(11),CONVERT(NUMERIC(10,2),STUFF(#x,9,0,'.')))
Simple as this:
SELECT ROUND('0000040004', 2, 1)/100;
Tested this with SQL Server 2017. It does not care about the fact that the number is a string, it does math on it just fine. Result of the above is 400.04
Your query is more of mathematical one.
To get the last 2 numbers after decimals, get the remainder of the number by dividing it by 100.
To get the digits, leaving the last 2 digits, divide the number by 100 again.
select convert(varchar,1234540067/100)+'.'+ convert(varchar,1234540067% 100);
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.
I am using Oracle SQL Developer and I have a query in which one of the columns has a number value that I want to divide by 1,000,000 then display to show commas and up to 10 decimal places if the calculate value has any decimal places. I am currently using this query:
to_char(value/1000000, 'FM999,999,990.9999999999') as Millions_Value
The above format gives me commas and retains the number of decimal places I want, but it unfortunately adds a decimal point at the end of whole numbers as well.
Example:
value/1000000 = 33993
formatted value = 33,993.
what I want = 33,993
For a decimal value, I get what I want and need to keep the format
Example:
value/1000000 = 0.158739
formatted value = 0.158739
value/1000000 = 10.82
formatted value = 10.82
And ideas on how to get rid of the decimal for integers?
The easiest way is to use RTRIM, e.g.
RTRIM(to_char(value/1000000, 'FM999,999,990.9999999999'), '.') as Millions_Value
will remove any trailing periods.
EDIT
If you want to get fancy and internationalize it you need to query NLS_SESSION_PARAMETERS to get the decimal separator, use the G (group) and D (decimal) specifiers in the format string, and then trim off any trailing decimal separator characters. Or in other words:
WITH cteData AS (SELECT 123456789.123456789 AS N FROM DUAL UNION ALL
SELECT 123 FROM DUAL),
cteDecimalSeparator AS (SELECT SUBSTR(VALUE, 1, 1) AS DECIMAL_SEPARATOR
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_NUMERIC_CHARACTERS')
SELECT TO_CHAR(N, 'FM999G999G990D9999999999') AS OLD_FORMAT,
RTRIM(TO_CHAR(N, 'FM999G999G990D9999999999'), s.DECIMAL_SEPARATOR) AS NEW_FORMAT
FROM cteData d
CROSS JOIN cteDecimalSeparator s
which returns
OLD_FORMAT NEW_FORMAT
123,456,789.123456789 123,456,789.123456789
123. 123
Remember: there's no kill like overkill :-)
Personally, I think it looks good with one trailing 0, so converting your format string to have a 0 after the decimal. But to answer you question, I think you just need to trim it after you convert the number like this:
SELECT TRIM(TRAILING '.' FROM TO_CHAR('345676', 'FM999G999G990D9999999999'))
FROM DUAL;
I'm trying to convert a varchar column where decimal separator is ','.
The workaround found was replace comma to dot but SQL Server is automatically rounding it up. See below example:
2018-10-08 -8679.95 -8679,94711560794
select DATA, REPLACE([PL]*1,',','.') , PL from TB_BOOK
Please, anyone know How can a get the value with all decimals?
Your approach to replace , with . is ok.
However on top of it you need to explicitly CAST the string to a number with enough decimals (FLOAT, DECIMAL(p, s), ...).
SELECT DATA, CAST(REPLACE([PL],',','.') AS FLOAT), PL from TB_BOOK
How do we convert and add trailing and leading zeros to a number? For example 123.45. I need to make this ten digits long and have padding numbers in front and back. I would like to convert it to 0001234500. Two trailing numbers after the last digit of the decimal. Remove the decimal. Fill in the remaining space with zeroes for the leading end.
I have this so far and it adds trailing zeroes and removes the decimal.
REPLACE(RIGHT('0'+CAST(rtrim(convert(char(10),convert(decimal(10,4),Field))) AS VARCHAR(10)),10),'.','') as New_Field
In MySQL you would have RPAD and LPAD to get stuff like this done, in SQL Server (2012+) you can get something similar by working with FORMAT.
Easiest way is to FORMAT your numbers with a dot so that they take the right place in the format string, then remove that dot. You need to specify a locale, since in different regions you will get a different decimal sign (even if you use . within the format pattern, you would get , in various locales) - using en-US makes sure you get a dot.
REPLACE(FORMAT(somenumber, '000000.0000', 'en-US'), '.', '')
A few examples:
WITH TempTable(somenumber) AS (
SELECT 3
UNION SELECT 3.4
UNION SELECT 3.45
UNION SELECT 23.45
UNION SELECT 123.45
)
SELECT
somenumber,
REPLACE(FORMAT(somenumber, '000000.0000', 'en-US'), '.', '')
FROM
TempTable;
Gives
3.00 0000030000
3.40 0000034000
3.45 0000034500
23.45 0000234500
123.45 0001234500
You seem to really be overthinking what you need to do here. If we take it in steps, perhaps you'll see that this can be achieved much more easily. This solution runs under the idea that the value 123.45 becomes 0001234500 and 6.5 becomes 0000065000.
Firstly, let's pad out the right hand side of the number 123.45 so that we have 1234500 That's easy enough : 123. 45 * 100 = 12345 So, to get 1234500 we simply need to multiple it by a couple of extra factors of 10:
SELECT 123.45 * 10000; --1234500.00
Ok, now, let's get rid of those decimal places. Easiest way, convert it to an int:
SELECT CONVERT(int, 123.45 * 10000); --1234500
Nice! Now, the finalstep, the leading 0's. A numerical value, in SQL Server, won't display leading zeros. SELECT 01, 001.00; Will return 1 and 1.00 respectively. A varchar however, will though (as it's not a number). We can, therefore, make use of that with a further conversion, and then then use of RIGHT:
SELECT RIGHT('0000000000' + CONVERT(varchar(10),CONVERT(int,123.45 * 10000)),10);
Now you have the value you want '0001234500'.
If you're only after padding, (so 6.5 becomes 0006500) then you should be able to work out how to achieve this with the help above (hint you don't need RIGHT).
Any questions, please do ask.
the zip is an integer datatype. so far i have
select substring('00000'||cast(zip as char(5)), character_length(cast(zip as char(5))),5)
It's not entirely clear what you are asking, but my guess from looking at your attempt is that you are trying to prefix a string with zeros to make it a common length.
For instance, if you have the following numbers: 123, 1564, 12413 and you would like them all to be 10 characters with repeated 0's prefixed like 0000000123, 0000001564, 0000012413, you will need something like:
SELECT substring('0000000000' FROM 1 FOR 10 - LENGTH(<yourField>)) || <yourField>
Apply a FORMAT to the zip:
TRIM(CAST(zip AS FORMAT '9(5)'))
or use LPAD in TD14:
LPAD(TRIM(zip), 5, '0')