I have been using SQL Server 2005, and I need to store some columns in database in Money format.
But SQL Server stores them like this --> 0000.0000, but I only want 2 digits after the decimal point ---> 0000.00
Do I have to control this in the program or there is a way to change this format in SQL Server 2005 ?
Thanks in advance
I would normally do these formatting stuffs in the front end. But if you still have to do it within the DB layer itself then make use of ROUND function.
SELECT CAST(ROUND(1234.1234, 2) AS MONEY)
Would also suggest you to check out the Performance storage comparison between Money & Decimal post written by Aaron Bertrand: https://sqlblog.org/2008/04/27/performance-storage-comparisons-money-vs-decimal
Money Vs Decimal Vs Float Decision Flowchart (Extract from SQLCAT.com):
Source: http://sqlcat.com/sqlcat/b/technicalnotes/archive/2008/09/25/the-many-benefits-of-money-data-type.aspx
Use Round() function. Returns a numeric value, rounded to the specified length or precision.
More here.
Round(value,2,1)
Second parameter specifies precision to be two decimal places.
Third parameter with value one specifies truncate rather than round off.
try this it works for SQL Server 2008 and below (2012 have already a FORMAT() function that you can use)
this will only works for data type Money and SmallMoney
declare #v money -- or smallmoney
set #v = 1000.0123
select convert(varchar(25), #v, 0)
select convert(varchar(25), #v, 1)
select convert(varchar(25), #v, 2)
select convert(varchar(25), #v, 126)
select '$' + convert(varchar(25), #v, 0)
select '$' + convert(varchar(25), #v, 1)
select '$' + convert(varchar(25), #v, 2)
select '$' + convert(varchar(25), #v, 126)
Hope this help!
Related
SQL Server 2012: converting a float to varchar is removing decimal places.
For example:
select convert(varchar, cast(834.7833 as float))
returns a varchar with 834.783.
How do I convert a float to varchar without loss of decimal places?
This is a similar question to this unanswered question:
How does convert(varchar, float) decide how many decimal places to keep?
Why do you need to do a CAST(... AS FLOAT) at all??
Try these snippets - they return the full number of decimal points:
SELECT
CONVERT(VARCHAR(20), 834.7833)
SELECT
CAST(834.7833 AS VARCHAR(20))
Both of these output this result:
834.7833
I have a column with HHMMSS in a table.
Similar to this out put:
SELECT convert(varchar, getdate(), 108)
I need to be able to make a view in MSSMS with only the HH from time and convert it as an int.
Example
Time
11:08:11,
12:08:12
Int
11
,12
I have tried several tricks from this page but could not find any work around
https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
Why not just use DATEPART? There's no need to convert the value from a datetime to a varchar and then to an int.
SELECT DATEPART(HOUR, GETDATE());
Edit: on a different note, I strongly suggest you always ensure you declare your length, precision and scale when using a datatype. SELECT CONVERT(varchar, GETDATE(), 108) will return the whole value here, as (in this case) this it converts the value to a varchar(25), however, not declaring these parameters can/does leads to unexpected behaviour.
I've seen many questions where people have asked "Why isn't this working?" because their SP is declared as CREATE PROC MyProc #String varchar AS ..., or they have a variable declaration of DECLARE #MyVar nvarchar;, which in both cases means the length has a value of 1; and thus their values are truncated.
Edit for Irony: And no less than an hour later... Substring function not comparing the output; looks like I need to order a new crystal ball. Exactly why not declaring your Length, Precision or Scale is a bad idea.
One way could be this:
SELECT LEFT(convert(varchar, getdate(), 108),2)
You can use stuff() & do conversations :
select col, datepart(hh, cast(stuff(stuff(col, 3, 0, ':'), 6, 0, ':') as time(0)))
from table t;
This assumes col has varchar type time.
For presentations sake I have a value that I want to show to only two decimal places before converting to varchar
select '£' + cast(round(amount,2) as varchar (10))
This works fine and would display the result as, say, £300.00. However the 'amount' also needs to be divided by 100 as part of the query. When I add that in to the code...
select '£' + cast(round(amount/100,2) as varchar (10))
..it displays as £3.000000. Is there any way to remove the extra 0s so only two are shown after the decimal point?
Just use format():
select '£' + format(amount, 2)
This also adds in commas, which seems desirable.
If you don't want the commas, then don't use round(), cast to a decimal type:
select '£' + cast(cast(round(amount, 2) as decimal(10, 2)) as varchar(10))
round() does change the value, but it doesn't change the storage mechanism. The cast( . .. as varchar) doesn't know -- or care -- how many significant values are in the result.
EDIT (for SQL Server):
Instead of format() you can use the str() function:
select '£' + ltrim(str(amount, 10, 2))
Or the last method of converting to a decimal before the conversion.
What is the datatype of amount column? cast the round part into float
select '£' + cast(round(cast(8375.8734/100 as float),2) as varchar (100));
Result is
£83.76
Also this should done in the front end application and not in sql
This is in MS SQL Server.
I have a varchar in the format yyyy-MM-dd-hh.mm.ss.nnnnnn
For example: 2010-09-17-20.52.31.870000
I want to convert this into a datetime... but ISDATE function in MSSQL says that is not a valid date and CONVERT won't work. Is there a way to use the convert function so that I can tell it what format the date is in?
I also want to check first that it is a valid date since the varchar input may contain nothing or it may contain an invalid date.
For example, invalid data of 2010-xx-r7-99.53esdfd.31.870000... I would skip that data and not even try to convert.
As far as I'm aware, SQL Server 2005 only supports milliseconds up to 3 digits, so you could replace the periods with colons, and grab the left and right portions (ignoring the hyphen between the day and hours) and come up with something like this:
DECLARE #myDate varchar(50)
SET #myDate = '2010-09-17-20.52.31.870000'
PRINT isdate(left(#myDate, 10) + ' ' +
replace(substring(#myDate, 12, 12), '.', ':')) -- Should print '1'
PRINT cast(left(#myDate, 10) + ' ' +
replace(substring(#myDate, 12, 12), '.', ':') as datetime)
... which will effectively give you 870 milliseconds
The ISO 8601 standard separates the date and time with the letter T. Maybe that's all it needs to convert successfully? Some conversion implementations accept a space there too. I've never seen a hyphen there.
You would have to change the punctuation a bit to make it work, and reduce the precision from microseconds to millisecond. This works:
convert(datetime, '2010-09-17 20:52:31.870', 121)
What you are getting in is a bit wonky, but this works (tested):
DECLARE #temp as varchar(50)
SET #temp = '2010-09-17-20.52.31.870'
SET #temp = replace(#temp,'.',':')
set #temp = stuff(#temp,11,1,'T')
set #temp = stuff(#temp,20,1,'.')
select #temp
select cast(#temp as datetime)
NB I cut off the extra 0s, you could do this with substring if there really are those extra ones.
There is a one value 15000000.00 with Numeric datatype in SQL server.
How can i get or convert the value 15000000.00 to 15,000,000?
The database server is the wrong place to do this. Localization should be handled in the presentation layer, not the datastore.
Since you're working with the money type, or you want the result to be a money type it could be as simple as:
SELECT CONVERT(VARCHAR, #money_val, 1) -- you may have to cast to money from int first
Check out this link for a deeper discussion:
Here is an excerpt:
Comma-formatting is available only for
MONEY data types. If you want to
comma-format INT, DECIMAL etc, you
should cast it to MONEY and do a
convert with style flag 1.
DECLARE #m DECIMAL(10,2) SELECT #m =
'23456789.25'
SELECT
CONVERT(VARCHAR, #m, 1) AS DecimalValue,
CONVERT(VARCHAR, CAST(#m AS MONEY), 1) AS MoneyValue /*
DecimalValue
MoneyValue
23456789.25 23,456,789.25
*/
This is not a conversion. 15.000.000,00 (german writing) is the string representation of 15 million. A decimal data type does not store the string, but the value.
All string represenration you see are basically visualizations - in enterprise manager or software according to the settings (locale) when generating the string.
I've always used the following with success
SELECT
Convert(VARCHAR, Cast(100000.01 AS MONEY), 1) Amount
Results:
Amount
-----------------------------
100,000.01
You don't want to save your numeric data in formats. You can do this when selecting:
SELECT CONVERT(varchar(12), value, 1) AS formattedvalue FROM table
The last 1 is a convert style that puts in the . and , signs (depending on your local settings and numeric type).
If you ask me, I wouldn't even let the SQL database server worry about this and format the number in code itself.