i am trying to pass first day and last day of previous month as a date parameter in my stored procedure but it's throwing data conversion error like below:
我试图通过我的存储过程中的第一天和上个月的最后一天作为日期参数,但它正在抛出数据转换错误,如下所示:
Conversion failed when converting date and/or time from character string.
从字符串转换日期和/或时间时转换失败。
Here's example how i passed the parameter;
这是我如何传递参数的例子;
EXEC DBO.usp_PRODUCTS 'DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0)'
,'DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1)'
The data type for start date and end date in my stored proc is date , even if i change to varchar, it's showing similar error.
我的存储过程中的开始日期和结束日期的数据类型是日期,即使我更改为varchar,它也显示类似的错误。
Is there any way i can pass first day and last day of previous month as parameter in my stored proc?
有没有办法可以通过我的存储过程中的第一天和上个月的最后一天作为参数?
2 个解决方案
#1
0
Try this
尝试这个
Declare @startDate Date = DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0));
Declare @endDate Date =DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0));
EXEC DBO.usp_PRODUCTS @startDate , @endDate ;
#2
0
If you are using SQLServer 2012 or above, you can use the EOMonth function to return the last day of the month with an offset of -1 for the previous month:
如果您使用的是SQLServer 2012或更高版本,则可以使用EOMonth函数返回该月的最后一天,上个月的偏移量为-1:
EXEC DBO.usp_PRODUCTS
DateAdd(day,1,EOMonth(GetDate(),-2)),
EOMonth(GetDate(),-1)
#1
0
Try this
尝试这个
Declare @startDate Date = DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0));
Declare @endDate Date =DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0));
EXEC DBO.usp_PRODUCTS @startDate , @endDate ;
#2
0
If you are using SQLServer 2012 or above, you can use the EOMonth function to return the last day of the month with an offset of -1 for the previous month:
如果您使用的是SQLServer 2012或更高版本,则可以使用EOMonth函数返回该月的最后一天,上个月的偏移量为-1:
EXEC DBO.usp_PRODUCTS
DateAdd(day,1,EOMonth(GetDate(),-2)),
EOMonth(GetDate(),-1)