如何使用 SQL 和 asp.net 核心中的存储过程删除不同的字符串日期
How to delete different string date with stored procedure in SQL and asp.net core
我要从 table 中删除日期以字符串格式存储的内容。小时、分钟不是 consistent.eg 这些日期来自不同的来源,我无法控制
- 2020-10-1612:00:00
- 2020-10-16 上午 12:00:00 上午
- 2020-10-1612:00:00上午
- 2020-10-18 00:00:00.0000000
- 2020-8-31 00:00:00.0000000
- 2020-10-21 00:00:00.0000000
- 2020-9-30 00:00:00.0000000
- 2020-10-21 12:00:00.0000000
- 2020-09-3000:00:00
我只能从我的代码中确定年、月和日。我只能将例如“2020-09-30”从 C# 代码传递到我的存储过程。在插入新记录之前,我必须删除与该日期和 accountNo 关联的所有事件。
我怎样才能做到这一点?
alter procedure [dbo].[DeleteFromStatement](
@AccountNo varchar(50),
@Date varchar(250)
)
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM [dbo].[MonthlyInterest] WHERE AccountNo=@AccountNo AND Date=@Date
END
可以使用PATINDEX
函数。
如果日期作为 VARCHAR(250) 传递,您可以使用 TRIM 函数。如果使用旧版本,则可以使用 LTRIM(RTRIM(...))。
alter procedure [dbo].[DeleteFromStatement](
@AccountNo varchar(50),
@Date varchar(250))
as
set nocount on;
delete mt
from [dbo].[MonthlyInterest] mt
where AccountNo=@AccountNo
and patindex('%'+ltrim(rtrim(@Date))+'%', mt.Date)>0;
go
如果日期作为日期传入,您可以使用 CAST(@dt as char(10))
alter procedure [dbo].[DeleteFromStatement](
@AccountNo varchar(50),
@Date date)
as
set nocount on;
delete mt
from [dbo].[MonthlyInterest] mt
where AccountNo=@AccountNo
and patindex('%'+cast(@Date as char(10))+'%', mt.Date)>0;
go
我要从 table 中删除日期以字符串格式存储的内容。小时、分钟不是 consistent.eg 这些日期来自不同的来源,我无法控制
- 2020-10-1612:00:00
- 2020-10-16 上午 12:00:00 上午
- 2020-10-1612:00:00上午
- 2020-10-18 00:00:00.0000000
- 2020-8-31 00:00:00.0000000
- 2020-10-21 00:00:00.0000000
- 2020-9-30 00:00:00.0000000
- 2020-10-21 12:00:00.0000000
- 2020-09-3000:00:00
我只能从我的代码中确定年、月和日。我只能将例如“2020-09-30”从 C# 代码传递到我的存储过程。在插入新记录之前,我必须删除与该日期和 accountNo 关联的所有事件。
我怎样才能做到这一点?
alter procedure [dbo].[DeleteFromStatement](
@AccountNo varchar(50),
@Date varchar(250)
)
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM [dbo].[MonthlyInterest] WHERE AccountNo=@AccountNo AND Date=@Date
END
可以使用PATINDEX
函数。
如果日期作为 VARCHAR(250) 传递,您可以使用 TRIM 函数。如果使用旧版本,则可以使用 LTRIM(RTRIM(...))。
alter procedure [dbo].[DeleteFromStatement](
@AccountNo varchar(50),
@Date varchar(250))
as
set nocount on;
delete mt
from [dbo].[MonthlyInterest] mt
where AccountNo=@AccountNo
and patindex('%'+ltrim(rtrim(@Date))+'%', mt.Date)>0;
go
如果日期作为日期传入,您可以使用 CAST(@dt as char(10))
alter procedure [dbo].[DeleteFromStatement](
@AccountNo varchar(50),
@Date date)
as
set nocount on;
delete mt
from [dbo].[MonthlyInterest] mt
where AccountNo=@AccountNo
and patindex('%'+cast(@Date as char(10))+'%', mt.Date)>0;
go