Conversion failed when converting date and/or time from character string.

本文探讨了在使用ASP.NET MVC和Entity Framework调用SQL Server存储过程中遇到的字符串转换时间错误,重点在于解决文本参数格式问题及提供排查和修复步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ASP.NET MVC  EF调用SQLServer的存储过程报错。

报错信息:从字符串转换日期/时间失败。

原文如下:

Conversion failed when converting date and/or time from character string.

我那个存储过程比较长,开始也没仔细看,拿到报错信息就百度了,结果折腾一大堆方法都不行。 

我的存储过程有一个参数是传的text,然后需要在里面截取某个位置的4个字符串作为时间的年(如:2020),问题就出在这里,我传进来的text格式不对,指定位置截取的字符串跟'-03-31'拼起来无法转成时间,就报上面那个错误了。

其实报错信息已经说得很明白了,就是字符串无法转成时间,遇到报错信息类似的,先仔细看一遍存储过程,是否有字符串拼接然后转时间的,然后看注意一下字符串格式就完了。

类似这样 @Text.substring(10, 4) + '-01-31'

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE SP_UpdateEmployee 
	-- Add the parameters for the stored procedure here
	@EmployeeId int, 
	@Text nvarchar(255)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update Employee set ReleaseTime = @Text.substring(10, 4) + '-01-31' where Id = @EmployeeId
GO

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑小晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值