SQL Server:将 NULL 值设置为今天的值 [英] SQL Server: set NULL value to today's value

查看:67
本文介绍了SQL Server:将 NULL 值设置为今天的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 数据库中有一个列 EntryDate.

I have a column EntryDate in a SQL Server database.

如果查询中未提供值,如何设置 NULL 值以使用今天的日期(服务器时间)填充它?

How can I set a NULL value to fill it with today's date (server time) if value was not provided in a query?

推荐答案

禁止列上的空值并在 getdate()

/*Deal with any existing NULLs*/
UPDATE YourTable SET EntryDate=GETDATE() WHERE EntryDate IS NULL

/*Disallow NULLs*/
ALTER TABLE YourTable ALTER COLUMN EntryDate DATE NOT NULL

/*Add default constraint*/
ALTER TABLE YourTable ADD CONSTRAINT
    DF_YourTable_EntryDate DEFAULT GETDATE() FOR EntryDate

这篇关于SQL Server:将 NULL 值设置为今天的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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