Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 需要在不添加结果的情况下将两个日期部分合并到新的日期字段中_Sql_Sql Server_Sql Server 2008_Data Structures_Dataset - Fatal编程技术网

Sql 需要在不添加结果的情况下将两个日期部分合并到新的日期字段中

Sql 需要在不添加结果的情况下将两个日期部分合并到新的日期字段中,sql,sql-server,sql-server-2008,data-structures,dataset,Sql,Sql Server,Sql Server 2008,Data Structures,Dataset,我试图得到两个日期部分,日和年,在不添加结果的情况下合并成一个新的数据字段。我不知道我做错了什么,任何帮助都将不胜感激 sELECT Convert(varchar,dbo.tblRpt291_AgingSnapshot.ReportDate)as DATEINT, Datepart(DAY,dbo.tblRpt291_AgingSnapshot.ReportDate)+ ''+''+DATEPART(YEAR,dbo.tblRpt291_AgingSnapshot.ReportDate)

我试图得到两个日期部分,日和年,在不添加结果的情况下合并成一个新的数据字段。我不知道我做错了什么,任何帮助都将不胜感激

sELECT   Convert(varchar,dbo.tblRpt291_AgingSnapshot.ReportDate)as DATEINT, Datepart(DAY,dbo.tblRpt291_AgingSnapshot.ReportDate)+ ''+''+DATEPART(YEAR,dbo.tblRpt291_AgingSnapshot.ReportDate) as Date, dbo.tblRpt291_AgingSnapshot.ReportDate, First.Age AS AvgAge, First.CountOfCalls AS Inventory
FROM         dbo.tblRpt291_AgingSnapshot LEFT OUTER JOIN
                          (SELECT     ReportDate, AVG(CAST(Age AS Numeric)) AS Age, SUM(CAST(CountOfCalls AS Numeric)) AS CountOfCalls
FROM         dbo.tblRpt291_AgingSnapshot AS tblRpt291_AgingSnapshot_1
WHERE     (Client = 'PEMS') AND (WorkableStatus = 'Workable') AND (Worktype IN ('Individual PCP', 'Group PCP'))
GROUP BY ReportDate) AS First ON dbo.tblRpt291_AgingSnapshot.ReportDate = First.ReportDate
WHERE     (MONTH(dbo.tblRpt291_AgingSnapshot.ReportDate) = 12) AND (YEAR(dbo.tblRpt291_AgingSnapshot.ReportDate) in (2013,2014))
GROUP BY dbo.tblRpt291_AgingSnapshot.ReportDate, First.Age, First.CountOfCalls
order by dbo.tblRpt291_AgingSnapshot.ReportDate ​
在连接之前,您需要将这两个部分转换为字符串,这样它们就不会相加,因为
DATEPART()
返回的是一个整数:

, CAST(Datepart(DAY,dbo.tblRpt291_AgingSnapshot.ReportDate) AS VARCHAR(5))+CAST(DATEPART(YEAR,dbo.tblRpt291_AgingSnapshot.ReportDate) AS VARCHAR(5)) as Date
此外,表别名将有助于提高可读性,我建议避免使用关键字作为别名:

SELECT   Convert(varchar,a.ReportDate)as DATEINT
       , CAST(Datepart(DAY,a.ReportDate) AS VARCHAR(5))+CAST(DATEPART(YEAR,a.ReportDate) AS VARCHAR(5)) as Dt
       , a.ReportDate
       , b.Age AS AvgAge
       , b.CountOfCalls AS Inventory
FROM         dbo.tblRpt291_AgingSnapshot a 
LEFT OUTER JOIN     (SELECT     ReportDate, AVG(CAST(Age AS Numeric)) AS Age, SUM(CAST(CountOfCalls AS Numeric)) AS CountOfCalls
                     FROM         dbo.tblRpt291_AgingSnapshot AS tblRpt291_AgingSnapshot_1
                     WHERE     (Client = 'PEMS') AND (WorkableStatus = 'Workable') AND (Worktype IN ('Individual PCP', 'Group PCP'))
                     GROUP BY ReportDate) AS b
    ON a.ReportDate = b.ReportDate
WHERE     (MONTH(a.ReportDate) = 12) AND (YEAR(a.ReportDate) in (2013,2014))
GROUP BY a.ReportDate, b.Age, b.CountOfCalls
order by a.ReportDate ​

您需要转换datepart的-SQL将它们视为整数,您希望它们的行为类似于文本

例如:

select cast(Datepart(DAY,getdate()) AS varchar(2)) + 

cast(DATEPART(YEAR,getdate()) AS varchar(4)) as Date

将getdate()替换为您的列名。

是否有错误?结果不正确?此外,如果使用表别名,您的查询将更容易阅读。是的,当我将其合并时,它看起来好像年份已结束,例如报告日期年份为2013合并后的年份显示为2014