简体   繁体   中英

concat with to_char

Write a SQL query to list FIRST_NAME, LAST_NAME, STATE and ZIP of Customers. STATE and ZIP should be displayed as STATE_ZIP (Ex: MO_65807)

select FIRST_NAME, LAST_NAME, concat(STATE,ZIP)state_zip
from customer;

It looks like that I have to combine state and zip within given specific form. (Ex: MO_65807). The query above shows that just combined with state, zip (Ex: MO65807). I think I need to use to_char in order to satisfy for the question. Is anybody know how to use those things at the same time?

在MYSQL中,您可以运行以下查询:

select FIRST_NAME, LAST_NAME, concat(STATE, '_', ZIP) as state_zip from customer;

您可以使用CONCAT或其他方式

select FIRST_NAME, LAST_NAME, STATE||'_'||ZIP as state_zip from customer;

Use the below script.

select FIRST_NAME, LAST_NAME, concat(concat(STATE,'_'),ZIP)state_zip
from customer;

OR

SELECT FIRST_NAME, LAST_NAME,STATE || '_' || ZIP AS state_zip
FROM customers;

there are two methods.

  1. you can use DOUBLE PIPE(||) operator

    • state || '_' || zip
  2. you can use concat inbuild function

    • concat(state,'_',zip)

Please note that if the data type are different for both columns then you have to convert both in common data type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM