我对SSIS中的派生列有问题。在SSMS中,我可以使用以下代码将列设置为默认值:
(dateadd(month,datediff(month,(0),getdate())-(1),(0)))
当数据输入到数据库中时,它将以以下格式提供上个月的时间戳,例如:
2010-09-01 00:00:00.000
奇怪的是,这就是我正在寻找的,并试图使用派生列DFT复制/生成类似的内容。
到目前为止,我有:
DATEADD("mm",DATEDIFF("mm",GETDATE(),GETDATE()) - 1,GETDATE())
它成功地生成了月份,但是GETDATE()不能正确地替换原始代码中的0。
在SQL中,原始代码中的0是否表示开始日期?
任何帮助都将不胜感激。
致以敬意,
李
发布于 2010-10-08 19:00:53
这是我想你可能要找的东西。它是一个SSIS表达式,用于获取给定日期的上个月的第一天(本例中为GETDATE())。
DATEADD("mm",DATEDIFF("mm", (DT_DBTIMESTAMP)2, GETDATE()) - 1, (DT_DBTIMESTAMP)2)
我尝试模拟您的SQL版本的表达式,它确定了0日期时间和当前日期时间之间的月数。然后,它将月数添加到0日期时间。
0日期时间是什么并不是太重要,除了你想要一个月的第一天。在SQL中,0日期时间是1900-01-01 00:00:00.000,因此添加月份会自动得到该月的第一天。在SSIS表达式中,0日期时间是1899-12-30 00:00:00.000。因为您想要一个月的第一天,所以上面的表达式引用2个日期时间。因此,在表达式中,(DT_DBTIMESTAMP)2
将数字2转换为1900-01-01 00:00:00.000。
发布于 2010-10-08 12:50:14
您的第一个代码片段是将日期时间“底”到月份(将日期设为给定月份的1号,没有时间) "-1“使其成为前一个月。这段代码中所有多余的不必要的括号让我头疼,下面是等效的:
dateadd(month,datediff(month,0,getdate())-1,0)
下面是如何将datetime设置为不同单位的方法:
--Floor a datetime
DECLARE @datetime datetime;
SET @datetime = '2008-09-17 12:56:53.430';
SELECT '0 None', @datetime -- none 2008-09-17 12:56:53.430
UNION SELECT '1 Second',DATEADD(second,DATEDIFF(second,'2000-01-01',@datetime),'2000-01-01') -- Second: 2008-09-17 12:56:53.000
UNION SELECT '2 Minute',DATEADD(minute,DATEDIFF(minute,0,@datetime),0) -- Minute: 2008-09-17 12:56:00.000
UNION SELECT '3 Hour', DATEADD(hour,DATEDIFF(hour,0,@datetime),0) -- Hour: 2008-09-17 12:00:00.000
UNION SELECT '4 Day', DATEADD(day,DATEDIFF(day,0,@datetime),0) -- Day: 2008-09-17 00:00:00.000
UNION SELECT '5 Month', DATEADD(month,DATEDIFF(month,0,@datetime),0) -- Month: 2008-09-01 00:00:00.000
UNION SELECT '6 Year', DATEADD(year,DATEDIFF(year,0,@datetime),0) -- Year: 2008-01-01 00:00:00.000
ORDER BY 1
PRINT' '
PRINT 'Note that when you are flooring by the second, you will often get an arithmetic overflow if you use 0. So pick a known value that is guaranteed to be lower than the datetime you are attempting to floor'
PRINT 'this always uses a date less than the given date, so there will be no arithmetic overflow'
SELECT '1 Second',DATEADD(second,DATEDIFF(second,DATEADD(day,DATEDIFF(day,0,@datetime),0)-1,@datetime),DATEADD(day,DATEDIFF(day,0,@datetime),0)-1) -- Second: 2008-09-17 12:56:53.000
第二个代码片段不会正确地将datetime设置为月份,它只会将日期移到前一个月,并使用与给定datetime相同的日期和时间。
除此之外,我不确定你真正的要求是什么。
下面是在OP的第一个代码片段中发生的事情的细目:
select convert(datetime,0),'first datetime "0"'
select datediff(month,0,getdate()), 'months difference between the first datetime (1900-01-01) and given "getdate()"'
select datediff(month,0,getdate())-1, 'months difference between the first datetime (1900-01-01) and month previous to given "getdate()"'
select dateadd(month,datediff(month,0,getdate())-1,0), 'takes the first datetime (1900-01-01) and adds 1328 months onto that'
输出:
----------------------- ------------------
1900-01-01 00:00:00.000 first datetime "0"
----------- --------------------------------------------------------------------------------
1329 months difference between the first datetime (1900-01-01) and given "getdate()"
----------- --------------------------------------------------------------------------------------------------
1328 months difference between the first datetime (1900-01-01) and month previous to given "getdate()"
----------------------- --------------------------------------------------------------------
2010-09-01 00:00:00.000 takes the first datetime (1900-01-01) and adds 1328 months onto that
发布于 2010-10-08 13:00:58
month,0
将其向下舍入到月初。
如果第一个表达式做的是你想要的,你就不能使用它吗?或者使用第二个表达式,但将已添加的两个GETDATE()
切换为0。如果你尝试这样做,你会得到错误吗?
https://stackoverflow.com/questions/3890491
复制相似问题