Thusitha Wickramasinghe
Thusitha Wickramasinghe

Reputation: 1103

How to get SQL combined field result

I have table "student" in my database;

CREATE TABLE student 
(
stud_id VARCHAR(20),
fname VARCHAR(100),
lname VARCHAR(100),
PRIMARY KEY (stud_id)
);

I have data in "student" table

INSERT INTO student (stud_id, fname, lname)
VALUES ('s0001', 'foo', 'bar');

I want to find stud_id of the student who has the full name "foo bar" how to do this? I tried this

SELECT stud_id FROM student WHERE 'fname'+' '+'lname' LIKE 'fo%';

Its not error but i cant get "foo bar" stud_id. How to do this?

Upvotes: 4

Views: 47

Answers (2)

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

Try this query

SELECT stud_id FROM student WHERE Convert(varchar(150),fname+' '+lname) LIKE 'fo%';

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93754

Single quotes will make your column as string so avoid using it in columns. Also instead of + use concat function to concatenate the columns

SELECT stud_id 
FROM student 
WHERE concat(fname,' ',lname) LIKE 'fo%';

Upvotes: 2

Related Questions