0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQL ServerでNLL、空文字を変換したい

Posted at

1. はじめに

  • SQL ServerでSQLの値を取得する時に、NULL、ブランクを別の値に変換したい

2. 開発環境

  • SQL Server 2022

3. NULL値を変換する

3.1. ISNULL関数

  • NULL値を固定の文字に変換する
例 UserNameがNULL値の場合、ブランクを取得する
SELECT 
    UserId 
    , ISNULL(UserName, '') 
FROM User;

3.2. COALESCE関数

  • NULL値の場合、別のカラムの値に変換する
例 Career1がNULL値の場合、Career2を取得する
SELECT
    UserId
    , UserName
    , COALESCE(Career1, Career2) AS Career
FROM User;

4. ブランクを変換する

4.1. CASE関数

  • 条件を記載して取得項目を変換する
例 FirstNameがブランクの場合、LastNameを取得する
SELECT 
    CASE WHEN FirstName <> '' FirstName
     ELSE LastName END AS Name
FROM User;

5. 参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?