1
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?

More than 3 years have passed since last update.

PostgreSQLのCASE文で日付のNULLを空文字にする

Posted at

case文を使ってデータベースに登録されている日付(DATE型)がNULLの場合に空文字に変換し,
それ以外はそのまま使います。


SELECT 
CASE HOGE_DATE
     WHEN HOGE_DATE IS NULL THEN ''
     ELSE TO_CHAR(HOGE_DATE,'YYYY/MM/DD')
END
FROM HOGE_TABLE 

CASE文ではELSEの前後で型が同じでなければならないそうなので、HOGE_DATEを文字列に型変換しています(僕は最初なぜか''を日付型に型変換しようとしてました)。
さらにTO_CHARの第2引数に日付の型を書かなければならないことにも注意です。

1
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
1
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?