简体   繁体   English

串联查询

[英]Query with concatenation

Input Table: Occupations 输入表:职业

Name      Occupation
_____     _________
Samantha    Doctor
Julia       Actor
Maria       Actor
Meera       Singer
Ashley    Professor
Ketty     Professor
Christen    Professor
Jane        Actor
Jenny       Doctor
Priya       Singer

Output: 输出:

 Detail
__________  
Samantha(D) 
Julia(A)
Maria(A)
Meera(S)
Ashley(P)

That means output will contain name concatenated with First alphabet of each occupations. 这意味着输出将包含与每个职业的第一个字母串联的名称。

Select concat(Name,'()') from Occupations

This query gives the concatenated brackets ,but I have to add alphabet based on one's profession.What should I do to accomplish that? 这个查询给出了连接的括号,但是我必须根据自己的专业来添加字母。我该怎么做?

For SQL 2012: 对于SQL 2012:
select concat (name,'(',substring(occupation,1,1),')') from table 从表中选择concat(name,'(',substring(occupation,1,1),')')

For other versions.. 对于其他版本。
select name +'('+ substring(occupation,1,1)+')' from table 从表中选择名称+'('+ substring(occupation,1,1)+')'

Concat ignores null values,but you may want to be carefull with '+' since whole output will become null in case any of the expression is null,you can use Isnull to avoid that Concat会忽略null值,但是您可能要谨慎使用'+',因为如果任何表达式为null,则整个输出将为null,可以使用Isnull来避免这种情况

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

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