Solved

SQL2005 Select where a variable contents is like ...

Posted on 2013-05-16
2
241 Views
Last Modified: 2013-08-06
I have a SQL statement where I'm trying to return selected data based on a customer number.

Since I want to look for either the fule or partial number I'm trying to use a like instaed of equal.

If I use the literal custnmbr in the below select it workse file but if I try using a variable it doesn't return anything.

I'm sure I have the like with a variable formatted incorrectly but not sure how to correct.

Thanks

      
Declare
      @KLEOrderNumber      VARCHAR(30),
      @PONumber                  VARCHAR(30),
      @AltPONumber            VARCHAR(61),
      @AcctNumb1                  varchar(15),
      @AcctNumb2                  varchar(15),
      @BeginDate                  DATETIME,
      @EndDate                  DATETIME
      
set @KLEOrderNumber = ''
set             @PONumber = ''
set            @AltPONumber = ''
set            @AcctNumb1 = '17319'
set            @AcctNumb2 = '344122'
set            @BeginDate = '1/1/2012'
set            @EndDate = '4/11/2013'      
      
      
      IF LEN(RTRIM(LTRIM(@KLEOrderNumber))) = 0
            SET @KLEOrderNumber = NULL      
      IF LEN(RTRIM(LTRIM(@PONumber))) = 0
            SET @PONumber = NULL      
      IF LEN(RTRIM(LTRIM(@AltPONumber))) = 0
            SET @AltPONumber = NULL      
      IF LEN(RTRIM(LTRIM(@AcctNumb1))) = 0
            SET @AcctNumb1 = NULL
      IF LEN(RTRIM(LTRIM(@AcctNumb2))) = 0
            SET @AcctNumb2 = NULL
      IF (LEN(RTRIM(LTRIM(@BeginDate))) = 0) OR (@BeginDate = '1900-01-01') OR (@BeginDate IS NULL)
            SET @BeginDate = GETDATE()-365      
      IF (LEN(RTRIM(LTRIM(@EndDate))) = 0) OR (@EndDate = '1900-01-01') OR (@EndDate IS NULL)
            SET @EndDate = GETDATE()
      
      
      
      
      SELECT --TOP 500
         A.CSTPONBR [PONumber]
       , A.SOPNUMBE [KLEOrderNumber]
       , A.Address3 [AltPONumber]
       , B.SOPTYPE
       , D.TCC [SKU]
       , CAST(B.UNITPRCE AS DECIMAL(15,2)) [UNITPRCE]
       , CAST(B.XTNDPRCE AS DECIMAL(15,2)) [XTNDPRCE]
       , CAST(B.QUANTITY AS INT) [QuantityOrdered]
       , CAST(B.QTYFULFI AS INT) [QuantityShipped]
       , CAST(B.QTYPRINV AS INT) [InvoicedQuantityShipped]
       , CONVERT(VARCHAR(10),A.DOCDATE,101) [OrderDate]
       , CONVERT(VARCHAR(10),B.ReqShipDate,101) [ShipDate]
       , CONVERT(VARCHAR(10),B.ReqShipDate,101) [FutureShipDate]
       , a.CUSTNMBR [Account]
      FROM
       KG.dbo.TEAMVIEW_SOP10100_SOP30200 A WITH(NOLOCK,READUNCOMMITTED)
       INNER JOIN
       KG.dbo.TEAMVIEW_SOP10200_SOP30300 B WITH(NOLOCK,READUNCOMMITTED)
            ON B.SOPNUMBE = A.SOPNUMBE AND
               B.SOPTYPE  = A.SOPTYPE  AND
               A.SOPNUMBE = ISNULL(@KLEOrderNumber,A.SOPNUMBE) AND
               A.SOPTYPE  = 2 AND
               A.DOCDATE BETWEEN @BeginDate AND @EndDate AND
               (A.CUSTNAME LIKE 'SBA-%'
                        or a.CUSTNAME like '%SCHN%')
       INNER JOIN
       KG.dbo.SOP10106 C WITH(NOLOCK,READUNCOMMITTED)
            ON C.SOPNUMBE = A.SOPNUMBE AND
               C.SOPTYPE  = A.SOPTYPE AND
               --A.CSTPONBR = ISNULL(@PONumber,A.CSTPONBR)AND
               A.CSTPONBR like (case when @PoNumber is NULL
                        then ISNULL(@PONumber,A.CSTPONBR)
                        else '%'+@PONumber+'%' end) AND
               --A.Address3 = ISNULL(@AltPONumber,A.Address3)
               A.Address3 like (case when @AltPoNumber is NULL
                        then ISNULL(@AltPONumber,A.Address3)
                        else '%'+@AltPONumber+'%' end)
       INNER JOIN KG.dbo.IV00101 D WITH(NOLOCK,READUNCOMMITTED)
            ON D.ITEMNMBR = B.ITEMNMBR
       INNER JOIN KG.dbo.RM00101 R WITH(NOLOCK,READUNCOMMITTED)
            ON R.CUSTNMBR = A.CUSTNMBR
                  and R.salsterr like '%SBA%'
       INNER JOIN KG.dbo.HM00600 E WITH(NOLOCK,READUNCOMMITTED)
            ON E.CUSTNMBR = A.CUSTNMBR

      
      --where a.custnmbr like '344122%'
      where a.custnmbr like '@AcctNumb2'+'%'
0
Comment
Question by:jdr0606
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 39172123
where a.custnmbr like @AcctNumb2+'%'
0
 

Author Comment

by:jdr0606
ID: 39172131
Thanks!

Duh!  Sometimes we can't see the forest for the trees
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question