I'm looking to use SQL to format a number with commas in the thousands, but no decimal (so can't use Money) - any suggestions?
I'm using SQL Server 2005, but feel free to answer for others as well (like MySQL)
With TSQL you could cast to money and convert it will add the .00, but you could use replace or substring to remove.
replace(convert(varchar, cast(column as money), 1), '.00', '')
In SQL 2005 you could use a CLR function as well
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString FormatNumber(SqlInt32 number)
{
return number.Value.ToString("N0");
}
and call it as any other user-defined function
SELECT dbo.FormatNumber(value)
In MySQL, the FORMAT() function will do the trick.
In Oracle you can specify a format parameter to the to_char function:
TO_CHAR(1234, '9,999') --> 1,234
Any specific reason you want this done on the server side? Seems like it is a task better suited for the client/report.
Otherwise, you are storing a number as a string just so you can keep the formatting how you want it -- but you just lost the ability to do even basic arithmetic on it without having to reconvert it to a number.
If you're really determined to do it in SQL and have a justifiable reason for it, I guess my vote is on Scott's method: Value --> Money --> Varchar --> Trim off the decimal portion
-- Kevin Fairchild
For SQL Server, you could format the number as money and then delete the right-most three characters.
replace(convert (varchar, convert (money, 109999), 1), '.00','')
Related
I'm importing data to a 3rd party CRM and the restrictions on the phone number field are tighter than those of the source database (MySQL). I would like to grab the first 10 numeric characters in the select. I don't have flexibility to sanitize the existing data. I can only work with what is there. All I can find in forums so far seems to be creating a custom function. Is a custom function the only way? The environment is MySQL 5.7.19.
Thanks in advance!
In MySQL 5.7, here's how I'd do it:
SELECT LEFT(mycolumn, 10)+0 FROM ...
The builtin function LEFT() uses only the first N characters.
The +0 makes sure to cast it to a number, just in case the first N characters include some non-numeric characters. Casting to a numeric in MySQL just uses the leftmost number characters, and when it reaches a non-numeric character, ignores the rest.
You can get rid of hyphens first, then use the result of that as I showed above:
SELECT LEFT(REPLACE(mycolumn, '-', ''), 10)+0 FROM ...
In a MySQL database, I have a string like 123456789. I want to add a decimal separator between every 3 digits, and thus turn it into 123.456.789.
How can I do this?
If you are using MySQL 8.0+, it is possible to use the REGEXP_REPLACE function:
SELECT REGEXP_REPLACE(YOUR_COLUMN, '([:digit:]{3})(?!$)', '$0.');
It adds a dot for each group of 3 digits, excepted for the last one.
Here are some examples:
123456789 returns 123.456.789
1234567891 returns 123.456.789.1
12345678910 returns 123.456.789.12
You can try it out on this DB Fiddle and you can change the regular expression to fit what you need.
However, this approach is probably not the best in terms of performance. If your query is called a lot of times, your database server will suffer since calculation is centralized on it. As #xNoJustice said, it is better to handle this string operation in the client part, where it will be divided between every client execution.
Use FORMAT() function with suitable locale.
fiddle, which shows all suitable locales.
On a mySQL database, in a dataset all values are stored as varchar, show amounts of Euro and look like this: 'EUR 2.5E+2', 'EUR 56.95', 'EUR 1E+2'
Now, I want to make it useful again and convert this into decimal for further calculation, but can't figure out how to do it.
I have tried to follow the suggested solution to this question:
Casting Scientific Notation (from varchar -> numeric) in a view
However, I failed to replicate the solution as a query, view or stored procedure, much less adapt it to my problem.
I really hope someone can help me out here.
Thanks in advance!
Thank you very much Rick James, your solution works quite well.
However, I prefer to explicitly cast the substring as decimal:
CAST(SUBSTRING_INDEX(p.amount, ' ', -1) AS DECIMAL(5,2))
Now I can carry out calculations on the result (see comment below Rick's answer).
SELECT SUBSTRING_INDEX('EUR 2.5E+2', ' ', -1) + 0;
->
250
Caution: This assumes there is one space, and the numeric value is after the space.
This is really simple. Does SQL SERVER 2008 auto convert values to string for you ?
I tried this Select * from Staff Where Division = 5
If I try to insert it, it works too.
If I change 5 for five I get and error though. Invalid column name five.
Division is a NVARCHAR. Shoudn't the 5 be within single quotes ?
Yes, SQL Server does an implicit conversion of int to nvarchar. It can also implicitly convert nvarchar to int (and other numeric types).
For more details: http://msdn.microsoft.com/en-us/library/ms191530.aspx
There are a lot of cases where implicit conversion takes place. For example:
EXEC sp_who2 active;
Shouldn't you need to delimit active as a string? Yes, but the syntax is forgiving.
My suggestion: always delimit data types correctly, and ignore the exceptions. The chances they'll ever get comprehensively fixed are close to nil.
Is it possible to change the decimal comma from "." (dot) to other character (comma) in MySQL output? I don't want to use functions like FORMAT, I just want to use all the queries I normaly use without any modification. I'm looking for some setting (of some variable, locale etc.). I tried to search the manual but without success.
Tip for CSV exports:
SELECT REPLACE(CAST(prijs_incl AS CHAR), '.', ',') will give you input that can be used as numeric fields in european excelsheets.
No, you can't. That's the SQL standard and MySQL complies with it (in that point at least).
The problem is not really with output (as you mention, there are various FORMAT functions in most DBMSs) but with INSERT. If you could use comma , for example as decimal point (that's common in other locales) which is aslo used as values separator, Inserts would become ambiguous. See my answer in the question: insert-non-english-decimal-points-in-mysql