2016-10-17 3 views
1

У меня есть ниже запрос SQL в моем кодеКак разрешить ошибку даты Informix?

String sql = "select t.card_num, \n" + 
    " right(t.card_num,4) as CardLast4 \n" + 
    ",t.invoice_trim as Ticket_number \n" + 
    ",t.pos_date \n" + 
    ",tiu.info as Vehicle_number \n" + 
    ",t.contract_id \n" + 
    ",tio.info as Odometer \n" + 
    ",l.address_1 \n" + 
    "\n" + 
    ",CASE -- different for CAN vs USA query --Field #7 \n" + 
    " when l.src_country = 'USA' then prodUSA.description \n" + 
    " when l.src_country = 'CAN' then prodCAN.description \n" + 
    " else '*** ERROR ***' \n" + 
    " END as product_name \n" + 
    " \n" + 
    "\n" + 
    " ,tl.qty --Field #8 \n" + 
    "\n" + 
    ",CASE --different for CAN vs USA --Field #9 \n" + 
    " when l.src_country = 'USA' then tl.ppu \n" + 
    "else tlb.base_net_ppu end as base_ppu \n" + 
    "\n" + 
    " ,tltf.tax_rate as fet_rate \n" + 
    ",tltp.tax_rate as pft_rate \n" + 
    "\n" + 
    ",CASE \n" + 
    "when l.src_country = 'USA' then tl.ppu \n" + 
    "else (tlb.base_net_ppu + tlb.unit_cpl_rate) end as net_ppu \n" + 
    "\n" + 
    ",tltg.amount as gsthstfnt \n" + 
    ",t.pref_total as net_amount \n" + 
    ",t.location_id as location_number \n" + 
    ",t.trans_date \n" + 
    ",l.city as City \n" + 
    ",l.state as Province \n" + 
    ",t.carrier_id as Account_number \n" + 
    ",cm.second_line as Customer_Field_1 \n" + 
    ",tmp.trans_meta_data as policy \n" + 
    ",tltv.tax_rate as MFT_rate \n" + 
    ",tltct.tax_rate as Carbon_tax_rate \n" + 
    ",tltpst.amount as pst_amount \n" + 
    ",currency \n" + 
    "\n" + 
    "from transaction as t \n" + 
    "left outer join contract as c \n" + 
    "on t.contract_id = c.contract_id \n" + 
    "left outer join trans_info as tiu \n" + 
    "on t.trans_id = tiu.trans_id \n" + 
    "and tiu.type = \"UNIT\" \n" + 
    "left outer join trans_info as tio \n" + 
    "on t.trans_id = tio.trans_id \n" + 
    "and tio.type = \"ODRD\" \n" + 
    "left outer join location as l \n" + 
    "on t.location_id = l.location_id \n" + 
    "left outer join trans_line as tl \n" + 
    "on t.trans_id = tl.trans_id \n" + 
    "\n" + 
    "left outer join products as prodUSA \n" + 
    "on prodUSA.num = tl.num and prodUSA.fps_partner = 'TCH' \n" + 
    "left outer join products as prodCAN \n" + 
    "on prodCAN.num = tl.num and prodCAN.fps_partner = 'IMPERIAL' \n" + 
    "\n" + 
    "left outer join trans_line_baseppu as tlb \n" + 
    "on t.trans_id = tlb.trans_id \n" + 
    "\n" + 
    "\n" + 
    " \n" + 
    "left outer join trans_line_tax as tltf \n" + 
    "on t.trans_id = tltf.trans_id \n" + 
    "and tltf.tax_cd = \"FET\" \n" + 
    "and tltf.gross_net_flag = \"N\" \n" + 
    "and tltf.exempt_flag = \"N\" \n" + 
    "\n" + 
    "\n" + 
    "left outer join trans_line_tax as tltp \n" + 
    "on t.trans_id = tltp.trans_id \n" + 
    "and tltp.tax_cd = \"PFT\" \n" + 
    "and tltp.gross_net_flag = \"N\" \n" + 
    "and tltp.exempt_flag = \"N\" \n" + 
    "\n" + 
    "left outer join trans_line_tax as tltg \n" + 
    "on t.trans_id = tltg.trans_id \n" + 
    "and tltg.tax_cd IN (\"GST\",\"HST\",\"FNT\") \n" + 
    "and tltg.gross_net_flag = \"N\" \n" + 
    "and tltg.exempt_flag = \"N\" \n" + 
    "\n" + 
    "left outer join card_misc as cm \n" + 
    "on t.card_num = cm.card_num \n" + 
    "and t.carrier_id = cm.carrier_id \n" + 
    "\n" + 
    "left outer join trans_meta as tmp \n" + 
    "ON t.trans_id = tmp.trans_id \n" + 
    "and tmp.trans_meta_type_id = \"CPOL\" \n" + 
    "\n" + 
    "left outer join trans_line_tax as tltv \n" + 
    "on t.trans_id = tltv.trans_id \n" + 
    "and tltv.tax_cd = \"VTT\" \n" + 
    "and tltv.gross_net_flag = \"N\" \n" + 
    "and tltv.exempt_flag = \"N\" \n" + 
    "\n" + 
    "left outer join trans_line_tax as tltct \n" + 
    "on t.trans_id = tltct.trans_id \n" + 
    "and tltct.tax_cd = \"VTT\" \n" + 
    "and tltct.gross_net_flag = \"N\" \n" + 
    "and tltct.exempt_flag = \"N\" \n" + 
    "\n" + 
    "left outer join trans_line_tax as tltpst \n" + 
    "on t.trans_id = tltpst.trans_id \n" + 
    "and tltpst.tax_cd in (\"PST\",\"QST\") \n" + 
    "and tltpst.gross_net_flag = \"N\" \n" + 
    "and tltpst.exempt_flag = \"N\" \n" + 
    " where t.trans_date between "+ getParam("jrs.param$p_startDate") +" and "+ getParam("jrs.param$p_endDate")+"\n" + 
    "\n" + 
    "\n"; 

Ниже приведен результат запроса, который я вижу, когда я отладки мой код:

select t.card_num, 
right(t.card_num,4) as CardLast4 
,t.invoice_trim as Ticket_number 
,t.pos_date 
,tiu.info as Vehicle_number 
,t.contract_id 
,tio.info as Odometer 
,l.address_1 

,CASE -- different for CAN vs USA query --Field #7 
    when l.src_country = 'USA' then prodUSA.description 
    when l.src_country = 'CAN' then prodCAN.description 
    else '*** ERROR ***' 
    END as product_name 
,tl.qty --Field #8 

,CASE --different for CAN vs USA --Field #9 
when l.src_country = 'USA' then tl.ppu 
else tlb.base_net_ppu end as base_ppu 

,tltf.tax_rate as fet_rate 
,tltp.tax_rate as pft_rate 

,CASE 
when l.src_country = 'USA' then tl.ppu 
else (tlb.base_net_ppu + tlb.unit_cpl_rate) end as net_ppu 

,tltg.amount as gsthstfnt 
,t.pref_total as net_amount 
,t.location_id as location_number 
,t.trans_date 
,l.city as City 
,l.state as Province 
,t.carrier_id as Account_number 
,cm.second_line as Customer_Field_1 
,tmp.trans_meta_data as policy 
,tltv.tax_rate as MFT_rate 
,tltct.tax_rate as Carbon_tax_rate 
,tltpst.amount as pst_amount 
,currency 

from transaction as t 
left outer join contract as c 
on t.contract_id = c.contract_id 
left outer join trans_info as tiu 
on t.trans_id = tiu.trans_id 
and tiu.type = "UNIT" 
left outer join trans_info as tio 
on t.trans_id = tio.trans_id 
and tio.type = "ODRD" 
left outer join location as l 
on t.location_id = l.location_id 
left outer join trans_line as tl 
on t.trans_id = tl.trans_id 

left outer join products as prodUSA 
on prodUSA.num = tl.num and prodUSA.fps_partner = 'TCH' 
left outer join products as prodCAN 
on prodCAN.num = tl.num and prodCAN.fps_partner = 'IMPERIAL' 

left outer join trans_line_baseppu as tlb 
on t.trans_id = tlb.trans_id 



left outer join trans_line_tax as tltf 
on t.trans_id = tltf.trans_id 
and tltf.tax_cd = "FET" 
and tltf.gross_net_flag = "N" 
and tltf.exempt_flag = "N" 


left outer join trans_line_tax as tltp 
on t.trans_id = tltp.trans_id 
and tltp.tax_cd = "PFT" 
and tltp.gross_net_flag = "N" 
and tltp.exempt_flag = "N" 

left outer join trans_line_tax as tltg 
on t.trans_id = tltg.trans_id 
and tltg.tax_cd IN ("GST","HST","FNT") 
and tltg.gross_net_flag = "N" 
and tltg.exempt_flag = "N" 

left outer join card_misc as cm 
on t.card_num = cm.card_num 
and t.carrier_id = cm.carrier_id 

left outer join trans_meta as tmp 
ON t.trans_id = tmp.trans_id 
and tmp.trans_meta_type_id = "CPOL" 

left outer join trans_line_tax as tltv 
on t.trans_id = tltv.trans_id 
and tltv.tax_cd = "VTT" 
and tltv.gross_net_flag = "N" 
and tltv.exempt_flag = "N" 

left outer join trans_line_tax as tltct 
on t.trans_id = tltct.trans_id 
and tltct.tax_cd = "VTT" 
and tltct.gross_net_flag = "N" 
and tltct.exempt_flag = "N" 

left outer join trans_line_tax as tltpst 
on t.trans_id = tltpst.trans_id 
and tltpst.tax_cd in ("PST","QST") 
and tltpst.gross_net_flag = "N" 
and tltpst.exempt_flag = "N" 
where t.trans_date between 2016-09-26 and 2016-10-10 

Теперь, когда я исполняю результат запроса в моем редакторе SQL , я получаю SQLException:

"ERROR java.sql.SQLException: It is not possible to convert between the specified types" 

может кто-то пожалуйста, помогите мне, я пытался ставить кавычки вокруг поля даты. Но это не сработало.

+1

Это не совсем MCVE ([Минимальный, полный, проверяемый пример] (http://stackoverflow.com/help/MCVE)). Попробуйте упростить SQL. –

ответ

3

Пока не соглашаясь с комментарием Джонатана Леффлера о фактической решаемой затемняется всего шума, я думаю, ваша проблема эта линия:

" where t.trans_date between "+ getParam("jrs.param$p_startDate") +" and "+ getParam("jrs.param$p_endDate")+"\n" 

, который разбирает это:

where t.trans_date between 2016-09-26 and 2016-10-10 

Дата поля должны быть заключены в кавычки, чтобы они не интерпретировались как арифметические (2016 - 9 - 26 = 1981), поэтому ваш предикат снижается до:

where t.trans_date between 1981 and 1996 

... явно не то, что вы хотите, и объясняет жалобу сервера на типы данных. Так попробуйте добавить кавычки экранированы:

" where t.trans_date between \""+ getParam("jrs.param$p_startDate") +"\" and \""+ getParam("jrs.param$p_endDate")+"\"\n" 

Как и в сторону, Informix агностик о одинарные или двойные кавычки, так что вы можете сделать свой код намного более удобным для чтения с помощью одинарных кавычек, где вы в настоящее время получили бежал дважды -кавычки. Другие двигатели, которые суетливы по этому поводу (например, SQL Server), ожидают, что кавычки для строк в любом случае.

+0

"* Другие двигатели, которые смущают это (например, SQL Server), ожидают, что одиночные кавычки для строк в любом случае *" - одиночные кавычки для строковых констант определяются (и повторно) стандартом SQL (двойные кавычки предназначены только для идентификаторов) –

 Смежные вопросы

  • Нет связанных вопросов^_^