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.
MAXon a string containing an integer value? Do you expect the result to be the alphabetical, not numeric, maximum?