Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

sql - Concat function is not working - invalid number of arguments

I have a table with two columns(Name, Occupation). I want to output the value in a format something like this.

Jane(A) 
Jenny(D) 
Julia(A)

Hear First one is the name and the value in brackets is the first letter of their occupation.

So far what I have done is

SELECT CONCAT(Name,SUBSTR(Occupation,1,1)) FROM OCCUPATIONS;

which output value like this

JaneS 
JennyS 
JuliaD

to get the required format I tried this

SELECT CONCAT(Name,"(",SUBSTR(Occupation,1,1),")") FROM OCCUPATIONS;

then it's throwing an error something like this.

SELECT CONCAT(Name,'(',SUBSTR(Occupation,1,1),')') FROM OCCUPATIONS * ERROR at line 1: ORA-00909: invalid number of arguments

What is the mistake that I have done and what should I do to fix it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
SELECT CONCAT(Name,"(",SUBSTR(Occupation,1,1),")") FROM OCCUPATIONS;

First, the double quotes " are used to enclose identifiers. use single quote ' to wrap a string.

Second, CONCAT accepts two params.

You could nest bunch of concats, but it's easier and cleaner to use concatenation operation ||:

SELECT Name || '('  || SUBSTR(Occupation,1,1) || ')' FROM OCCUPATIONS;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...