1

I have an existing T-SQL query that returns a list of customers who need to be sent updates. As a part of the WHERE clause, the query checks to make sure the version conforms to a length of 6, and then appends additional characters.

Our latest version has a longer version, so I need to add a conditional statement to the SQL, but I keep getting an error when I do so. I won't post the entire statement for brevity. The SQL statement returns values when I run it like this.

(SELECT     
     MAX(substring(cversion, 1, 4) + case when len(cversion) = 6 then '0' else '' end + substring(cversion,5,3))
 FROM version
 GROUP BY iproductid, LEFT(cversion, 3)))

Below are my modifications. I know that the else code is identical, but I am just trying to get it to run currently. All I did was add the CASE and ELSE, and duplicate what was done, but now I get errors stating

Incorrect syntax near the keyword SELECT

along with two similar errors near the added keywords.

vr.cversion IN
(
    CASE WHEN product.iproductid < 8 THEN
        SELECT MAX
        (
            substring(cversion,1,4) + 
                case when len(cversion) = 6 
                    then '0' 
                    else '' 
                end + substring(cversion,5,3)
        )
        FROM version
        GROUP BY iproductid,LEFT(cversion, 3)
    ELSE
            SELECT MAX
        (
            substring(cversion,1,4) + 
                case when len(cversion) = 6 
                    then '0' 
                    else '' 
                end + substring(cversion,5,3)
        )
        FROM version
        GROUP BY iproductid,LEFT(cversion, 3)
    END
)

Could anyone tell me what I am doing wrong? Thanks.

UPDATE

Several people have said I need to use CASE instead. I had tried that with the same results, but I updated the code to show how I organized it.

1
  • Aside: Are you trying to use MAX on a string containing an integer value? Do you expect the result to be the alphabetical, not numeric, maximum? Commented Jul 23, 2015 at 2:23

3 Answers 3

1

You need parentheses around the select statements so that they get parsed as sub-queries. I noticed that the two code blocks are identical, but I guess they are meant to not be?

vr.cversion IN 
(
    CASE WHEN product.iproductid < 8 THEN
        (
          SELECT MAX
          (
             substring(cversion,1,4) + 
                case when len(cversion) = 6 
                    then '0' 
                    else '' 
                end + substring(cversion,5,3)
          )
          FROM version
          GROUP BY iproductid,LEFT(cversion, 3)
       )
    ELSE
        (
           SELECT MAX
          (
             substring(cversion,1,4) + 
                case when len(cversion) = 6 
                    then '0' 
                    else '' 
                end + substring(cversion,5,3)
          )
          FROM version
          GROUP BY iproductid,LEFT(cversion, 3)
       )
    END
)
Sign up to request clarification or add additional context in comments.

1 Comment

That looks like it did it. I'm still getting an error about the subquery returning more than one value, but this is progress. I do know that both cases do the same thing. I was just trying to get this part to work before I began modifying the ELSE to do what I need it to do. Little steps. :)
0

You can't use IF inside a TSQL Statement. You can only use it on Stored Procedures.

Use CASE or IIF instead

1 Comment

I have tried using case as well, and still got an error.
0

When you start that IN query you start it with if - shouldn't you start that with a CASE statement? CASE When THEN ELSE END?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.