1

Table:

CREATE TABLE venue (id INTEGER DEFAULT NEXTVAL('venue_id_seq'), building_code     building_code, floorNo int,  roomNo int,  width int, length int);

The function:

 CREATE or REPLACE FUNCTION roomCode(_id int ) RETURNS text AS 
$$
 SELECT building_code + floorNo + roomNo FROM venue as v WHERE _id = v.id;
$$ LANGUAGE SQL;

How can I Concatenate certain attributes together?

2
  • SQL query can only give you tuples. If you want to concatenate the results, you have to do it by hand or use (for example) Python. Why do you want to concatenate these numbers? Commented Aug 15, 2013 at 10:16
  • I want "building_code", "floorNo" and "roomNo" to concatenate into text that the function can return. For example: IT 4 - 2. Where "building_code = IT", "floorNo = 4" and "roomNo = 2". Commented Aug 15, 2013 at 10:24

2 Answers 2

2

I dont know if this is the best way to do it, But I think it will get the job done:

You can use SELECT TEXTCAT( param 1, param 2) If you want more than one attribute then you have to use a TEXTCAT within a TEXTCAT such as:

TEXTCAT( param 1, TEXTCAT(param 2, param3))

If your attributes are not of type text already then you will have to cast them to text as so:

attribute::text

So to answer your question in a whole:

SELECT TEXTCAT(building_code::text, TEXTCAT( floorNo::text , roomNo::text))
Sign up to request clarification or add additional context in comments.

Comments

2

you can use string concatenation operator - ||

CREATE or REPLACE FUNCTION roomCode(_id int ) RETURNS text AS 
$$
 SELECT building_code || ' ' || floorNo::text || ' - ' || roomNo::text FROM venue as v WHERE _id = v.id;
$$ LANGUAGE SQL;

sql fiddle demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.