I want to get a number 5000.1 and divide it by 1000 before adding an "F" infront of it.
How do i do this? I tried and failed this:
select "F" + round ( acq.store_size_net / 1000, 0) from acq
I suspect your missing the cast of the number to a text data type
Without knowing the exact dialect of sql you're using im gonna hazard a guess at ms-sql
select 'F' + cast(cast(round ( 5000.1 / 1000, 0)as int) as nvarchar(50))
produces output
F5
This will work in Oracle :
select 'F' || round (acq.store_size_net / 1000, 0) from acq
Related
I'd like to add a divisor symbol (%) to the end of my expression shown here:
select 'On-Site Case Rate' Exp1,
isnull(sum(onsite.a) * 100 / count(onsite.casecount), 0) '400',
isnull(sum(onsite.b) * 100 / count(onsite.casecount), 0) '401'
from onsite
How would I go about doing that? Do I need to use a concat and reformat my query or is it possible to insert a " + '%' "+ somewhere?
Here is a sample result, this is for an SSRS report
EDIT1: Here is the design view of my report as well
Considering that you're using SSRS, don't try to add a % sign to the end of your percentage, and convert it to a varchar, leave it as a decimal. Instead, change your display format.
Select the cell(s) that are returning your percentages and press F4. Then, in the now targeted Properties Pane locate the Format Property and change it to 0%. If you want it to display 1 (or more) decimal places then use 0.0%, 0.00%, ... you get the idea.
Note that you need to ensure that your values are returning a decimal value. You're multiplying your values by 100, which implies that you aren't. 15 isn't 15%, it's 1500%. 15% = 0.15.
In Sql Server (starting with version 2012) you can use the CONCAT function:
select 'On-Site Case Rate' Exp1,
CONCAT(isnull(sum(onsite.a) * 100 / count(onsite.casecount), 0), '%') '400',
CONCAT(isnull(sum(onsite.b) * 100 / count(onsite.casecount), 0), '%') '401'
from onsite
I'm trying to read a column from a database using a SQL query. The column consists of empty string or numbers as strings, such as
"7500" "4460" "" "2900" "2640" "1850" "" "2570" "9050" "8000" "9600"
I'm trying to find the right sql query to extract all the numbers (as integers) and removing the empty ones, but I'm stuck. So far I've got
SELECT *
FROM base
WHERE CONVERT(INT, code) IS NOT NULL
Done in program R (package sqldf)
If all columns are valid integers, you could use:
select * , cast(code as int) IntCode
from base
where code <> ''
To prevent cases when field code is not a valid number, use:
select *, cast(codeN as int) IntCode
from base
cross apply (select case when code <> '' and not code like '%[^0-9]%' then code else NULL end) N(codeN)
where codeN is not null
SQL Fiddle
UPDATE
To find rows where code is not a valid number, use
select * from base where code like '%[^0-9]%'
select *
from base
where col like '[1-9]%'
Example: http://sqlfiddle.com/#!6/f7626/2/0
If you don't need to test for the number being valid, ie. a string such as '909XY2' then this may run marginally faster, more or less depending on the size of the table
Is this what you want?
SELECT (case when code not like '%[^0-9]%' then cast(code as int) end)
FROM base
WHERE code <> '' and code not like '%[^0-9]%';
The conditions are repeated in the where and case on purpose. SQL Server does not guarantee that where filters are applied before logic in the select, so you can get an error with conversions. More recent versions of SQL Server have try_convert() to fix this problem.
Using sqldf with the default sqlite database and this test data:
DF <- data.frame(a = c("7500", "4460", "", "2900", "2640", "1850", "", "2570",
"9050", "8000", "9600"), stringsAsFactors = FALSE)
try this:
library(sqldf)
sqldf("select cast(a as aint) as aint from DF where length(a) > 0")
giving:
aint
1 7500
2 4460
3 2900
4 2640
5 1850
6 2570
7 9050
8 8000
9 9600
Note In plain R one could write:
transform(subset(DF, nchar(a) > 0), a = as.integer(a))
I got a output like this
0.00234690616839645663803848176618444236941
the way I want is
2.3
first I remove the zero
select replace(0.00234690616839645663803848176618444236941,0) from dual;
then I try to do the round function on it , but its giving me zero any idea how can we get this
select round(replace(0.00234690616839645663803848176618444236941,0) ) from dual;
You can try this :
select round(0.00234690616839645663803848176618444236941 * 1000,1) from dual;
Result:
2.3
When you strip out the zeros, you are left with .23.....
Select round (.23, 0) will return 0, because you are telling the db to round to 0 decimal places.
If you multiply the result of your replace by 10, that will get you want you want. Not sure what you are doing makes any sense, but it works:
select round (10 * (replace(0.00234690616839645663803848176618444236941,0)),1) from dual;
SQL Fiddle
I have field called CallingParty in My CDR table it contains data like this:
CallingParty
------------
267672668788
I want to select the first 3 number of each of those numbers like
CallingParty
------------
267
if CallingParty is of type int:
SELECT CAST(LEFT(CallingParty, 3) AS INT)
From CDR
SQL Server has a Left() function, but it works best on strings. (varchar/char in SQL)
Select left(cast(267672668788 as varchar), 3)
Use this query:
SELECT SUBSTRING(CAST(CallingParty AS VARCHAR(50)), 1, 3) FROM [CDR]
If the data length does not change then you can always divide by 10 * the digits you have
SELECT FLOOR(267672668788 / 1000000000)
=267
Try this:
SELECT Substring(callingparty, 1, Length(callingparty) - 9)
FROM cdr;
I have a one database table field called Amount which type is decimal(18,6). so it is stored in database up to 6 decimal points like 9.786534 But while retrieving that field using select query i have to take care like following
Remove trialling zero e.g if number is 9.230000 then result is only 9.23
If decimal points are all zero then only remove only four trialling zero e.g If number is 9.000000 then result is 9.00
Result is up to 2 decimal point if there are trialling zero.
If we write simple query like
select TOP 1 Amount From EmployeeMaster
then it gives 9.230000
but my intension is to remove trailing zero..
Please help me..
It works for removing trailing zeros, but I am still not able to convert 9 to 9.00 in this method.
Declare #myvalue varchar(50),
#Price Varchar(50)
Set #Price = '9.230000'
set #Myvalue = reverse(substring(#Price,patindex('%.%',#Price)+1,len(#Price)))
SELECT
case
When patindex('%.%[1-9]%',#price) = 0 Then
substring(#price,1,patindex('%.%',#price)-1)
else
substring(#price,1,patindex('%.%',#price)-1) + '.' + Reverse(substring(#Myvalue,patindex('%[1-9]%',#Myvalue),len(#Myvalue)))
END
Coming from decimal(18,6) you could do...
select cast(Amount as decimal(18,2))
Most databases that support the CAST function will round the number while converting it. On SQLServer this is what I would do if I wanted rounding.
If what you actually want is a string with only two digits after the decimal then you could
select cast((Amount as decimal(18,2)) as nvarchar)
nvarchar is SQLServer's variable length unicode type. Databases do not agree much on string types. Your database may have a different one. The rest of that sql is ANSI standard. Not all dbs support that either but many do.
This should work
SELECT CAST(REPLACE(RTRIM(REPLACE(CAST(CAST(33.9082976 AS DECIMAL(38,8)) AS NVARCHAR(256)),'0',' ')),' ','0') AS FLOAT)
Does this work?
select TOP 1 ROUND(Amount, 2) From EmployeeMaster
TRY below mentioned code.
SELECT TOP 1 CONVERT(DECIMAL(10,2),Amount) From EmployeeMaster
Hope it will work as expected.
An alternative approach:
1) convert the decimal to a string;
2) split the string into 2 parts, separating the last 4 characters from the rest of the string;
3) remove trailing zeros from the last 4 characters;
4) concatenate the two parts back.
WITH data (V) AS (SELECT CAST(9.786534 AS decimal(18,6))
UNION ALL
SELECT CAST(9.78653 AS decimal(18,6))
UNION ALL
SELECT CAST(9.7800 AS decimal(18,6))
UNION ALL
SELECT CAST(9.7 AS decimal(18,6))
UNION ALL
SELECT CAST(9.00000 AS decimal(18,6))
)
, AsString (V) AS (SELECT CAST(V AS varchar) FROM data)
, Split (L, R) AS (SELECT LEFT(V, LEN(V) - 4), RIGHT(V, 4) FROM AsString)
, Adjusted AS (SELECT L,
REPLACE(RTRIM(REPLACE(R, '0', ' ')), ' ', '0') AS R
FROM Split)
SELECT Result = L + R FROM Adjusted
The output of the above script is:
Result
--------
9.786534
9.78653
9.78
9.70
9.00
I guess using patindex in your case:
CASE WHEN FLOOR(Amount) <> CEILING(Amount) THEN
LTRIM(SUBSTRING(STR(Amount, 18, 6), 1, LEN(STR(Amount, 18, 6)) - PATINDEX('%[^0]%', REVERSE(str(Amount, 18, 6))) + 1))
ELSE STR(Amount,18,2)
END
for a decimal(18,6) field this should work:
select trim(to_char(Amount, '999999999999999999.99')) from EmployeeMaster
(at least for Oracle, not sure about other types)