在此存储过程中,我收到将数据类型varchar转换为datetime的错误

时间:2014-01-02 10:45:26

标签: sql sql-server datetime stored-procedures

请在执行此存储过程时帮助解决此问题

  

将数据类型varchar转换为datetime

时出错

在仅作为日期创建的数据库中。

列包含日期数据类型

Lawcurdatefrm, Lawcurdateto
Lawcomdatefrom,Lawcomdateto

这四列是date数据类型

我的要求是我需要记录FromDateToDate

示例

今年我正在过去那些日子

 Lawcurdatefrm = 01/04/2011 
 Lawcurdateto = 31/03/2012

用于比较我正在使用的日期

Lawcomdatefrom = 01/04/2010 
Lawcomdateto = 31/03/2011

应用此条件后应显示当年和上一年的记录。如果上一年没有找到应显示零的记录。

CREATE PROCEDURE MVR_New_L_New_La_RPRT_spO
    @language       Ctxt_Language,
    @operationname      Ctxt_Operation,
    @ouinstance     Ctxt_OuInstance,
    @user       Ctxt_User,
    @Brndnamefrm        Brndnamefrm,
    @Brndnameto     Brndnameto,
    @Frgtypefrm     Frgtypefrm,
    @Frgtypeto      Frgtypeto,
    @LAWAND_SAL_ANANAME1        LAWAND_SAL_ANANAME1,
    @LAWAND_SAL_PRINT       LAWAND_SAL_PRINT,
    @LAWAND_SAL_RENDERTYPE1     LAWAND_SAL_RENDERTYPE1,
    @LAWAND_SAL_RPTID1      LAWAND_SAL_RPTID1,
    @LAWAND_SAL_RPTYPE1     LAWAND_SAL_RPTYPE1,
    @LAWAND_SAL_subaction       LAWAND_SAL_subaction,
    @Lawcomdatefrom     Lawcomdatefrom,
    @Lawcomdateto       Lawcomdateto,
    @Lawcurdatefrm      Lawcurdatefrm,
    @Lawcurdateto       Lawcurdateto,
    @LawCustCodefrm     LawCustCodefrm,
    @LawCustCodeto      LawCustCodeto,
    @LawDocnofrm        LawDocnofrm,
    @LawDocnoto     LawDocnoto,
    @Lawitemfrm     Lawitemfrm,
    @Lawitemto      Lawitemto,
    @lawprintcombo      lawprintcombo,
    @m_errorid      m_errorid   OUT
AS
Begin
   --nocount should be switched on to prevent phantom rows
   Set nocount on

   --@m_errorid should be 0 to Indicate Success
   Set @m_errorid=0

    --declaration of local variables
   --temporary and formal parameters mapping
   SET @language = ltrim(rtrim(@language))
   SET @operationname = ltrim(rtrim(@operationname))
   SET @ouinstance = ltrim(rtrim(@ouinstance))
   SET @user = ltrim(rtrim(@user))
   SET @Brndnamefrm = ltrim(rtrim(@Brndnamefrm))
   SET @Brndnameto = ltrim(rtrim(@Brndnameto))
   SET @Frgtypefrm = ltrim(rtrim(@Frgtypefrm))
   SET @Frgtypeto = ltrim(rtrim(@Frgtypeto))
SET @LAWAND_SAL_ANANAME1=ltrim(rtrim(@LAWAND_SAL_ANANAME1))
SET @LAWAND_SAL_PRINT=ltrim(rtrim(@LAWAND_SAL_PRINT))
SET @LAWAND_SAL_RENDERTYPE1=ltrim(rtrim(@LAWAND_SAL_RENDERTYPE1))
SET @LAWAND_SAL_RPTID1=ltrim(rtrim(@LAWAND_SAL_RPTID1))
SET @LAWAND_SAL_RPTYPE1=ltrim(rtrim(@LAWAND_SAL_RPTYPE1))
SET @LAWAND_SAL_subaction=ltrim(rtrim(@LAWAND_SAL_subaction))
SET @Lawcomdatefrom=ltrim(rtrim(@Lawcomdatefrom))
SET @Lawcomdateto=ltrim(rtrim(@Lawcomdateto))
SET @Lawcurdatefrm=ltrim(rtrim(@Lawcurdatefrm))
SET @Lawcurdateto=ltrim(rtrim(@Lawcurdateto))
SET @LawCustCodefrm=ltrim(rtrim(@LawCustCodefrm))
SET @LawCustCodeto=ltrim(rtrim(@LawCustCodeto))
SET @LawDocnofrm=ltrim(rtrim(@LawDocnofrm))
SET @LawDocnoto=ltrim(rtrim(@LawDocnoto))
SET @Lawitemfrm=ltrim(rtrim(@Lawitemfrm))
SET @Lawitemto=ltrim(rtrim(@Lawitemto))

SET @lawprintcombo=ltrim(rtrim(@lawprintcombo))
If @Lawcurdatefrm='' or isnull(@Lawcurdatefrm,'')=''
begin
   raiserror('Please enter a valid Current From Date',16,1)
   return
end

If @Lawcurdateto='' or isnull(@Lawcurdateto,'')=''
begin
    raiserror('Please enter a valid Current to Date',16,1)
   return
end

--select 'albus',@Lawcomdatefrom,@Lawcomdateto,@Lawcurdatefrm,@Lawcurdateto
(SELECT isnull (Itemqty1,0) from law_sale_view  where custordinv_hdr_tran_date between @Lawcomdatefrom and @Lawcomdateto )
(SELECT isnull(itemvalue1,0)  from law_sale_view  where custordinv_hdr_tran_date between @Lawcurdatefrm and @Lawcurdateto)
Select 0 'Lw_BrndNm',
convert(varchar(10),custordinv_hdr_anchor_date,109) 'Lw_Comfrmdate',
itemqty1 'Lw_ComQt',    
convert(varchar(10),custordinv_hdr_anchor_date,109) 'Lw_Comtodate',
itemvalue1 'Lw_ComVal',
convert(varchar(10),custordinv_hdr_tran_date,109) 'Lw_Curfrmdate',
custordinv_dtl_item_qty'Lw_CurQt',
convert(varchar(10),custordinv_hdr_tran_date,109) 'Lw_Curtodate',
custordinv_dtl_item_amt 'Lw_Curval',
custlo_cust_code 'Lw_CustCod',
custlo_cust_name 'Lw_CustNam',
stdattrvalue1 'Lw_Frghttyp',
custordinv_dtl_item_tcd_code 'Lw_ItmCod',
itemdesc 'Lw_ItmDes',
custordinv_dtl_uom 'Lw_UOM'
from law_sale_view 
--where custlo_cust_code=custlo_cust_code 
Set nocount off
End

2 个答案:

答案 0 :(得分:0)

请删除

 convert(varchar(10),custordinv_hdr_anchor_date,109)

在所有日期时间地点&尝试...

答案 1 :(得分:0)

将日期值重写为yyyy / MM / dd格式,例如“31/03/2011”至“2011/03/31”。为什么?当您的变换字符串“31/03/2011”到目前为止,31将被假定为月而不是日。