我尝试执行下面的查询,但是我收到一条错误消息“将char数据类型转换为日期时间数据类型会导致超出范围的datetime值。”。我尝试转换日期并转换日期,但会出现同样的错误。
select DATEDIFF(YEAR,cast(Startdate as datetime),isnull(cast(Enddate as datetime), GETDATE()))
from employmentdetails where id=36
select DATEDIFF(YEAR,CONVERT(varchar,Startdate,101),isnull(CONVERT(varchar,Enddate,101),CONVERT(varchar,GETDATE(),101)))
from employmentdetails where id=35
create table EmploymentDetails(
Id bigint not null identity constraint PK_EmploymentDetails primary key,
aId bigint not null,
Startdate varchar(25),
Enddate varchar(25),
Position varchar(30),
PositionLevelId bigint not null,
SpecializationId bigint not null,
PositionId bigint not null,
StartSalary varchar(50),
EndSalary varchar(50),
DescriptionofDuties nvarchar(1000),
ReasonforLeaving nvarchar(200),
CompanyName nvarchar(100),
TypeofBusiness varchar(50),
Address1 varchar(25),
Address2 varchar(25),
City varchar(25),
Province varchar(25),
StateorRegion varchar(25),
CountryId bigint,
PostalCode varchar(10)
)
go
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (34, 154, N'April 1,2010', N'April 10,2010', N'Sr. .Net Developer', 1, 1, 1, N'P12,000', N'P12,000', N'Design websites for company using Adobe dreamweaver, Flash and other web applications.', N'sample reason for leaving', N'Appsource', N'Information Technology', N'Unit 1401 Robinsons Equit', N'ADB Ave. Corner Poveda Ro', N'Pasig', N'NCR', N'NCR', 177, N'')
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (35, 154, N'April 7,2009', N'April 7,2010', N'Lead Software Developer', 1, 2, 1, N'P12,000', N'P12,000', N'Design websites for company using Adobe dreamweaver, Flash and other web applications.', N'sample reason for leaving', N'Corebuilt Technologies', N'Information Technology', N'24/F 88 Corporate Center', N'Valero St.,', N'Makati', N'NCr', N'NCr', 177, N'')
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (36, 154, N'April 7,2009', N'', N'Web Developer', 1, 1, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'IDCSI', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', N'', N'NCR', 177, N'')
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (38, 155, N'June 12,2008', N'June 12,2009', N'Analyst', 1, 3, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'IDCSI', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL)
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (40, 156, N'August 1, 2001', N'', N'Quality Controller', 1, 2, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'IDCSI', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', N'', N'', 177, N'')
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (41, 165, N'January 12, 1980', N'June 8, 1995', N'Analyst', 1, 2, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL)
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode])
VALUES (42, 165, N'August 2,1995', N'', N'Programmer', 1, 3, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', N'', N'', 177, N'')
答案 0 :(得分:1)
MS-SQL支持从“1753-01-01”到“9999-12-31”的日期。您必须提供超出此范围的日期(或正在尝试转换为日期的varchar格式的大数字)。
答案 1 :(得分:1)
您应该解决问题,而不是症状:将数据库列更改为DATETIME数据类型,您再也不用担心它了。
答案 2 :(得分:1)
你永远不应该把日期存储为varchar(25),使用datetime,smalldatetime, 或日期数据类型(日期仅在sql server 2008中)!!!
对于id为36,40的行,而42 EndDate
不为null,则为空字符串。因此,您的ISNULL尝试使用GETDATE()失败。这是您的示例查询失败并修复到位:
select DATEDIFF(YEAR,cast(Startdate as datetime),isnull(cast(NULLIF(Enddate,'') as datetime), GETDATE()))
from employmentdetails where id=36