0

Lets say I have a postgres table with the following data:

hostname | ip
---------+--------
a1       | 198.162.4.2
a1       | 198.162.7.5
a2       | 10.43.243.4
a3       | 10.3.1.1

I want to copy the values from table tmp to table abc in psql. I want to have an sql insert command to display the hostname_ip in hostname column like below:

hostname | ip
---------+--------
a1_198.162.4.2 | 198.162.4.2    
a1_198.162.7.5 | 198.162.7.5
a2_10.43.243.4 | 10.43.243.4
a3_10.3.1.1    | 10.3.1.1

3 Answers 3

1

You can do like below

insert into abc(hostname,ip)
select (hostname || '_' || ip) as hostname,ip from tmp;

Sign up to request clarification or add additional context in comments.

1 Comment

Do you mean update same table? Yes it is possible
1

I suggest using CONCAT_WS here:

SELECT CONCAT_WS('_', hostname, ip) AS hostname, ip
FROM yourTable;

Comments

0

SELECT CONCAT(hostname,'_',ip) as hostname,ip FROM table;

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.