码迷,mamicode.com
首页 > 其他好文 > 详细

SQL技术内幕-7 varchar类型的数字和 int 类型的数字的比较+cast的适用

时间:2015-03-01 18:18:40      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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<y? y<z? x<z?
TRUE TRUE FALSE

 

第二种

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)<y? y<CAST(z)? x<z? CAST(x)<CAST(z)?
TRUE TRUE FALSE TRUE

SQL技术内幕-7 varchar类型的数字和 int 类型的数字的比较+cast的适用

标签:

原文地址:http://www.cnblogs.com/alphafly/p/4307258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!