我正在插入公众假期以便在项目中进一步参考,假日日期是DATE类型,并且由于某种原因它必须是这种类型,我正在连接@Year但是它会引发错误 那么我怎么能想把节日放在充满活力的一年呢?
ERROR:
Msg 206, Level 16, State 2, Line 3
Operand type clash: int is incompatible with date
Msg 402, Level 16, State 1, Line 6
The data types varchar and date are incompatible in the add operator.
USE [AMS]
GO
Declare @Year date
Set @Year = (Select Year(Getdate()))
INSERT INTO [dbo].[GazettedHolidays]
Select 1, GETDATE(), 'Kashmir Day', '05 Feb' +@Year,'Kashmir Day',1
Union All
Select 1, GETDATE(), 'Pakistan Day', '23 Mar'+@Year,'Pakistan Day',1
Union All
Select 1, GETDATE(), 'Labour Day', '01 May' +@Year,'Labour Day',1
Union All
Select 1, GETDATE(), 'Independence Day', '14 Aug' +@Year,'Independence Day',1
Union All
Select 1, GETDATE(), 'Iqbal day', '09 Nov' +@Year,'Iqbal Day',1
Union All
Select 1, GETDATE(), 'Quaid-e-Azam Day', '25 Dec' +@Year,'Quaid-e-Azam Day',1
GO
怎么样?
答案 0 :(得分:1)
对于第一个问题,您需要指定表格布局。您的列类型不匹配。
要解决第二个问题,只需更改变量声明:
Declare @Year varchar(255);
Set @Year = cast(year(getdate() as varchar(255));
我建议您使用ISO标准日期格式(YYYY-MM-DD或YYYYMMDD),而不是依赖本地日期格式。
答案 1 :(得分:1)
INSERT INTO [dbo].[GazettedHolidays]
Select 1, GETDATE(), 'Kashmir Day', '05 Feb' +CAST(@Year AS VARCHAR(4)), ' Kashmir Day',1
Union All
Select 1, GETDATE(), 'Pakistan Day', '23 Mar'+CAST(@Year AS VARCHAR(4)), ' Pakistan Day',1
Union All
Select 1, GETDATE(), 'Labour Day', '01 May' +CAST(@Year AS VARCHAR(4)) , ' Labour Day',1
Union All
Select 1, GETDATE(), 'Independence Day', '14 Aug' +CAST(@Year AS VARCHAR(4)), 'Independence Day',1
Union All
Select 1, GETDATE(), 'Iqbal day', '09 Nov' +CAST(@Year AS VARCHAR(4)) , ' Iqbal Day',1
Union All
Select 1, GETDATE(), 'Quaid-e-Azam Day', '25 Dec' +CAST(@Year AS VARCHAR(4)), 'Quaid-e-Azam Day',1
GO
答案 2 :(得分:1)
我认为你需要像这样的varchar案例年:
CAST(@Year as VARCHAR(4))
所以它是这样的:
INSERT INTO [dbo].[GazettedHolidays]
Select 1, GETDATE(), 'Kashmir Day', '05 Feb' +CAST(@Year as VARCHAR(4)),'Kashmir Day',1
...
然后我认为YEAR函数返回INT而不是DATE。所以改变这个陈述:
Declare @Year date
Set @Year = (Select Year(Getdate()))
对此:
Declare @Year int
Set @Year = (Select Year(Getdate()))
答案 3 :(得分:1)
Declare @Year VARCHAR(10)
Set @Year = (Select Year(Getdate()))
INSERT INTO [dbo].[GazettedHolidays]
Select 1, GETDATE(), 'Kashmir Day', '05 Feb' +@Year,'Kashmir Day',1
Union All
Select 1, GETDATE(), 'Pakistan Day', '23 Mar'+@Year,'Pakistan Day',1
Union All
Select 1, GETDATE(), 'Labour Day', '01 May' +@Year,'Labour Day',1
Union All
Select 1, GETDATE(), 'Independence Day', '14 Aug' +@Year,'Independence Day',1
Union All
Select 1, GETDATE(), 'Iqbal day', '09 Nov' +@Year,'Iqbal Day',1
Union All
Select 1, GETDATE(), 'Quaid-e-Azam Day', '25 Dec' +@Year,'Quaid-e-Azam Day',1