Data conversion or data mapping error in SQL - sql

This is my WinSQL query that I try and execute.
SELECT ezait6 AS "MO No",
vhitno AS "Item No",
vhrefd AS "Finish Date",
vhmaqa AS "Manuf Qty",
vhmaun AS "U/M"
FROM m3edbtest.mwohed,
m3edbtest.cinacc
WHERE ezcono = 1
AND vhcono = ezcono
AND vhrefd >= '20170801'
AND vhrefd <= '20170831'
AND vhmfno = ezait6;
--Different Data Types
--vhmfno ==> Integer
--ezait6 ==> nchar(8)
After I run my query, I got below errors:
-- Error : SQL0802 - Data conversion or data mapping error.
I suspect that it is beclose of different data type
I have a nvarchar column in one of my table "ezait6".
So how should I convert that values to INT type.

You can check first if the content of the varchar field is numeric and query only on those:
SELECT ezait6 AS "MO No",
vhitno AS "Item No",
vhrefd AS "Finish Date",
vhmaqa AS "Manuf Qty",
vhmaun AS "U/M"
FROM m3edbtest.mwohed,
m3edbtest.cinacc
WHERE IsNumeric(ezait6)=1
AND ezcono = 1
AND vhcono = ezcono
AND vhrefd >= '20170801'
AND vhrefd <= '20170831'
AND vhmfno = CONVERT(INT,ezait6);

Related

How to change status based on date in another text box on form - Access

Im making a training database.
Im looking to change a textbox value on a form based on a date in another textbox.
I have the following:
Refresher Period - TxtRef
Participation Date - TxtPart
Refresher Date - TxtRefDate
Status - TxtStatus
I want status to update to either - In Date, Expired or Expiring based on the following rules applying to the date in TxtRefDate.
Value < Now() + 60 "Expiring"
Value < Now() "Expired"
Value > Now() + 60 "In Date"
Create a small helper function:
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
Select Case DateDiff("d", Date, RefDate)
Case > 60
Description = "In date"
Case > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
Status = Description
End Function
Now, set the ControlSource of txtStatus to:
=Status([TxtRefDate])
I think you're looking for something like that ?
If TxtRefDate.Value < Now() Then
TxtStatus.Value = "Expired"
Else:
If TxtRefDate.Value < Now() + 60 Then
TxtStatus.Value = "Expiring"
Else:
TxtStatus.Value = "In Date"
End If
End If

Convert INNER JOIN to LEFT JOIN - Sql

i have a query which returns ~ 97k records because i am using an inner join. Now i want to use left join to show also the null records on the right table(should be about 300k records). This is the actual query(INNER JOIN):
select artnr as "Article number", ardbez1 as "Article description ", arpadrnr as "Supplier code", cd.ADPPHYSLAND as "Country of this supplier code", p.adrnr as "Parent supplier code", p.adrkname as "Name of parent", pd.ADPPHYSLAND as "Country of parent ", arppreis as "Price", arpwae as "Unit", arzwert as "COO", atatarnr as "HS Code"
from mic_ccs_artikel, mic_ccs_artikeldetail, mic_ccs_artikelpreis, mic_dna_adressen c, mic_dna_adrdetail cd, mic_dna_adressen p, mic_dna_adrrelation, mic_dna_adrdetail pd, mic_ccs_artikelzoll, mic_ccs_artikeltarif
where artsid = ardartsid
and arddatstart <= sysdate
and nvl(arddatend, '01.01.4000') >= sysdate
and artsid = arpartsid
and ARPPREISART = 'VP'
and arpdatstart <= sysdate
and nvl(arpdatend, '01.01.4000') >= sysdate
and c.adrsid = cd.adpadrsid
and arpadrnr = c.adrnr
and cd.adpdatvon <= sysdate
and nvl(cd.adpdatbis, '01.01.4000') >= sysdate
and c.adrmandant = 'S1'
and c.adrwerk = 'V1'
and p.adrmandant = 'S1'
and p.adrwerk = 'V1'
and p.adrsid = arlrelsid1
and c.adrsid = arlrelsid2
and p.adrsid = pd.adpadrsid
and pd.adpdatvon <= sysdate
and nvl(pd.adpdatbis, '01.01.4000') >= sysdate
and artsid = arzartsid
and arztyp = 'URLD'
and arzadrnr = c.adrnr
and arzdatstart <= sysdate
and nvl(arzdatend, '01.01.4000') >= sysdate
and artsid = ataartsid
and ATAREGION = 'SE'
and atatarart='EXPORT'
and atadatstart <= sysdate
and nvl(atadatend, '01.01.4000') >= sysdate
;
Then i tried to "convert" it to LEFT JOIN, but it's always showing the same result:
select artnr as "Article number", ardbez1 as "Article description ", arpadrnr as "Supplier code", cd.ADPPHYSLAND as "Country of this supplier code", p.adrnr as "Parent supplier code", p.adrkname as "Name of parent", pd.ADPPHYSLAND as "Country of parent ", arppreis as "Price", arpwae as "Unit", arzwert as "COO", atatarnr as "HS Code"
from mic_ccs_artikel, mic_ccs_artikeldetail, mic_ccs_artikelpreis, mic_dna_adressen c, mic_dna_adrdetail cd, mic_dna_adressen p, mic_dna_adrrelation, mic_dna_adrdetail pd, mic_ccs_artikelzoll, mic_ccs_artikeltarif
where artsid = ardartsid(+)
and arddatstart <= sysdate
and nvl(arddatend, '01.01.4000') >= sysdate
and artsid = arpartsid(+)
and ARPPREISART = 'VP'
and arpdatstart <= sysdate
and nvl(arpdatend, '01.01.4000') >= sysdate
and c.adrsid = cd.adpadrsid(+)
and arpadrnr = c.adrnr
and cd.adpdatvon <= sysdate
and nvl(cd.adpdatbis, '01.01.4000') >= sysdate
and c.adrmandant = 'S1'
and c.adrwerk = 'V1'
and p.adrmandant = 'S1'
and p.adrwerk = 'V1'
and p.adrsid = arlrelsid1(+)
and c.adrsid = arlrelsid2
and p.adrsid = pd.adpadrsid
and pd.adpdatvon <= sysdate
and nvl(pd.adpdatbis, '01.01.4000') >= sysdate
and artsid = arzartsid(+)
and arztyp = 'URLD'
and arzadrnr = c.adrnr
and arzdatstart <= sysdate
and nvl(arzdatend, '01.01.4000') >= sysdate
and artsid = ataartsid(+)
and ATAREGION = 'SE'
and atatarart='EXPORT'
and atadatstart <= sysdate
and nvl(atadatend, '01.01.4000') >= sysdate
;
Why is it always showing the same result?
Echoing the sentiments in the comments, using a left join and a where clause can result in some unexpected behavior. See my answer here for more details as to why that happens. Basically, you should switch to using ANSI syntax and alias your tables. In your case, that would look something like
select * -- select whichever fields you want here
from mic_ccs_artikel a -- only one table in the from clause
left join mic_ccs_artikeldetail b on b.someID = a.someID -- join conditions here
left join mic_ccs_artikelpreis c
on c.someID = a.someID -- join conidition
and c.someField = 'Some Property' -- filtering here
...
The secondary join conditions removes the risk of throwing out those records in the where clause. Again, see my other answer for more details on that.

Teradata SQL Update Table "Query Error 3802 Database 'oo' does not exist"

I need help with the following query code. Currently it is giving me the 3802 query error "Database does not exist". Well I did my "from" selection query as 'oo', so that definitely exists. This worked yesterday with some minor changes to the "from" part of the query. The assistance is appreciated.
UPDATE snd_bqa.open_order_all_test
FROM (
SELECT
CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Complete",
CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Cancelled",
CASE WHEN pih.effective_start_date IS NOT NULL AND pih.product_instance_status_code = 3
THEN 'YES'
ELSE 'NO'
END AS "Service Active in PB",
c.ordernumber,
c1.order_requested_due_date,
c.creationdate AS "Order Creation Date",
c.ordersubmitdate AS "Order Submit Date",
c.lastupdateddate,
c.supptype,
m.order_status_desc,
m.phase_stage,
cus.sub_nasp_id,
cus.gch_id,
b.work_order_no,
b.itemcode,
b.instance_id,
COALESCE(b.specific_date, b.standard_date) AS "Service Req Due Date",
m.milestone,
m.milestone_desc,
cus.custlegalname,
a.account_number,
vle.vle_id,
vle.currency_cd,
cus.duns_number,
a.cle_vle_id,
CURRENT_TIMESTAMP(2) AS "Originally Loaded",
CURRENT_TIMESTAMP(2) AS "Last Upd",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN c.lastupdateddate
ELSE NULL
END) AS TIMESTAMP(2)) "Order Completed Date",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN c.lastupdateddate
ELSE NULL
end) AS TIMESTAMP(2)) "Order Cancelled Date",
pih.last_modified AS "Service Last Modified Date",
pih.effective_start_date AS "Service Effective Date"
FROM (
SELECT *
FROM edw_stg_ord_cw_vw.cwpc_basketitem
WHERE itemcode LIKE 'PR%'
AND (instance_id, lastupdateddate) IN (
SELECT instance_id, MAX(lastupdateddate)
FROM edw_stg_ord_cw_vw.cwpc_basketitem
GROUP BY instance_id
)) AS b
INNER JOIN edw_stg_ord_cw_vw.cworderinstance AS c
ON c.basket_id = b.basketid
AND c.isactive = 1
AND c.request_type = 'ORD'
LEFT JOIN (
SELECT c.ordernumber,
MAX(COALESCE(b.specific_date, b.standard_date)) order_requested_due_date
FROM edw_stg_ord_cw_vw.cworderinstance AS c
INNER JOIN edw_stg_ord_cw_vw.cwpc_basketitem AS b
ON b.basketid = c.basket_id
AND c.request_type = 'ORD'
AND c.isactive = 1
GROUP BY c.ordernumber
) AS c1
ON c1.ordernumber = c.ordernumber
LEFT JOIN edw_stg_ord_cw_vw.uno_customer AS cus
ON cus.cworderid = c.cwdocid
AND cus.oi_customer_id = c.ordering_customer_id
LEFT JOIN edw_stg_ord_cw_vw.uno_milestone AS m
ON m.milestone_id = c.milestone_id
LEFT JOIN edw_stg_ord_cw_vw.uno_account AS a
ON a.cworderid = c.cwdocid
AND b.account_id = a.oi_account_id
LEFT JOIN edw_stg_ord_cw_vw.uno_cle_vle AS vle
ON vle.cle_vle_id = a.cle_vle_id
AND c.cwdocid = vle.cworderid
INNER JOIN snd_bqa.open_order_all_test tst
ON tst."Service Instance ID" = b.instance_id
AND tst."Milestone" <> m.milestone
LEFT JOIN edw_pb_stg_vw.product_instance_history AS pih
ON pih.general_5 = tst."Service Instance ID"
WHERE cus.sub_nasp_id NOT IN ('19HNYO', '23MAXA', '19HNYR', '10INTD')
AND COALESCE(b.specific_date, b.standard_date, c1.order_requested_due_date) BETWEEN CURRENT_DATE - 60 AND CURRENT_DATE - 30
) oo
SET "Order Complete" = oo."Order Complete",
"Order Cancelled" = oo."Order Cancelled",
"Service Active in PB" = oo."Service Active in PB",
"Service Order Number" = oo.ordernumber,
"Order Requested Due Date" = oo.order_requested_due_date,
"Order Last Updated" = oo.lastupdateddate,
"Order Creation Date" = oo."Order Creation Date",
"Order Submit Date" = oo."Order Submit Date",
"Order Supp Type" = oo.c.supptype,
"Order Status" = oo.order_status_desc,
"Phase Stage" = oo.phase_stage,
"Milestone" = oo.milestone,
"Milestone Description" = oo.milestone_desc,
"NASP ID" = oo.sub_nasp_id,
"GCH ID" = oo.gch_id,
"Work Order Numbers" = oo.work_order_no,
"Product Code" = oo.itemcode,
"Service Requested Due Date" = oo."Service Req Due Date",
"Customer Name" = oo.custlegalname,
"Account Number" = oo.account_number,
"VLE ID" = oo.vle_id,
"Currency Code" = oo.currency_cd,
"DUNs Number" = oo.duns_number,
"CLE VLE ID" = oo.cle_vle_id,
"Last Record Update" = oo."Last Upd",
"Order Completed Date" = oo."Order Completed Date",
"Order Cancelled Date" = oo."Order Cancelled Date",
"Service Last Modified Date" = oo."Service Last Modified Date",
"Service Effective Date" = oo."Service Effective Date"
WHERE open_order_all_test."Service Instance ID" = oo.instance_id
;
I found the issue. I had oo.c.supptype, when it should have just been oo.supptype
This caused Teradata to think 'oo' was referring to a database. case closed.

Issue with CASE statement in a calculated column

I have a SELECT statement which works fine up until the CASE part, I think it's just a case of syntax error and the lead time bit. I am using Oracle APEX.
I get the error;
ORA-00923: FROM keyword not found where expected
select
supplier.companyname as "Supplier",
purchaseorder.orderdate as "Order Date",
purchaseorder.duedate as "Due Date",
popayment.datedelivered as "Delivered",
(popayment.paymentdate - popayment.datedelivered) - 31 as paidontime,
/* this is the same as the next one (this works fine) */
(popayment.datedelivered - purchaseorder.duedate) - 1 as deleiverylate,
/* trying to advance the previous query (this does not work) */
popayment.datedelivered - purchaseorder.duedate -1,
case when "lead time" <0 then 'early'
when "lead time" >0 then 'late'
else 'on time'
end as "lead time";
from supplier, purchaseorder, suppliercontact, popayment
where
purchaseorder.SUPPLIERCONTACTID2 = suppliercontact.suppliercontactid
and
supplier.supplierid = suppliercontact.supplierid
and
purchaseorder.purchaseorderid = popayment.purchaseorderid
order by COMPANYNAME
Thanks for any help
Dangerous Brian
Is this what you want?
SELECT supplier.companyname AS "Supplier",
purchaseorder.orderdate AS "Order Date",
purchaseorder.duedate AS "Due Date",
popayment.datedelivered AS "Delivered",
(popayment.paymentdate - popayment.datedelivered) - 31 AS paidontime,
/* this is the same as the next one (this works fine) */
(popayment.datedelivered - purchaseorder.duedate) - 1 AS deleiverylate,
/* trying to advance the previous query (this does not work) */
CASE
WHEN popayment.datedelivered - purchaseorder.duedate -1 <0
THEN 'early'
WHEN popayment.datedelivered - purchaseorder.duedate -1 >0
THEN 'late'
ELSE 'on time'
END AS "lead time"
FROM supplier,
purchaseorder,
suppliercontact,
popayment
WHERE purchaseorder.SUPPLIERCONTACTID2 = suppliercontact.suppliercontactid
AND supplier.supplierid = suppliercontact.supplierid
AND purchaseorder.purchaseorderid = popayment.purchaseorderid
ORDER BY COMPANYNAME
You should delete ; at end as "lead time";

Complex Query Optimization

Running Postres 7.4 (yep upgrading)
This is a view query and it's used a lot but takes longer than I would like. Any Optimization Suggestions? Any field with a Id in the name is indexed
SELECT
db_tbl_1.field_id, s."Field ID" AS foreign_field_id,
s."Field Name" AS field_name,
CASE
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' THEN 'Field Label 1'
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' THEN 'Field Label 1'
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' THEN 'Field Label 1'
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' THEN 'Field Label 1'
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76' THEN 'Field Label 2'
END AS new_field, s.field_1,
db_tbl_2.field_2, db_tbl_1.field_2_a,
db_tbl_1.field_3, db_tbl_1.field_4,
db_tbl_1.field_5, db_tbl_1.field_6, db_tbl_1.field_date_1,
db_tbl_1.field_7, db_tbl_2.field_8 AS new_field_name,
s."Field Date" AS field_date, db_tbl_1.created_date,
CASE
WHEN (((DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH') - "Field Date" > 60) AND
((DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH') - "Field Date" <= 92))
THEN (DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH')
-- the most days in a three month span can be 92
ELSE (DATE_TRUNC('month', "Field Date") + INTERVAL '3 MONTH')
END AS next_date
FROM db_schema_1.db_tbl_1, db_schema_1.db_tbl_2, "Table S" AS s
WHERE db_tbl_1.program_level_id = db_tbl_2.id
AND db_tbl_1.field_id = s."Another ID"
AND (CASE
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' THEN 1
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' THEN 1
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' THEN 1
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' THEN 1
WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76' THEN 1
ELSE 0
END) = 1
Try to change the last statement, where you are using the case statement because the query will evaluate all of them. and then check if is equals to 1, you should try and "OR" statement, because if one is correct, it will not evaluate the other alternatives. It should help you a bit. Something like this.
AND ( SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' OR
SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' OR
SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' OR
SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' OR
SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76'
)