We help IT Professionals succeed at work.

using a SELECT statement in a  THEN  of a CASE statement.

Last Modified: 2017-04-24
I am trying to make a conversion script that takes a measurement and converts it to a different unit.
Example centimeter to milometers.   I don’t want to make the field case sensitive in case someone would put  Inch or inch in @p2 I want it to return the same value but certain  values need to be case sensitive ‘mm’ could be Mega meters  or Millimeters. Therefore I was trying to use  a select statement in the then section of the where clause an it is giving me Incorrect syntax near the keyword 'select'.
Below is the code

declare  @P1 VARCHAR(max) –- value of the measurement
declare @P2 VARCHAR(max) –- original unit
declare  @P3 VARCHAR(max)—new unit
DECLARE @cleanInput VARCHAR(100);

set @P1 = '12'
set @P2 = 'm'
set @P3 = 'inch'

 SET @cleanInput ='';
      SET @pos = 1;

      WHILE @pos <= Len(@P1)
            SET @cleanInput= @cleanInput + CASE WHEN Isnumeric(Substring(@P1,
                             , 1)
                             = 1 THEN Substring(
                             @p1, @pos, 1) ELSE '' END;
            SET @pos = @pos + 1;

      --SET @cleanInput = Replace(@CleanInput, ',', '');
      SET @cleanInput = Replace(@CleanInput, '$', '');

 set @cleanInput =  case
                                     when @P2 = 'mm' then select (  cast (@cleanInput as float) *multiplier
                                                                                                 from [L_Conversion_Table ]                                          
                                                                                                       where ASCII( type) = ASCII(@P2))
                                    Else @cleanInput
Watch Question

HainKurtSr. System Analyst


declare @mm as float;
select @mm = multiplier
from [L_Conversion_Table ]                                          
where ASCII( type) = ASCII(@P2));

 if (@P2 = 'mm') set @cleanInput = cast (@cleanInput as float) *multiplier;

Open in new window

SQL Server Consultant
Unlock this solution and get a sample of our free trial.
(No credit card required)


(select (  cast (@cleanInput as float) *multiplier
                                                                                                 from [L_Conversion_Table ]                                          
                                                                                                       where ASCII( type) = ASCII(@P2)))

it then gives me incorrect Incorrect syntax near the keyword 'from'.
Megan BrooksSQL Server Consultant

I don't think the syntax of your SELECT is correct. You have an open parenthesis from after the SELECT keyword that is not closed before FROM. Debug your query first, wrap it in parentheses, and then you should be able to use it as an expression in CASE.
HainKurtSr. System Analyst

or try
if @P2 = 'mm' 
 select @cleanInput = cast (@cleanInput as float) *multiplier
  from [L_Conversion_Table ]                                          
 where ASCII( type) = ASCII(@P2);

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.