Latitude and Longitude conversion error - sql

I'm trying to execute a specific query in my database, but occurs the following error
Table San_Filial
Filial_Id Name lat lon
2 A -19.926131 -43.924373
3 B -19.952192 -43.938789
4 C -19.939626 -43.924541
5 D -19.95529 -43.92953
6 E -19.9099 -43.93124
7 F -19.926191 -43.946067
9 G -19.97125 -43.96622
14 H -19.89038 -43.921734
17 I -19.88838 -43.93059
19 J -19.94305 -43.94093
Query
SELECT *
FROM San_Filial
WHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17)
AND ACOS(COS(RADIANS(ltrim(San_Filial.lat)))
* COS(RADIANS(convert(float, -19.926131)))
* COS(RADIANS(ltrim(San_Filial.lon))
- RADIANS(convert(float, -43.924373)))
+ SIN(RADIANS(ltrim(San_Filial.lat)))
* SIN(RADIANS(convert(float, -19.926131)))) * 6380 < 5.0
Error
Mesage 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Someone can help me ?

Start by seeing if you have any data that is not in a correct format:
select *
from san_filia1
wHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17) and
(isnumeric(lat) = 0 or isnumeric(long) = 0)
From this, you'll see what is causing the problem and then you can fix it.
The correct fix is probably along these lines:
select *
from (select sf.*,
(case when isnumeric(lat) then cast(lat as float) end) as latf,
(case when isnumeric(long) then cast(long as float) end) as longf
from san_filial sf
) sf
where . . . -- use Latf and Longf instead of lat and long
You need to do the conversion inside a case statement to guarantee that it works as expected. SQL does not guarantee the ordering of statements and a filter might be applied after the calculation.

Related

where statement not allowed with group by function

I'm still a newbie with oracle, here I have a query that returns data like :
0.52
0.01
1
12
Desired result :
1
12
i have tried to do something like this in where part but it returns group function is not allowed here:
to_char((Max(start_time_timestamp+ (2/24))- p.port_statusmoddat), 999.999) >1
the query I'm working with:
select to_char((Max(start_time_timestamp+ (2/24))- p.port_statusmoddat), 999.999) as Diff
from ZAJBIREJ.UDR_ST r,directory_number d, CONTR_SERVICES_CAP C, MPUSNTAB SN, unicam.vw_contr_imsi_sim x, port p
where reject_reason_code = 'ISUBS'
and r.s_p_port_address = p.port_num (+)
and c.co_id = x.co_id (+)
and s_p_number_address = d.dn_num (+)
and d.dn_id =c.dn_id
AND C.SNCODE = sn.SNCODE
and C.MAIN_DIRNUM = 'X'
and c.cs_deactiv_date is null
and p.port_status = 'd'
AND nvl(C.cs_deactiv_date,'01-jan-2300') = (SELECT MAX(nvl(CA.cs_deactiv_date,'01-jan-2300'))
FROM CONTR_SERVICES_CAP CA, MPUSNTAB SNT
WHERE CA.DN_ID = D.DN_ID
AND SNT.SHDES = SN.SHDES)
group by reject_reason_code ,c.co_id, s_p_number_address,r.s_p_port_address,x.IMSI,p.port_status, p.port_statusmoddat
You need to use the HAVING clause:
group by reject_reason_code ,c.co_id, s_p_number_address, r.s_p_port_address,
x.IMSI, p.port_status, p.port_statusmoddat
having Max(start_time_timestamp+ (2/24))- p.port_statusmoddat >1
I also removed the redundant TO_CHAR as you want to test the number exceeds 1, not that a string of characters exceeds 1.

How to run procedure without parameter in Teradata

How to run procedure without parameter in Teradata
I'm trying with : call db1.proc_dbOSA()
Error Msg:
Call failed 3707: PROC_DBOSA: Syntax error, expected something
like a name or a Unicode delimited identifier between ‘(‘ and ‘)’
New Procedure with error result.
When i run only code then everything works ok.
REPLACE PROCEDURE db1.proc_dbOSA()
BEGIN
DELETE FROM db1.LOG_dbOSA;
INSERT INTO
db1.LOG_dbOSA
(StoreNo, IDX, Flow, Status, MRP, OSA_IDX, OSA_AC)
WITH a AS (
SELECT
c.StoreCode,
CAST(SUBSTRING(c.ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
CASE
WHEN c.MinimumTargetStockWrpl >= l.MinimumTargetStockWrpl THEN CAST(l.MinimumTargetStockWrpl AS INT)
WHEN c.MinimumTargetStockWrpl < l.MinimumTargetStockWrpl THEN CAST(c.MinimumTargetStockWrpl AS INT)
End AS StoreMin,
c.ValUnrestrictedStock
FROM
db1.tab1 c
INNER JOIN
(
SELECT
StoreCode,
ArticleCode,
MinimumTargetStockWrpl
FROM
db1.tab1
WHERE
ProcessingDate = CURRENT_DATE - 14
) l ON c.StoreCode = l.StoreCode AND c.ArticleCode = l.ArticleCode
WHERE
c.ProcessingDate = CURRENT_DATE AND c.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl > 0
)
, t AS
(
SELECT
CAST(SUBSTRING(ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
ArticlesPlanner
FROM
DWH_db_V.STK_B_ARTICLE_DAY_V
WHERE
ProcessingDate = CURRENT_DATE AND StoreCode = 'DR04'
)
SELECT
a.StoreCode,
a.IDX,
t.RpType,
t.ArticlesPlanner,
a.RpType,
CASE
WHEN a.ValUnrestrictedStock > 0 THEN 1
WHEN a.ValUnrestrictedStock <= 0 THEN 0
End AS OSA_IDX,
CASE
WHEN a.ValUnrestrictedStock >= StoreMin THEN 1
WHEN a.ValUnrestrictedStock < StoreMin THEN 0
End AS OSA_AC
FROM
a
LEFT JOIN
t ON t.IDX = a.IDX;
End;
BTEQ Error:
+---------+---------+---------+---------+---------+---------+---------+----
Call proc_dbOSA;
Call proc_dbOSA;
$
* Failure 3707 Syntax error, expected something like '(' between the word
'proc_dbOSA' and ';'.
Statement# 1, Info =18
* Total elapsed time was 1 second.
Call proc_dbOSA();
* Failure 3707 PROC_DBOSA:Syntax error, expected something like a name or
a Unicode delimited identifier between '(' and ')'.
* Total elapsed time was 1 second.
Stored procedures do not support the following:
EXPLAIN and USING request modifiers within a stored procedure
EXECUTE macro statement
WITH clause within a stored procedure.
Stored procedures, as well as macros, do not support the following query logging statements:
BEGIN QUERY LOGGING
END QUERY LOGGING
FLUSH QUERY LOGGING
REPLACE QUERY LOGGING

How to get database size in SQL Server Enterprise edition

I'm getting an error with regard of this query
SELECT
DB.name,
SUM(CASE WHEN type = 0 THEN MF.size * 8 / 1024.0 ELSE 0 END) AS DataFileSizeMB,
SUM(CASE WHEN type = 1 THEN MF.size * 8 / 1024.0 ELSE 0 END) AS LogFileSizeMB,
SUM(mf.size* 8 / 1024.0) AS TotalSizeMB,
SYSDATETIME() AS Datelogged
FROM
sys.master_files MF
JOIN
sys.databases DB ON DB.database_id = MF.database_id
WHERE
DB.source_database_id IS NULL
GROUP BY
DB.name
ORDER BY
Datelogged DESC
I get an error:
Arithmetic overflow error converting expression to data type int
I'm running this on SQL Server 2016 Enterprise edition. I've tried to run this on Standard and other lower editions and it'll work. Can someone tell what's wrong with this code and what should be the correct one. I do not own this code, I've just copied it from another site.
Action taken : I've try to convert this into bigint data type but still no avail.
Thank you very much
instead of MF.size * 8 use MF.size * 8.0

Percentage of two values returning NULL

Same as yesterdays question which has been answered successfully but different problem. I have two values, 1 and 0 for which I need to calculate the percent change. Based on this website http://www.percent-change.com/index.php?y1=1&y2=0 the percent change between 1 and 0 is -100%. Based on the suggested formula which is (((y2- y1))/ y1) my code looks like this.
DefinedYearVSPriorYearIndividual = ((( CTEDefinedYear.IndividualCases - CTEPreviousYear.IndividualCasesLastYear ))
/ ( CTEPreviousYear.IndividualCasesLastYear ) ) * 100
which returns NULL.
The two numbers are
CTEDefinedYear.IndividualCases = 1
CTEPreviousYear.IndividualCasesLastYear = 0
The desired result should be -100%.
Can anybody see what I'm doing wrong?
Here is the answer.
Declare #y1 as int =1;
Declare #y2 as int =0;
select (((#y2- #y1))/ #y1)*100
Output is -100. You missed the *100 part.
In your case, You switched variables. attached formula is right one.
select ((0 - 1) / 1)*100;
But you used select ((1 - 0) / 0)*100;
so, you will get an error:
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
You have to handle 0 in the division side, with CASE logic, to avoid divide by zero error.
DECLARE #CTEDefinedYear_IndividualCases INT = 1
DECLARE #CTEPreviousYear_IndividualCasesLastYear INT = 0
SELECT ((#CTEDefinedYear_IndividualCases - #CTEPreviousYear_IndividualCasesLastYear) / (CASE WHEN #CTEPreviousYear_IndividualCasesLastYear = 0 THEN 1 ELSE #CTEPreviousYear_IndividualCasesLastYear END)) * 100
Got it to work with this code.
DefinedYearVSPriorYearIndividual = ISNULL(100.0 *
(ISNULL(CTEDefinedYear.IndividualCases,0)
- ISNULL(CTEPreviousYear.IndividualCasesLastYear,0))
/ NULLIF(CTEPreviousYear.IndividualCasesLastYear,0),0)

Divide by zero error in SQL even though I excluded the zero cases?

Here is the code that I've been trying to run:
SELECT C.* FROM
(SELECT
B.[OUTSIDE_ROW],
B.[INSIDE_ROW],
B.[r_HU_vac_ns],
B.[r_HU_vac_ns_MOE],
CASE WHEN B.[r_HU_vac_ns] = 0 THEN 999 ELSE B.[r_HU_vac_ns_MOE]/B.[r_HU_vac_ns] END AS [PCT]
FROM
(SELECT
A.[OUTSIDE_ROW],
A.[INSIDE_ROW],
(A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT] AS [r_HU_vac_ns],
(1/A.[HU_VACANT]) * POWER(
CASE WHEN ((A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT]) * POWER(A.[HU_VACANT_MOE], 2) < POWER(A.[HU_VACANT_MOE], 2) + POWER(A.[HU_VACANT_SEASONAL_RECREATIONAL_MOE], 2) THEN POWER(A.[HU_VACANT_MOE], 2) + POWER(A.[HU_VACANT_SEASONAL_RECREATIONAL_MOE], 2) - (((A.[HU_VACANT] - A. [HU_vACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT]) * POWER(A.[HU_VACANT_MOE], 2))
ELSE POWER(A.[HU_VACANT_MOE], 2) + POWER(A. [HU_VACANT_SEASONAL_RECREATIONAL_MOE], 2) + (((A.[HU_VACANT] - A. [HU_vACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT]) * POWER(A.[HU_VACANT_MOE], 2)) END, 0.5) AS [r_HU_vac_ns_MOE]
FROM
(SELECT
[OUTSIDE_ROW],
[INSIDE_ROW],
SUM([ESTIMATE_1]) AS [HU_VACANT],
POWER(SUM(POWER([MOE_1], 2)), 0.5) AS [HU_VACANT_MOE],
SUM([ESTIMATE_2]) AS [HU_VACANT_SEASONAL_RECREATIONAL],
POWER(SUM(POWER([MOE_2], 2)), 0.5) AS [HU_VACANT_SEASONAL_RECREATIONAL_MOE]
FROM #TEST_TABLE
GROUP BY [OUTSIDE_ROW], [INSIDE_ROW]) A
WHERE A.[HU_VACANT] > 0) B ) C
WHERE C.[PCT] < 0.2
Every time I run it, I get the following error:
Msg 8134, Level 16, State 1, Line 533
Divide by zero error encountered.
However, if I take off the last line of code (the following WHERE clause) the code runs fine:
WHERE C.[PCT] < 0.2
Just from looking at my query, can anyone tell me what I'm doing wrong? I thought I eliminated all PCT values that were zero with the CASE WHEN statement below so this error is baffling me:
CASE WHEN B.[r_HU_vac_ns] = 0 THEN 999 ELSE B.[r_HU_vac_ns_MOE]/B.[r_HU_vac_ns] END AS [PCT]
If it helps, PCT is cast as floating point.
Thanks.
SQL Server reserves the right to rearrange calculations. That means that the calculation in a SELECT can happen before filtering occurs. This is true even when the filters are in subqueries and CTEs.
The only way to guarantee order of calculation is CASE. However, I think it is easier to just use NULLIF(), an ANSI standard function. Instead of logic like this:
(A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT] AS [r_HU_vac_ns],
do:
(A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/NULLIF(A.[HU_VACANT], 0) AS [r_HU_vac_ns],
Instead of filtering out the records using
WHERE A.[HU_VACANT] > 0
You should filter out the records at root level
having SUM([ESTIMATE_1]) > 0
You could also use nullif function but that will result producing NULL where you have zero