在日期时间创建一个计算列 [英] Create a computed column on a datetime

查看:68
本文介绍了在日期时间创建一个计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2005表中有一个可为空的DateTime列,称为DateTimeDeleted。

I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.

我希望有一个BIT类型计算列,如果DateTimeDeleted为不是null,否则为0。但是,我似乎无法正确理解公式的语法。

I'd like to have a BIT type computed column that is 1 if DateTimeDeleted is not null, otherwise 0. However, I can't seem to get the syntax for the formula correct.

我尝试过:

(TsDeleted IS NULL) = 0

但是它给了我一个语法错误。

but it gives me a syntax error.

帮助! :)

推荐答案

Alter Table MyTableName 
  Add IsDeleted As 
     (Case When [DateTimeDeleted] Is Null 
       Then (0) Else (1) End)

这将输出为整数...如果您真的希望它有点,那么:

This will output as an integer... If you really want it to be a bit, then:

Alter Table MyTableName 
  Add IsDeleted As 
     cast( (Case When [DateTimeDeleted] Is Null 
       Then (0) Else (1) End) as Bit)

这篇关于在日期时间创建一个计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆