获取一月日期月份记录
Get January Date Month Record
我的查询需要帮助。我试图生成一个从 1 月到 2 月的列表。然而,在我的查询中,我不确定我做错了哪一部分或弄错了它唯一的 2 月记录。好心提醒 。谢谢你
set @FirstDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-2))),dateadd(m,1,getdate())),112)
set @LastDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,2,getdate()))),DATEADD(m,2,getdate())),112)
--------------------------------------------------
INSERT INTO [dbo].[ProcessLog] ([LogTime] ,[LogDescription] ,[LogRemark])
VALUES (GETDATE(), 'Import Client Info', '')
--------------------------------------------------
SET @sSQL = 'SELECT DISTINCT CHDR.CHDRNUM, ZTRN.CCDATE, CHDR.CRDATE, CLNT.CLNTNUM, CLNT.SURNAME, CLNT.GIVNAME,
CLNT.SECUITYNO, ZCLN.EMAIL from (((MPIDTA.ZTRNPF AS ZTRN
LEFT JOIN MPIDTA.CHDRPF AS CHDR ON ZTRN.RLDGACCT = CHDR.CHDRNUM AND ZTRN.EFFDATE = CHDR.CURRFROM)
LEFT JOIN MPIDTA.CLNTPF AS CLNT ON CHDR.COWNNUM = CLNT.CLNTNUM)
LEFT JOIN MPIDTA.ZCLNPF AS ZCLN ON CLNT.CLNTNUM = ZCLN.CLNTNUM)
where (ZTRN.BATCPFX = ''BA'' AND ZTRN.BATCCOY = ''1''
AND ZTRN.CNTTYPE = ''PTB'' AND ZTRN.TRANDATE >= ''20140101''
AND (ZTRN.EXPIRY_DATE BETWEEN ' + @FirstDateOfNextMth + ' AND ' + @LastDateOfNextMth + '))
AND (CHDR.MPLNUM = '''' AND CHDR.CHDRPFX = ''CH''
AND CHDR.CHDRCOY = ''1'' AND CHDR.VALIDFLAG = ''1'')'
SET @sExe = 'Insert into dbo.TPA_Client_Info Select * FROM OPENQUERY(AS400, ''' + REPLACE(@sSQL, '''', '''''') + ''')'
exec (@sExe)
问题出在@FirstDateOfNextMth
。
set @FirstDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-2))),
dateadd(m,1,getdate())),112)
结果是 20150202
因为 @FirstDateOfNextMth
是 20150202
,其中条件 ZTRN.EXPIRY_DATE
仅在 feb
个月之间过滤。试试这样改。
set @FirstDateOfNextMth = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
我的查询需要帮助。我试图生成一个从 1 月到 2 月的列表。然而,在我的查询中,我不确定我做错了哪一部分或弄错了它唯一的 2 月记录。好心提醒 。谢谢你
set @FirstDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-2))),dateadd(m,1,getdate())),112)
set @LastDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,2,getdate()))),DATEADD(m,2,getdate())),112)
--------------------------------------------------
INSERT INTO [dbo].[ProcessLog] ([LogTime] ,[LogDescription] ,[LogRemark])
VALUES (GETDATE(), 'Import Client Info', '')
--------------------------------------------------
SET @sSQL = 'SELECT DISTINCT CHDR.CHDRNUM, ZTRN.CCDATE, CHDR.CRDATE, CLNT.CLNTNUM, CLNT.SURNAME, CLNT.GIVNAME,
CLNT.SECUITYNO, ZCLN.EMAIL from (((MPIDTA.ZTRNPF AS ZTRN
LEFT JOIN MPIDTA.CHDRPF AS CHDR ON ZTRN.RLDGACCT = CHDR.CHDRNUM AND ZTRN.EFFDATE = CHDR.CURRFROM)
LEFT JOIN MPIDTA.CLNTPF AS CLNT ON CHDR.COWNNUM = CLNT.CLNTNUM)
LEFT JOIN MPIDTA.ZCLNPF AS ZCLN ON CLNT.CLNTNUM = ZCLN.CLNTNUM)
where (ZTRN.BATCPFX = ''BA'' AND ZTRN.BATCCOY = ''1''
AND ZTRN.CNTTYPE = ''PTB'' AND ZTRN.TRANDATE >= ''20140101''
AND (ZTRN.EXPIRY_DATE BETWEEN ' + @FirstDateOfNextMth + ' AND ' + @LastDateOfNextMth + '))
AND (CHDR.MPLNUM = '''' AND CHDR.CHDRPFX = ''CH''
AND CHDR.CHDRCOY = ''1'' AND CHDR.VALIDFLAG = ''1'')'
SET @sExe = 'Insert into dbo.TPA_Client_Info Select * FROM OPENQUERY(AS400, ''' + REPLACE(@sSQL, '''', '''''') + ''')'
exec (@sExe)
问题出在@FirstDateOfNextMth
。
set @FirstDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-2))),
dateadd(m,1,getdate())),112)
结果是 20150202
因为 @FirstDateOfNextMth
是 20150202
,其中条件 ZTRN.EXPIRY_DATE
仅在 feb
个月之间过滤。试试这样改。
set @FirstDateOfNextMth = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)