2012-03-09 58 views
1

因此,我正在處理一個查詢,該查詢將獲得申請人的first_name_txt,last_name_txt或business business_name。SQL concat和轉義字符

我在試圖連接逗號時遇到問題。

case 
when (credit_req_contr_requestor.requestor_type_id=0) 
then CONCAT(CONCAT(credit_req_contr_requestor.last_name_txt,'','') , 
credit_req_contr_requestor.first_name_txt) 
when(requestor_business.requestor_type_id=3) 
then requestor_business.business_name_txt end as applicant_name_txt 

我給兩個單引號的原因是因爲查詢由Java SQL加載運行,當它遇到一個單引號打破。

但是,當我運行這個查詢時,它給出了一個錯誤說「無效的爭論數量」。所以,我添加了三個單引號''',''',但是然後applicant_name_txt將顯示爲Smith',' John

我試過使用||而不是concat,這也是同樣的問題。

我該如何解決這個問題?如何在顯示時在這兩個字段之間添加逗號?我可以使用任何其他轉義字符?

+1

我希望你不抱怨'了mystring =「選擇...‘喇嘛’...」'不工作 - 因爲在這種情況下,你應該轉義[Java字符串字符](http://docs.oracle.com/javase/tutorial/java/data/characters.html),而不是SQL - 即''... \'bla \'...「或''''bla'...''' – Aprillion 2012-03-09 22:32:39

回答

1

如果您需要完全避免使用單引號(似乎很奇怪,如果喂SQL語句中的工具將打破包含一個字符串字面),你可以做這樣的事情

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (
    2 select 0 type_id, 'John' first_name, 'Smith' last_name, null business_name 
    3  from dual 
    4 union all 
    5 select 3, null, null, 'ACME Bolts' 
    6  from dual 
    7 ) 
    8 select (case when type_id = 0 
    9    then last_name || chr(44) || chr(32) || first_name 
10    else business_name 
11   end) 
12* from x 
SQL>/

(CASEWHENTY 
----------- 
Smith, John 
ACME Bolts 
+0

這個查詢是由java sql加載器解析的,它在遇到單引號時會將字符串縮短,因此我嘗試使用轉義字符。 – roymustang86 2012-03-09 22:02:01

+0

@ roymustang86 - 更新了我的答案。 – 2012-03-09 22:06:57

1

如何 CONCAT(credit_req_contr_requestor.last_name_txt,CHR(44))