简体   繁体   English

与to_char结合

[英]concat with to_char

Write a SQL query to list FIRST_NAME, LAST_NAME, STATE and ZIP of Customers. 编写SQL查询以列出FIRST_NAME, LAST_NAME, STATE客户的FIRST_NAME, LAST_NAME, STATEZIP STATE and ZIP should be displayed as STATE_ZIP (Ex: MO_65807) STATEZIP应显示为STATE_ZIP(例如: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). (例如:MO_65807)。 The query above shows that just combined with state, zip (Ex: MO65807). 上面的查询显示仅与状态zip组合(例如:MO65807)。 I think I need to use to_char in order to satisfy for the question. 我想我需要使用to_char来满足这个问题。 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 您可以使用DOUBLE PIPE(||)运算符

    • state || 状态|| '_' || '_'|| zip 压缩
  2. you can use concat inbuild function 您可以使用concat inbuild函数

    • concat(state,'_',zip) concat(状态,'_',zip)

Please note that if the data type are different for both columns then you have to convert both in common data type. 请注意,如果两列的数据类型都不相同,则必须将两者转换为通用数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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