I try to populate a result per col for each of the cast lines, but it gives me an error on each of the lines except the first one. If i just do the first one, it works as i want it too.
Why does it give me an error on the others even though the col names are existing in the referred view?
I tried just one cast line and thats working fine.
Select
cast ([sum nulls_shipment_id3] as float) / [count Total rows by carriername] *100 as [% NULL id3]
,cast ([sum nulls_shipment_id4] as float) / [count Total rows by carriername] *100 as [%null]
,cast ([EA/AH_REF] as float) / [count Total rows by carriername] *100
From dbo.BAS_CT_DATA_COMPLETENESS_vw1
Doesnt work (only if i remove the 2nd and 3rd cast formula)
If i run it with all three casts, the following error occurs:
Error:
Msg 207, Level 16, State 1, Line 4
Invalid column name 'sum nulls_shipment_id4'.
Msg 207, Level 16, State 1, Line 5
Invalid column name 'EA/AH_REF'.
cast ([sum nulls_shipment_id3] as float)
For the line above, the sum function is not being used correctly. Please fix into:
cast (sum([nulls_shipment_id3]) as float)
And the other problem is: here- > ,cast ([EA/AH_REF] as float), please use regular brackets instead of square ones like: ,cast ((EA/AH_REF) as float)
Ok i solved it, it also ran into error because on col name contained a "/", so renamed that and now it works...
Select
cast (nulls_shipment_id3 as float) / (Total_rows_by_carriername) *100 as [% NULL id3]
,cast (nulls_shipment_id4 as float) / (Total_rows_by_carriername) *100 as [%null]
,cast (nulls_EAAH_REF as float) / (Total_rows_by_carriername) *100 as [% NULLEAH]
From dbo.BAS_CT_DATA_COMPLETENESS_vw1
Related
I work on SQL Server 2012 query - I get an error:
Invalid length parameter passed to the LEFT or SUBSTRING function.
Code:
SELECT
LEFT([Impedance], PATINDEX('%[^0-9.]%', [Impedance]) - 1) AS [Impedance],
RIGHT([Impedance], LEN([Impedance]) - PATINDEX('%[^0-9.]%', [Impedance]) + 1) AS [ImpedanceUnits]
FROM
##NewTable0
I have a column Impedance with values such as 50kg as example. It splits this values to 50 and kg without any issue.
Problem occurs when the column contains only 50, without any unit of measure.
In this case, I get an error:
Invalid length parameter passed to the LEFT or SUB STRING function.
How to solve this error please?
To summarize my issue : how to handle error when column impedance has a number only, such as 50 - to work without causing an error?
50kg is working fine
50 causes an error
Update: I don't have any issue on number with unit of measure as 50kg, I only have an issue when I have only a number, such as 50, without unit of measure.
This is my issue below I need to solve
SELECT
Impedacte = (CASE
WHEN PATINDEX('%[^0-9.]%', '50') > 0
THEN LEFT('50', PATINDEX('%[^0-9.]%', '50') - 1)
ELSE '50'
END),
ImpedanceUnits = (CASE
WHEN PATINDEX('%[^0-9.]%', '50') > 0
THEN RIGHT('50', (LEN('50') - PATINDEX('%[^0-9.]%', '50') + 1))
ELSE NULL
END)
Just add an extra character so the PATINDEX() returns an appropriate value:
LEFT([Impedance], PATINDEX('%[^0-9.]%', [Impedance] + ' ') - 1) AS [Impedance],
I have a field in SQL Server 2014 that I am working with that looks like this:
**RawField**
20060202
20060323
I want to add a split the field and add a decimal point and create a numerical field. This is what I would like to see:
**RawField**
200602.02
200603.23
So I need to split the field, add the decimal point, and convert to a numerical value. I tried some code but was getting an error. Please see my code below:
select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as int)
from Table
I get an error of:
Msg 245, Level 16, State 1, Line 11
Conversion failed when converting the varchar value '200602.' to data type int.
Is this a good approach?
you want to convert the string to numeric with 2 decimal places ?
select convert(decimal(10,2), RawField) / 100.0
I guest your RawField contains other alphanumeric after that and you only posted the first 8 characters ?
this should work. Just take the first 8 characters and convert. Simple and direct
select convert(decimal(10,2), left(RawField, 8)) / 100.0
Please try this.
select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as numeric(8,2))
from Table
You are trying to cast string with decimal number to int.
Use float/numeric/decimal when casting
select cast(SUBSTRING(RawField ,1,6) + cast('.' as varchar(1)) + SUBSTRING(RawField ,7,2) as numeric(16,2))
I have a query which is proving to be cumbersome for me. I want to remove the trailing zeroes from the result.
Remove trailing zeros from decimal in SQL Server
Remove trailing zeroes using sql
select concat(100 * round(cast(count(ft.*) filter (where "Realtor_Sale" = 'Yes')
as numeric(12,5)) /
cast(count(ft.*) as numeric(12,5)),3),'%') as "Realtor Sales"
Result I am getting is:
84.800% --------------> I want 84.8%.
I tried doing this as well:
select concat(100 * round(cast(cast(count(ft.*) filter (where "Realtor_Sale" = 'Yes')
as decimal(18,5)) as float) /
cast(cast(count(ft.*) as decimal(18,5)) as float),3), '%') as "Realtor Sales"
Error:
ERROR: function round(double precision, integer) does not exist
select round(cast(cast(count(ft.*) filter (where "Realtor_Sa...
How do I get the result to round of to 84.8%?
With PostgreSQL 13 it is matter of calling trim_scale function:
trim_scale ( numeric ) → numeric
Reduces the value's scale (number of fractional decimal digits) by removing trailing zeroes
trim_scale(8.4100) → 8.41
select trim_scale(100.0 * count(ft.*) filter (where "Realtor_Sale" = 'Yes')/
count(ft.*) ) ||'%'as "Realtor Sales"
db<>fiddle demo
No need for the many casts, just use to_char() on the result:
select to_char((100 * count(ft.*) filter (where "Realtor_Sale" = 'Yes'))::decimal
/ count(ft.*), 'FM99.0%') as "Realtor Sales"
The problem with the ROUND function is, it also adds 0 at the end for the integer values.
E.g. select round(238,2) --> 238.00
Here is the solution i tried, which also preserves the integer data.
select cast(trim(trailing '0' from round(238.0100::numeric,2)::text)::numeric as text)
This will round the values with removing the trailing spaces as well as keeps the whole number as it is.
Please help me out in this as im getting an error:
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
Here is the code:-
(CAST(SUM(ISNULL(Cur.CurAmount,0)) AS FLOAT) -
CAST(SUM(ISNULL(GLActualAmount,0)) AS FLOAT)) /
CAST(SUM(ISNULL(GLActualAmount,0)) AS FLOAT) * 100 GROWTH_AMT
I use NULLIF() in this situation:
(CAST(SUM(ISNULL(Cur.CurAmount,0)) AS FLOAT) -
CAST(SUM(ISNULL(GLActualAmount,0)) AS FLOAT)
) /
NULLIF(CAST(SUM(ISNULL(GLActualAmount,0)) AS FLOAT) * 100, 0) as GROWTH_AMT
Dividing by zero always gives an error. I would add WHERE ISNULL(GLActualAmount,0) <> 0) to the end of your query, since if GLActualAmount is zero, your result is meaningless.
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