sql服务中的日期函数 [英] date function in sql serve r
本文介绍了sql服务中的日期函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
insert into datef
( id , date1 , date2 , name1 )
values
( 1 , getdate() , select max(date1)+ 1 from datef else 'insert 1st Jan <Current year>' , 'name1')
如何在普通查询中使用上述陈述
OP的其他信息从下面的非解决方案中移出
我在执行pls进行排序时出现此语句错误
How can i use the above statment in normal query
OP''s additional information moved from non-solution below
I am getting error in this statement while executing pls sort it out
select max(date1)+ 1 from datef else 'insert 1st Jan '
推荐答案
如果date1
为NULL
,则MAX(date1)
始终为NULL
.您需要使用COALESCE
函数来获取不可为空的值.例如:
Ifdate1
isNULL
thenMAX(date1)
is alwaysNULL
. You need to useCOALESCE
function to get non-nullable value. For example:
DECLARE @curdate AS DATETIME
SET @curdate = GETDATE()
SELECT MAX(COALESCE(date1, @curdate)+ 1
FROM YOUR_TABLE_NAME
根据MS SQL Server使用日期和时间函数 [
Depending on MS SQL Server use date and time function[^] corresponding to your needs.
Declare @date As datetime
Select @date=MAX(date1) From datef
If(@date Is Null) Set @date=Convert(datetime, '01-Jan-'+DateName(yyyy,GetDate()))
Else Set @date=DateAdd(d,1,@date)
insert into datef
( id , date1 , date2 , name1 )
values
( 1 , getdate() , @date , 'name1')
这篇关于sql服务中的日期函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文