Round function query 2 to 3 arguments? - sql

I am attempting to find the revenue per distinct user in this query but seem to be running in this error.
select concat('$',format(cast(round(sum(total)/count(distinct(customers))),2)
as int),N'N','en-US')
from table
My error:
The round function requires 2 to 3 arguments

I suspect you mean:
SELECT CONCAT('$',FORMAT(CAST(ROUND(SUM(Total)/COUNT(DISTINCT customers),2) AS int),N'N'),'en-US')
FROM [table];
But, really, worry about the formatting of your values in your presentation layer (The FORMAT and CONCAT don't need to be there).
Also, Why ROUND({expr},2) and then CAST({expr} AS int)? Why not ROUND({expr},0)?

For instance used 2 as length to round
round(sum(total)/count(distinct(customers)),2)

Related

How to query column with letters on SQL?

I'm new to this.
I have a column: (chocolate_weight) On the table : (Chocolate) which has g at the end of every number, so 30x , 2x5g,10g etc.
I want to remove the letter at the end and then query it to show any that weigh greater than 35.
So far I have done
Select *
From Chocolate
Where chocolate_weight IN
(SELECT
REPLACE(chocolote_weight,'x','') From Chocolate) > 35
It is coming back with 0 , even though there are many that weigh more than 35.
Any help is appreciated
Thanks
If 'g' is always the suffix then your current query is along the right lines, but you don't need the IN you can do the replace in the where clause:
SELECT *
FROM Chocolate
WHERE CAST(REPLACE(chocolate_weight,'g','') AS DECIMAL(10, 2)) > 35;
N.B. This works in both the tagged DBMS SQL-Server and MySQL
This will fail (although only silently in MySQL) if you have anything that contains units other than grams though, so what I would strongly suggest is that you fix your design if it is not too late, store the weight as an numeric type and lose the 'g' completely if you only ever store in grams. If you use multiple different units then you may wish to standardise this so all are as grams, or alternatively store the two things in separate columns, one as a decimal/int for the numeric value and a separate column for the weight, e.g.
Weight
Unit
10
g
150
g
1000
lb
The issue you will have here though is that you will have start doing conversions in your queries to ensure you get all results. It is easier to do the conversion once when the data is saved and use a standard measure for all records.

SQL Server floating point numbers

I was wondering if there is a way to show the values of columns of type floating point numbers in two decimal places in SQL Server 2008 via settings? For instance, let say I have a table called orders with several columns. I want to be able to do the following:
SELECT * FROM orders
I expect to see any values in columns of type float to display with decimal notation; for instance, a value of 4 should display as 4.0 or 4.00.
Thanks
You may use CONVERT function with NUMERIC( x , 2) for numeric values
( where x is at least 3, better more, upto 38 )
SELECT CONVERT(NUMERIC(10, 2), 4 ) as "Dcm Nr(2)";
Dcm Nr(2)
---------
4,00
SELECT CONVERT(NUMERIC(10, 1), 4 ) as "Dcm Nr(1)";
Dcm Nr(1)
---------
4,0
The simplest form of what happens to me is making a "cast", for example:
SELECT CAST(orders AS DECIMAL(10,2)) FROM [your table];
The short answer to your question is "No".
SQL Server isn't really in the business of data presentation. We all do a lot of backbends from time to time to force things into a presentable state, and the other answers provided so far can help you on a column by column basis.
But the sort of "set it and forget it" thing you're looking for is better handled in a front end application.

sql query to show a range and account for missing numbers

I have a SQL query
SELECT
Group_Id, MIN(Rec_Number) as RecStart, MAX(Rec_Number) AS RecEnd
FROM
Rec
WHERE
Group_Id != ''
GROUP BY
Group_Id
ORDER BY
Group_Id
This produces the following kind of results.
92-2274 9222740001 9222740004
92-2275 9222750001 9222750026
etc...
However if record 3 is missing (in the first row for instance) the query obviously doesn't account for it. What I am trying to do is the following
92-2274 9222740001 9222740002
92-2274 9222740004 9222740018
92-2275 9222750001 9222750016
92-2275 9222750018 9222750026
etc...
So essentially each time the script sees a record missing inside the group it starts a new line whilst staying inside the group before iterating on the next group. The group_Id is of course the first 6 digits of the rec_Number
I would also like to do this as well
92-2274 0001 0002
92-2274 0003 0004
Or even trim it to and remove the leading 0's as well if possible I know about using Right (Rec_Number, 4) however as this is a float the automatic convert to string seems to be messing something up as I get +009 is many columns so I assume I need to cast first or something. This particular function I could do it Excel after the fact I guess but I'm sure SQL could do it if the guy writing the query was a DBA and not a bumbling server admin (that's me!)
So is there a way of doing that in SQL also I must warn you that the standard CTE or using functions such as row number don't work as this is SQL Server 2000 - yes it is that old!
Hence me struggling to find posts on Stack Overflow that apply. Many of them start with the WITH keyword which means I can't use any of those to start with!
I think I am needing an IF ELse kind of block but I am not sure what kind of method I can use to get the query to create a new row each time it hits a missing concurrent number in the group range.
The final output will show me the ranges of records in each group whilst highlighting the missing ones via a new line each time.
For the second part, this should work :
RIGHT ( CAST ( MIN (Rec_Number) as Decimal(10)), 4)
It will only keep the last 4 characters of your number.

Checking Range in Comma Separated Values [SQL Server 2008]

I have a table with following structure
ID FirstName LastName CollectedNumbers
1 A B 10,11,15,55
2 C D 101,132,111
I want a boolean value based on CollectedNumber Range. e.g. If CollectedNumbers are between 1 and 100 then True if Over 100 then False. Can anyone Suggest what would be best way to accomplish this. Collected Numbers won't be sorted always.
It so happens that you have a pretty simple way to see if values are 100 or over in the list. If such a value exists, then there are at least three characters between the commas. If the numbers are never more than 999, you could do:
select (case when ','+CollectedNumbers+',' not like '%,[0-9][0-9][0-9]%' then 1
else 0
end) as booleanflag
This happens to work for the break point of 100. It is obviously not a general solution. The best solution would be to use a junction table with one row per id and CollectedNumber.
Just make a function, which will return true/False, in the database which will convert the string values(10,11,15,55) into a table and call that function in the Selection of the Query like this
Select
ID, FirstName, LastName,
dbo.fncCollectedNumbersResult(stringvalue) as Result
from yourTableName
I think the easiest you can do is build a C# function and use the builtin sqlclr to load it as a custom function you can then call.
Inside the C# function, you can then sort your numbers and make simple logic to return your true/false.

Substring of a Substring

I have a text field (field named "BiographyText" in the "Employs" table) in SQL Server 2008 which stores KPI target figures:
rebfirst60,
reifirst1.3,
retfirst50
The first target is 60, for RebFirst, the second is 1.3 for ReiFirst and 50 for the third, RetFirst.
I want to be able to return the 3 different numerical values, as these would be deemed the targets for each kpi for a certain employee.
I am having a complete mind block trying to figure out the best way to do this, any advice/help?
Overall I am trying to find the kpiname ("rebfirst") and then retrieve the next 2 characters/digits
I tried the following, but it errors on function 2 of the first substring, as it is non-numeric:
select SUBSTRING(biographytext,SUBSTRING('rebfirst',1,2),2) from employs
Thanks
Try this
SELECT
CASE WHEN PatIndex('%[a-z]%',REVERSE(BiographyText)) > 0
THEN RIGHT(BiographyText,PatIndex('%[a-z]%',REVERSE(BiographyText))-1)
ELSE '' END AS target
FROM employs
and also check another solution using function
From Post The first target is 60, for RebFirst, the second is 1.3 for ReiFirst and 50 for the third, RetFirst.
From Comment I don't need the actual kpiname from this just the value
The following Query will give you the Answer.
select case column_name when 'rebfirst60' then 60
when 'reifirst1.3' then 1.3
when 'retfirst50' then 50
from employs
I have this now by using the following:
select
SUBSTRING(SUBSTRING(biographytext,1,10),9,2) as RebookingFirst,
SUBSTRING(SUBSTRING(biographytext,11,20),12,3) as ReInventFirst,
SUBSTRING(SUBSTRING(biographytext,21,30),16,3) as ReCreateFirst,
SUBSTRING(SUBSTRING(biographytext,31,40),20,2) as RetentionFirst,
SUBSTRING(SUBSTRING(biographytext,41,50),23,2) as ReferralsFirst
from employs
This gives me the results for each kpi