Switch is causing #error, why and how can I fix it - sql

I have 3 fields in my table: start, end (dates) and length (number, might be blank).
My Aim is to calculate an end date using start and length where end doesn't exist...
I have:
SELECT Switch((g.length<>0) And IsDate(g.end),DateAdd("m",g.length,g.start)) AS field FROM table g
If there is no start, end or length, Access displays blank - this is fine.
If there is no end, but start and length are ok, the calculated date is shown - again fine.
BUT
If there is no end, or length, but a start exists, access displays #Error
I don't understand why, and can't fix it, please help!

If there is no end, but start and length are ok, the calculated date is shown
Are you certain about that point? When I try your query with values for start and length, but no value for end, I get a Null for "field".
Also, you're calling the DateAdd function when this condition is True:
g.length<>0) And IsDate(g.end)
In order for that condition to be True, g.end would have to already contain a valid date ... but I thought you didn't want to perform the calculation when you already have a value for g.end. I'm confused.
Let's try a different approach. If this query returns what you want, good. If not help us understand why it is incorrect.
SELECT
d.start,
d.end,
d.length,
IIf(IsDate(d.end), d.end,
IIf(Nz(d.length)>0, DateAdd("m", d.length, d.start), Null)) AS field
FROM table AS d;

Related

Postgresql - CASE/WHEN with wrong return

I tried to use CASE/WHEN inside Postgresql to check two colums, but the results are odd.
As it's shown in the image below, all the lines where "gain_value" is 8 AND "patrimony_value" have a higher value return a wrong result.
This is my statement:
select stop_value, gain_value, patrimony_value,
case
when patrimony_value >= gain_value then 1
else 2
end
from copy.copy_stop_gain csg
Since it's a pretty straightforwad "if/else", i'm really not sure what i could be doing wrong.
Can anyone show me where is my mistake?
Try casting string values to numbers (or perhaps change column type in schema)...
select stop_value, gain_value, patrimony_value,
case
when patrimony_value::INTEGER >= gain_value::INTEGER then 1
else 2
end
from copy.copy_stop_gain csg

STUFF function sql returns null?

I have a specific column in a table, it shall contains only numbers in Nvarchar that have a length of 3. Unfortunately, some users wrote '12' but they should have written '012'. There were not enough validation at the time.
I need to fix that. Here is the logic I used :
UPDATE [Mandats_Approvisionnement].[dbo].[ARTICLE_ECOLE]
SET [UNIT_ADM] = STUFF(UNIT_ADM, 0, 0, '0')
WHERE LEN(UNIT_ADM) = 2;
The error goes like :
Cannot insert the value NULL into column 'UNIT_ADM', table
'Mandats_Approvisionnement.dbo.ARTICLE_ECOLE'; column does not allow
nulls. UPDATE fails.
I can't see where the problem is, I verified and all the records contain at least 2 characters, so the STUFF function cannot returns null as there are no NULL records in that table column [unit_adm]... How do I make it work ?
It should be stuff(UNIT_ADM,1,0,'0') as stuff returns null if the start position is 0.
Citing the documentation:
If the start position or the length is negative, or if the starting
position is larger than length of the first string, a null string is
returned. If the start position is 0, a null value is returned.
You could make this simpler by using
right('0' + UNIT_ADM, 3)
instead of stuff.

DB2 SQL - How can I display nothing instead of a hyphen when the result of my case statement is NULL?

All,
I'm writing a query that includes a CASE statement which compares two datetime fields. If Date B is > Date A, then I'd like the query to display Date B. However, if Date B is not > Date A, then the user who will be getting the report created by the query wants the column to be blank (in other words, not contain the word 'NULL', not contain a hyphen, not contain a low values date). I've been researching this today but have not come up with a viable solution so thought I'd ask here. This is what I have currently:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN B.DTE_LNP_LAST
ELSE ?
END AS "DATE OF DISCONNECT"
If I put NULL where the ? is, then I get a hyphen (-) in my query result. If I omit the Else statement, I also get a hyphen in the query result. ' ' doesn't work at all. Does anyone have any thoughts?
Typically the way nulls are displayed is controlled by the client software used to display query results. If you insist on doing that in SQL, you will need to convert the date to a character string:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN VARCHAR_FORMAT(B.DTE_LNP_LAST)
ELSE ''
END AS "DATE OF DISCONNECT"
Replace VARCHAR_FORMAT() with the formatting function available in your DB2 version on your platform, if necessary.
You can use the coalesce function
Coalesce (column, 'text')
If the first value is null, it will be replaced by the second one.

Query for dropping value from field

I have a query that looks at duty and vat information and does calculation based on the returned value.
The column that tells me the duty rates is in the table formatted as either, for example 3.7% or 8% in both bases I need remove the % from my return value. Otherwise my SUM clasue fails.
I have sorted the problem for the 3.7% example with the follwoing:
CASE WHEN CustomsTariff.CommodityCode.StandardDuty = 'Free' THEN '0.0' ELSE SUBSTRING(CustomsTariff.CommodityCode.StandardDuty, 1, 3) END AS DutyRate,
This drops the % for any returns where there is decimal palce but I need to add to the CASE to say if the StandardDuty value has no decimal places drop the % character as well without messing up the first statement that looks to the 1st 3 digits.
Thanks.
Did you try a replace() on the % character? Replace
CASE WHEN CustomsTariff.CommodityCode.StandardDuty = 'Free'
THEN '0.0' ELSE REPLACE(CustomsTariff.CommodityCode.StandardDuty, N'%', N'')
END AS DutyRate,

Invalid floating point operation error when counting logarithm in SQL Server 2008

In Microsoft SQL Server 2008, I have a table, say myTable, containing about 600k rows (actually, it is a result of joining several other tables, but i suppose this is not important). One of its columns, say value is of type numeric(6,2).
The simple query SELECT value FROM myTable ORDER BY value returns of course about 600k numbers, starting with 1.01 (i.e. the lowest) and ending with 70.00 (highest); no NULLs or other values.
Please notice, that all these values are numeric and positive. However, when calling SELECT LOG(value) FROM myTable, i obtain an error message "An invalid floating point operation occurred".
This error always appears after about 3 minutes of the query running. When copying the 600k values to Excel and counting their LN(), there is absolutely no problem.
I have tried converting value to real or float, which did not help at all. Finally I found a workaround: SELECT LOG(CASE WHEN value>0 THEN value ELSE 1 END) FROM myTable. This works. But why, when all the values are positive? I have tried to take the result and compare the logarithms with those counted by Excel - they are all the same (only differences of the order 10^(-15) or smaller occured in some rows, which is almost surely given by different accuracy). That means that the condition in the CASE statement is always true, I suppose.
Does anyone have any idea why this error occurs? Any help appreciated. Thanks.
You can identify the specific value that's causing the prob;
declare #f numeric(6,2), #r float
begin try select
#f = value, #r = LOG(value)
from mytable
end try begin catch
select error_message(),'value=',#f
end catch
You would get this error - "An invalid floating point operation occurred" when you do LOG(0). The value of LOG(zero) is indeterminate in the world of Maths, hence the error.
Cheers.