Stored procedure with if condition on column names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sudhaMurugesan
    New Member
    • May 2007
    • 94

    Stored procedure with if condition on column names

    Dear Friends,
    Would any body knew the solution for this?
    If Gross Weight value is null i would get Divide by Zero error. So
    I have to check whether Grossweight is zero or not, to get the result.I cant check this in aspx.cs file becoz each InternalControl No has different Grosswt.and I do not know the gross weight while i call this sp. Please help what to do?
    Code:
    Create procedure [dbo].[USP_ImportPaymentView_Select]                                    
    @Internalcontrolnumber nvarchar(100),                
    @FromDate varchar(20),                                  
    @ToDate varchar(20)
    As
    Select 
    Gross Weight,
    TLCActualGrossValue=round((TotalFreightValue/GrossWeight),2),
    from importpaymentview                            
    where      
    (Yearofimport between @FromDate and @ToDate)                                 
      or (InternalControlNumber =@Internalcontrolnumber)
  • frozenmist
    Recognized Expert New Member
    • May 2007
    • 179

    #2
    Hi ,
    try giving case statment in the SELECT
    Like
    Select ...
    CASE WHEN grossweight is null then else

    Also if you want to assign a default value to gross weight when it is null then you can give
    ISNULL(grosswei ght,default value)

    Hope these hints are useful
    Cheers

    Comment

    • frozenmist
      Recognized Expert New Member
      • May 2007
      • 179

      #3
      Hi,
      If you want to use the case try this piece of code
      Code:
      Select 
      Gross Weight,
      CASE when GrossWeight is not  NULL 
                then   round((TotalFreightValue/GrossWeight),2)
                else NULL End as TLCActualGrossValue
      from importpaymentview                            
      where      
      (Yearofimport between @FromDate and @ToDate)                                 
        or (InternalControlNumber =@Internalcontrolnumber)

      Hope it works,because i couldnt run and see it.
      Cheers

      Comment

      • sudhaMurugesan
        New Member
        • May 2007
        • 94

        #4
        Thanks Mr.FrozenMist.
        I had already used a default value for gross weight. I try with select case too. Thanks for your idea.

        Comment

        Working...