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?

More than 5 years have passed since last update.

0 と 1 で保存されたフィールドを'True'、'False' の文字列で取得する。

Posted at

概要

フラグを1,0で管理している項目を、
ASP.NET のGridView のチェックボックスにそのまま読み込もうとしたら、
True,False で判定できずにエラーで落ちました。

対応として、
無理やりSQLサーバ 側でTRUE,FALSEの文字列を返すようにしました。

サーバ構成:

 Windows Server
 ASP.NET (VB.NET、ODBC接続)
 Postgresql

経緯

Google先生に聞いてみると、
REPLACEとか、Translateとかあったけど、自分の環境ではうまく動きませんでした。
何かの設定かもしれないけど、ODBC接続していて、ODBCで使えるスカラ関数の一覧を
見つけることができず、賢いやり方は諦めて、、、
えいやっとPostgreSQLに関数自作して対応しちゃったYOO

ソース

関数の作成内容(IS_TRUE)
/*------------------------------------------------------------*
* IS_TRUE
*
* 1だったらTrueの文字列を返し、それ以外はFalseを返す関数。
*
* 2013 KurokoSin
*------------------------------------------------------------*/
DROP FUNCTION IS_TRUE( BIGINT );

CREATE FUNCTION IS_TRUE( BIGINT ) RETURNS VARCHAR AS '
DECLARE
    is_one ALIAS FOR $1;
    rtn VARCHAR(5);
BEGIN
    rtn = FALSE::varchar;
    IF (is_one IS NOT NULL and is_one = 1) THEN
        rtn = TRUE::varchar;
    END IF;

    RETURN rtn;
END;
' LANGUAGE 'plpgsql'; 

使い方
select  IS_true(h.fugafuga)
  from  hogehoge h
;

標準の関数とか、もっといいやり方があれば教えて下さい。

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?