操作数类型冲突:date is incompatible with smallint error in sql server
Operand type clash: date is incompatible with smallint error in sql server
我写了一个 sql 查询来获取 2006 年到 2008 年之间的员工雇用人数。这是我来自 adventurework2014.dimemployee table
的代码
SELECT YEAR(cast('HireDate' as int)), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where HireDate between 2006 and 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(HireDate)
我上面的代码显示错误
Operand type clash: date is incompatible with smallint
请帮我解决一些问题。
您错过了在 where
子句中使用 year()
:
where year(HireDate) >= 2006 and year(HireDate) <= 2008
此外,您也不需要将 cast()
函数与 year()
一起使用,因为它将 return 数字类型。
你的 SELECT
语句对我来说很奇怪,当你包含 GROUP BY
时它应该有聚合列:
SELECT YEAR(HireDate), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
WHERE year(HireDate) >= 2006 and year(HireDate) <= 2008
GROUP BY DepartmentName, YEAR(HireDate), FirstName, ParentEmployeeKey
ORDER BY YEAR(HireDate);
下面的语句将HireDate
作为字符串,不能转换为int。
cast('HireDate' as int)
理想情况下你不需要将它转换成 int,如果你在 date 上使用 YEAR
,它只会给你 int。
像下面这样更改您的查询。
SELECT YEAR(HireDate), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where YEAR(HireDate) >= 2006 and YEAR(HireDate) <= 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(HireDate)
请尝试以下操作,
SELECT YEAR(cast(HireDate as date)), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where YEAR(cast(HireDate as date)) between 2006 and 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(cast(HireDate as date))
如果您的列 'HireDate' 是日期时间字段,则不需要任何转换
我写了一个 sql 查询来获取 2006 年到 2008 年之间的员工雇用人数。这是我来自 adventurework2014.dimemployee table
的代码SELECT YEAR(cast('HireDate' as int)), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where HireDate between 2006 and 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(HireDate)
我上面的代码显示错误
Operand type clash: date is incompatible with smallint
请帮我解决一些问题。
您错过了在 where
子句中使用 year()
:
where year(HireDate) >= 2006 and year(HireDate) <= 2008
此外,您也不需要将 cast()
函数与 year()
一起使用,因为它将 return 数字类型。
你的 SELECT
语句对我来说很奇怪,当你包含 GROUP BY
时它应该有聚合列:
SELECT YEAR(HireDate), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
WHERE year(HireDate) >= 2006 and year(HireDate) <= 2008
GROUP BY DepartmentName, YEAR(HireDate), FirstName, ParentEmployeeKey
ORDER BY YEAR(HireDate);
下面的语句将HireDate
作为字符串,不能转换为int。
cast('HireDate' as int)
理想情况下你不需要将它转换成 int,如果你在 date 上使用 YEAR
,它只会给你 int。
像下面这样更改您的查询。
SELECT YEAR(HireDate), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where YEAR(HireDate) >= 2006 and YEAR(HireDate) <= 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(HireDate)
请尝试以下操作,
SELECT YEAR(cast(HireDate as date)), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where YEAR(cast(HireDate as date)) between 2006 and 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(cast(HireDate as date))
如果您的列 'HireDate' 是日期时间字段,则不需要任何转换