control in display Decimal separators in sql server - sql

I have problem in display the decimal numbers, it has many number.
My sql statement :
Select sum(HS$totalMoney)
the result :
12132.123444343
I want to display as 12132.12 without the another number
Thanks.

If your logic is for money you should first round the values not truncate
select CONVERT(decimal(18,2),round(12132.123444343 ,2)) gives 12132.12
select CONVERT(decimal(18,2),round(12132.125944343 ,2)) gives 12132.13

Try this one -
SELECT CAST(12132.123444343 AS DECIMAL(10,2))

if you are using mysql, use code blew
SELECT TRUNCATE(sum(HS$totalMoney), 2);

this query slove your problem
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
or you can use
select CONVERT(decimal(18,2),round(12132.1255555 ,2))

SELECT CONVERT(decimal(21, 2), sum(HS$totalMoney))
-- This one will round in SQL Server but truncate in ASE 15 (which was available to me at the time)
SELECT CONVERT(decimal(21, 2), round(sum(HS$totalMoney), 2, 1))
-- This one uses a variant of ROUND supported by SQL Server, but not ASE 15 (and will truncate the third and subsequent decimal places).

The round function has a function parameter to truncate instead of round:
select round(12132.123444343 , 2, 1)
From here:
http://msdn.microsoft.com/en-us/library/ms175003.aspx

Related

How do I extract the last 4 digits of numerical value?

How can I extract the last for digits from a number in Microsoft SQL Server? For example '6789' from a number like '123456789'.
I just want to keep the last 4 numbers.
If you are starting with a number, you are asking for the modulo operator. This generally has one of two syntaxes. SQL Server uses:
select col % 10000
An alternative syntax in other databases is:
select mod(col, 10000)
You can use right function, example for you:
select right('123456789',4)
One more approach using SUBSTRING.
DECLARE #table table(a varchar(30))
insert into #table
values ('12345678')
SELECT SUBSTRING(a,len(a) - 3, 4) from #table
5678

T-SQL Conditional logic based on Thousandth ( .00X) decimal point

I am trying to use either Round or Ceiling method based on Thousandth decimal number.
How do I write condition in the T-SQL stored procedure?
Thanks in advance!
Example:
If I have this number:
1,793.5123611111
I would like to use Round( Variable ,2,1) so that it becomes 1,793.51
So that thousandth decimal does not round off.
If I have this number:
11,80620619333
I would like to use ceiling(Variable *100) / 100 so that it becomes 11,806.21
So that thousandth decimal rounds off.
Thanks.
What is the current data type? VARCHAR?
You may try this
DECLARE #a VARCHAR(100) = '11,806.20619333'
PRINT CONVERT(VARCHAR(100),CAST(#a AS MONEY),1)
Use ROUND() without a third argument. When the third argument is not specified, it defaults to 0, which means rounding (any other value means truncating):
SELECT ROUND(Value, 2)
FROM (
SELECT 1793.5123611111
UNION ALL
SELECT 11806.20619333
) AS s (Value)
;
The above will yield these results:
--------
1793.51
11806.21

Problem with max() in sql server

I have alphanumeric values like. XYZ1,XYZ2......XYZ11, XYZ12 and so on, now I want to select only the Max numeric value, i.e. 12 here.
I tried-
select max(REPLACE(ID,'XYZ','')) from myTable;
but this is returning 9. why?
Try converting to INT before max
select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
It's still treating your value as a string instead of a number. Try:
select max(CAST(REPLACE(ID,'XYZ','') AS INT) from myTable;
Because you're still comparing strings. The fact that they contain only numeric digits doesn't mean that they're not strings. You need to convert them:
SELECT MAX(CAST(REPLACE(id, 'XYZ', '') AS INT)) FROM My_Table
Another method is
select max(REPLACE(ID,'XYZ','')*1) from myTable

How to give select digit after decimal point?

In database there is column amount which datatype is money. I want to select that row only with two digit after decimal. for this how to write the query?
My query is like this:
SELECT AMOUNT FROM DETAIL_PAGE.
I want to modify this query so that it selects two digits after decimal point.
SELECT AMOUNT - FLOOR(AMOUNT) FROM DETAIL_PAGE
That will get you just the decimal though. I think you want
SELECT FORMAT(AMOUNT, 2) FROM DETAIL_PAGE
Or without commas:
SELECT REPLACE(FORMAT(AMOUNT, 2), ',', '') FROM DETAIL_PAGE
Not sure if this is SQL standard and works elsewhere, but in Oracle you can say
select round(amount,2) from detail_page
-- round(12.345, 2) would return 12.35
or
select trunc(amount,2) from detail_page
-- trunc(12.345, 2) would return 12.34

SQL Server 2008: Varchar Conversion to Numeric Data Overflow, Probably Because Some Are Ranges

I'm working on a query with a varchar column called ALCOHOL_OZ_PER_WK. Part of the query includes:
where e.ALCOHOL_OZ_PER_WK >= 14
and get the errors:
Arithmetic overflow error converting varchar to data type numeric.
and:
Error converting data type varchar to numeric.
Looking into the values actually stored in the column, the largest look close to 100, but some of the entries are ranges:
9 - 12
1.5 - 2.5
I'd like to get the upper limit (or maybe the midpoint of the range) from rows with entries like this and have it be the value being compared to 14.
What would be the (or an) easy way to do this?
As always, thank you!
Your DB is obviously result of some survey, and it seems to contain the original survey data. The usual way is to run this through an ECCD (Extract, Clean, Conform, Deliver) process and store clean and standardized data into a separate database (maybe a warehouse) which can then be used for analytics and reporting.
If you have SSIS use data profiling task to get an idea of types of strings you have in there. The Column Pattern Profile reports a set of regular expressions on the string column, so you will get an idea of what's inside those strings. If you do not have SSIS, you can use eobjects DataCleaner to do the same.
If you can not spare a new database or at least a new table -- at minimum add a numeric column to this table and then extract numeric values form those strings into the new column. You may want to use "something else" (SSIS, Pentaho Kettle, Python, VB, C#) to do this -- in general T-SQL in not very good at string processing.
My guess is that this is not the only column that has garbage inside, so any analysis that you may run on this may be worthless.
And if you still think that the ranges are the only problem, this example may help:
First some data
DECLARE #myTable TABLE (
AlUnits varchar(10)
) ;
INSERT INTO #myTable
(AlUnits )
VALUES ( '10' )
, ( '15' )
, ( '20' )
, ( '7 - 12' )
, ( '3 - 5' )
;
The query splits records into two groups, numeric and not numeric -- assumed ranges.
;
WITH is_num
AS ( SELECT CAST(AlUnits AS decimal(6, 2)) AS Units_LO
,CAST(AlUnits AS decimal(6, 2)) AS Units_HI
FROM #myTable
WHERE ISNUMERIC(AlUnits) = 1
),
is_not_num
AS ( SELECT CAST( RTRIM(LTRIM(LEFT(AlUnits,
CHARINDEX('-', AlUnits) - 1)))
AS decimal(6,2)) AS Units_LO
,CAST(RTRIM(LTRIM(RIGHT(AlUnits,
LEN(AlUnits)
- CHARINDEX('-', AlUnits))))
AS decimal(6,2)) AS Units_HI
FROM #myTable
WHERE ISNUMERIC(AlUnits) = 0
)
SELECT Units_LO
,Units_HI
,CAST(( Units_LO + Units_HI ) / 2.0 AS decimal(6, 2)) AS Units_Avg
FROM is_num
UNION ALL
SELECT Units_LO
,Units_HI
,CAST(( Units_LO + Units_HI ) / 2.0 AS decimal(6, 2)) AS Units_Avg
FROM is_not_num ;
Returns:
Units_LO Units_HI Units_Avg
----------- ----------- ----------
10.00 10.00 10.00
15.00 15.00 15.00
20.00 20.00 20.00
7.00 12.00 9.50
3.00 5.00 4.00
Not sure about easy ways.
A proper way is to store the numbers in two columns, ALCOHOL_OZ_PER_WK_MIN and ALCOHOL_OZ_PER_WK_MAX.
As you say you need to calculate numeric values, which you can then use in your query.
Probably the easiest way is to use some simple logic to calculate the average or upper limit using string functions, and string to numeric functions.
If all you want is the upper limit, just get the characters after the '-' and use that.
"probably because some are ranges" - do you get that "range" is not a SQL Server Data type? You've got non-numeric data you're trying to convert into numeric data, and you've got a scalar value you're comparing to a non-scalar value.
This database has some issues.