Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL2005 Select where a variable contents is like ...

Posted on 2013-05-16
2
Medium Priority
?
245 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

610 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