当前位置: 动力学知识库 > 问答 > 编程问答 >

SQL Query to concatenate strings or add default value

问题描述:

I have to create a View that shows a field created by concatenating some other fields. The simple query that I use is this one:

SELECT

CODPROY, DESCPROY, USER, CODPROY + ' - ' + USER + ' - ' + DESCPROY AS Expr

FROM

dbo.PROY

The problem is that USER may be NULL and in this case I have to insert a default text, something like 'NOT AVAILABLE'. Can this be done in a view?

网友答案:

Use coalesce if you are using SQL Server

SELECT 
  CODPROY, 
  DESCPROY, 
  USER, 
  CODPROY + ' - ' + COALESCE(USER,'NOT AVAILABLE') + ' - ' + DESCPROY AS Expr 
FROM dbo.PROY
网友答案:

Yes, it can be done. You need to use IFNULL (mySQL) or something like it ..

CREATE VIEW foobar as 
SELECT CODPROY
      , DESCPROY
      , USER
      , CODPROY + ' - ' + IFNULL(USER,'DEFAULT') + ' - ' + DESCPROY AS Expr 
 FROM dbo.PROY;
分享给朋友:
您可能感兴趣的文章:
随机阅读: