SQL技術裏話-7 varrharタイプの数字とintタイプの数字の比較+castの適用

966 ワード

DECLARE @x VARCHAR(10);

DECLARE @y INT;

DECLARE @z VARCHAR(10);



SET @x =  '1000';

SET @y =  '2000';

SET @z = '+3000';

--第一種類
SELECT

  CASE WHEN @x < @y THEN 'TRUE' ELSE 'FALSE' END AS [x<y?],

  CASE WHEN @y < @z THEN 'TRUE' ELSE 'FALSE' END AS [y<z?], 

  CASE WHEN @x < @z THEN 'TRUE' ELSE 'FALSE' END AS [x<z?]

結果は
x 
第二種
SELECT

  CASE WHEN @x < @y THEN 'TRUE' ELSE 'FALSE' END AS [CAST(x)<y?],

  CASE WHEN @y < @z THEN 'TRUE' ELSE 'FALSE' END AS [y<CAST(z)?], 

  CASE WHEN @x < @z THEN 'TRUE' ELSE 'FALSE' END AS [x<z?],

  CASE WHEN CAST(@x AS INT) < CAST(@z AS INT)

       THEN 'TRUE' ELSE 'FALSE' END AS [CAST(x)<CAST(z)?]

結果は
CAST(x)