getting an error while converting '11-1' to INT in sql server












-1















I am not able to convert '11-1' to INT in sql server, the result should be 10.
i tried with convert and cast but getting an error.










share|improve this question























  • if you mean you are basically doing CAST('11-1' AS INT) then that will clearly complain. It is a string not an integer equation.

    – arahman
    Nov 15 '18 at 13:10













  • How to handle this case?

    – Girish Mulgund
    Nov 15 '18 at 13:11











  • what is the logic to convert it on 10 ??

    – Zaynul Abadin Tuhin
    Nov 15 '18 at 13:11











  • @ZaynulAbadinTuhin 11-1 = 10

    – arahman
    Nov 15 '18 at 13:13











  • @arahman . . . You would need to use dynamic SQL.

    – Gordon Linoff
    Nov 15 '18 at 13:13
















-1















I am not able to convert '11-1' to INT in sql server, the result should be 10.
i tried with convert and cast but getting an error.










share|improve this question























  • if you mean you are basically doing CAST('11-1' AS INT) then that will clearly complain. It is a string not an integer equation.

    – arahman
    Nov 15 '18 at 13:10













  • How to handle this case?

    – Girish Mulgund
    Nov 15 '18 at 13:11











  • what is the logic to convert it on 10 ??

    – Zaynul Abadin Tuhin
    Nov 15 '18 at 13:11











  • @ZaynulAbadinTuhin 11-1 = 10

    – arahman
    Nov 15 '18 at 13:13











  • @arahman . . . You would need to use dynamic SQL.

    – Gordon Linoff
    Nov 15 '18 at 13:13














-1












-1








-1








I am not able to convert '11-1' to INT in sql server, the result should be 10.
i tried with convert and cast but getting an error.










share|improve this question














I am not able to convert '11-1' to INT in sql server, the result should be 10.
i tried with convert and cast but getting an error.







sql sql-server






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 13:09









Girish MulgundGirish Mulgund

3015




3015













  • if you mean you are basically doing CAST('11-1' AS INT) then that will clearly complain. It is a string not an integer equation.

    – arahman
    Nov 15 '18 at 13:10













  • How to handle this case?

    – Girish Mulgund
    Nov 15 '18 at 13:11











  • what is the logic to convert it on 10 ??

    – Zaynul Abadin Tuhin
    Nov 15 '18 at 13:11











  • @ZaynulAbadinTuhin 11-1 = 10

    – arahman
    Nov 15 '18 at 13:13











  • @arahman . . . You would need to use dynamic SQL.

    – Gordon Linoff
    Nov 15 '18 at 13:13



















  • if you mean you are basically doing CAST('11-1' AS INT) then that will clearly complain. It is a string not an integer equation.

    – arahman
    Nov 15 '18 at 13:10













  • How to handle this case?

    – Girish Mulgund
    Nov 15 '18 at 13:11











  • what is the logic to convert it on 10 ??

    – Zaynul Abadin Tuhin
    Nov 15 '18 at 13:11











  • @ZaynulAbadinTuhin 11-1 = 10

    – arahman
    Nov 15 '18 at 13:13











  • @arahman . . . You would need to use dynamic SQL.

    – Gordon Linoff
    Nov 15 '18 at 13:13

















if you mean you are basically doing CAST('11-1' AS INT) then that will clearly complain. It is a string not an integer equation.

– arahman
Nov 15 '18 at 13:10







if you mean you are basically doing CAST('11-1' AS INT) then that will clearly complain. It is a string not an integer equation.

– arahman
Nov 15 '18 at 13:10















How to handle this case?

– Girish Mulgund
Nov 15 '18 at 13:11





How to handle this case?

– Girish Mulgund
Nov 15 '18 at 13:11













what is the logic to convert it on 10 ??

– Zaynul Abadin Tuhin
Nov 15 '18 at 13:11





what is the logic to convert it on 10 ??

– Zaynul Abadin Tuhin
Nov 15 '18 at 13:11













@ZaynulAbadinTuhin 11-1 = 10

– arahman
Nov 15 '18 at 13:13





@ZaynulAbadinTuhin 11-1 = 10

– arahman
Nov 15 '18 at 13:13













@arahman . . . You would need to use dynamic SQL.

– Gordon Linoff
Nov 15 '18 at 13:13





@arahman . . . You would need to use dynamic SQL.

– Gordon Linoff
Nov 15 '18 at 13:13












5 Answers
5






active

oldest

votes


















0














As has been discussed '11-1' can't be converted to an int because it's a varchar representing a numerical expression.



The only way to reliably derive an expression stored as a varchar is by using dynamic SQL, which, has it own problems; specifically SQL injection.



Assuming that your data is coming from a table, and that these varchar expressions only contain simple arithmetic, you could do something like this:



CREATE TABLE dbo.test (ID int IDENTITY(1,1),
varcharExpression varchar(20));
INSERT INTO dbo.test (varcharExpression)
VALUES('11-1'),('12*2'),('2+9'),('15/3'),
('2^4'), --not supported in this example. SQL Server doesn't use ^, it uses POWER: POWER(2,4)
('DROP TABLE dbo.test;'); --Intentional example of an injection attempt.

GO
DECLARE @SQL nvarchar(MAX);

SET @SQL = STUFF((SELECT NCHAR(10) + N'UNION ALL' + NCHAR(10) +
N'SELECT ' + CONVERT(varchar(6),ID) + N' AS ID,' + NCHAR(10) +
N' ' + CASE WHEN varcharExpression NOT LIKE '%[^0-9-+*/]%' ESCAPE '' THEN varcharExpression ELSE N'NULL' END + N' AS ExpressionValue'
FROM dbo.test
ORDER BY ID ASC
FOR XML PATH(N'')),1,10,N'') + ';';
PRINT @SQL;
EXEC sp_executesql @SQL;
GO
DROP TABLE dbo.test





share|improve this answer

































    2














    '11-1' is a string and can't be converted to INT. You can do that as



    SELECT (CAST (11-1) AS INT)


    Or by using Dynamic SQL as



    DECLARE @SQL NVARCHAR(MAX) = N'SELECT 11-1';

    EXECUTE sp_executesql @SQL;


    Or even



    CREATE TABLE Operations(
    ID INT IDENTITY(1, 1),
    Operation VARCHAR(45)
    );

    INSERT INTO Operations VALUES
    ('10-1'),
    ('10+1'),
    ('(5+5) - 5'),
    ('5+5');

    DECLARE @SQL NVARCHAR(MAX) = N'SELECT ';

    SELECT @SQL = @SQL + Operation + ' ,'
    FROM Operations

    SET @SQL = LEFT(@SQL, LEN(@SQL) - 1);

    EXECUTE sp_executesql @SQL;


    Or if you want to get it as rows



    SELECT @SQL = @SQL + Operation + ' UNION ALL SELECT '
    FROM Operations

    SET @SQL = LEFT(@SQL, LEN(@SQL) - 17);

    EXECUTE sp_executesql @SQL;


    You can also declare the @SQL variable as follow if you want to name the column



    DECLARE @SQL NVARCHAR(MAX) = N'SELECT Operations = ';


    Demo



    As a final note (or warning), be careful for SQL Injection if your column has other strings as it maybe malicious commands.






    share|improve this answer

































      0














      you can try like below, separate both part by using substring then
      convert it into int then add both



      SELECT convert(int, substring('11-1',0, charindex('-', '11-1')))+
      convert(int, substring('11-1',charindex('-', '11-1'), len('11-1')))


      It will return 10






      share|improve this answer


























      • You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

        – arahman
        Nov 15 '18 at 13:18






      • 2





        @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

        – Zaynul Abadin Tuhin
        Nov 15 '18 at 13:19





















      0














      Use dynamic SQL to parse expression



      DECLARE @result int, @sql nvarchar(max);
      SET @sql = N'SELECT ' + '11 - 1';
      EXEC sp_executesql @sql


      or



      DECLARE @result int, @sql nvarchar(max);
      SET @sql = N'SELECT @result = ' + '11 - 1';
      EXEC sp_executesql @sql, N'@result int OUTPUT', @result = @result OUTPUT
      SELECT @result


      This will return 10






      share|improve this answer































        0














        The example below shows how you could evaluate the T-SQL expression stored in a table column. However, I would advise against this approach unless the value is from a source that is fully trusted. Otherwise, there is a SQL injection risk.



        Also, this method cannot be used easily to return values from multiple rows in a single result. You would need a cursor to evaluate and insert each result into a temp table/variable and then select from the table as the final result.



        DECLARE @Foo TABLE(
        FooID int
        , Expression nvarchar(10) NOT NULL
        )
        INSERT INTO @Foo VALUES(1, '11-1');
        DECLARE @sql nvarchar(MAX) = N'SELECT ' + (SELECT Expression FROM @foo WHERE FooID = 1) + N' AS Result;';
        EXECUTE (@sql);





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320220%2fgetting-an-error-while-converting-11-1-to-int-in-sql-server%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          As has been discussed '11-1' can't be converted to an int because it's a varchar representing a numerical expression.



          The only way to reliably derive an expression stored as a varchar is by using dynamic SQL, which, has it own problems; specifically SQL injection.



          Assuming that your data is coming from a table, and that these varchar expressions only contain simple arithmetic, you could do something like this:



          CREATE TABLE dbo.test (ID int IDENTITY(1,1),
          varcharExpression varchar(20));
          INSERT INTO dbo.test (varcharExpression)
          VALUES('11-1'),('12*2'),('2+9'),('15/3'),
          ('2^4'), --not supported in this example. SQL Server doesn't use ^, it uses POWER: POWER(2,4)
          ('DROP TABLE dbo.test;'); --Intentional example of an injection attempt.

          GO
          DECLARE @SQL nvarchar(MAX);

          SET @SQL = STUFF((SELECT NCHAR(10) + N'UNION ALL' + NCHAR(10) +
          N'SELECT ' + CONVERT(varchar(6),ID) + N' AS ID,' + NCHAR(10) +
          N' ' + CASE WHEN varcharExpression NOT LIKE '%[^0-9-+*/]%' ESCAPE '' THEN varcharExpression ELSE N'NULL' END + N' AS ExpressionValue'
          FROM dbo.test
          ORDER BY ID ASC
          FOR XML PATH(N'')),1,10,N'') + ';';
          PRINT @SQL;
          EXEC sp_executesql @SQL;
          GO
          DROP TABLE dbo.test





          share|improve this answer






























            0














            As has been discussed '11-1' can't be converted to an int because it's a varchar representing a numerical expression.



            The only way to reliably derive an expression stored as a varchar is by using dynamic SQL, which, has it own problems; specifically SQL injection.



            Assuming that your data is coming from a table, and that these varchar expressions only contain simple arithmetic, you could do something like this:



            CREATE TABLE dbo.test (ID int IDENTITY(1,1),
            varcharExpression varchar(20));
            INSERT INTO dbo.test (varcharExpression)
            VALUES('11-1'),('12*2'),('2+9'),('15/3'),
            ('2^4'), --not supported in this example. SQL Server doesn't use ^, it uses POWER: POWER(2,4)
            ('DROP TABLE dbo.test;'); --Intentional example of an injection attempt.

            GO
            DECLARE @SQL nvarchar(MAX);

            SET @SQL = STUFF((SELECT NCHAR(10) + N'UNION ALL' + NCHAR(10) +
            N'SELECT ' + CONVERT(varchar(6),ID) + N' AS ID,' + NCHAR(10) +
            N' ' + CASE WHEN varcharExpression NOT LIKE '%[^0-9-+*/]%' ESCAPE '' THEN varcharExpression ELSE N'NULL' END + N' AS ExpressionValue'
            FROM dbo.test
            ORDER BY ID ASC
            FOR XML PATH(N'')),1,10,N'') + ';';
            PRINT @SQL;
            EXEC sp_executesql @SQL;
            GO
            DROP TABLE dbo.test





            share|improve this answer




























              0












              0








              0







              As has been discussed '11-1' can't be converted to an int because it's a varchar representing a numerical expression.



              The only way to reliably derive an expression stored as a varchar is by using dynamic SQL, which, has it own problems; specifically SQL injection.



              Assuming that your data is coming from a table, and that these varchar expressions only contain simple arithmetic, you could do something like this:



              CREATE TABLE dbo.test (ID int IDENTITY(1,1),
              varcharExpression varchar(20));
              INSERT INTO dbo.test (varcharExpression)
              VALUES('11-1'),('12*2'),('2+9'),('15/3'),
              ('2^4'), --not supported in this example. SQL Server doesn't use ^, it uses POWER: POWER(2,4)
              ('DROP TABLE dbo.test;'); --Intentional example of an injection attempt.

              GO
              DECLARE @SQL nvarchar(MAX);

              SET @SQL = STUFF((SELECT NCHAR(10) + N'UNION ALL' + NCHAR(10) +
              N'SELECT ' + CONVERT(varchar(6),ID) + N' AS ID,' + NCHAR(10) +
              N' ' + CASE WHEN varcharExpression NOT LIKE '%[^0-9-+*/]%' ESCAPE '' THEN varcharExpression ELSE N'NULL' END + N' AS ExpressionValue'
              FROM dbo.test
              ORDER BY ID ASC
              FOR XML PATH(N'')),1,10,N'') + ';';
              PRINT @SQL;
              EXEC sp_executesql @SQL;
              GO
              DROP TABLE dbo.test





              share|improve this answer















              As has been discussed '11-1' can't be converted to an int because it's a varchar representing a numerical expression.



              The only way to reliably derive an expression stored as a varchar is by using dynamic SQL, which, has it own problems; specifically SQL injection.



              Assuming that your data is coming from a table, and that these varchar expressions only contain simple arithmetic, you could do something like this:



              CREATE TABLE dbo.test (ID int IDENTITY(1,1),
              varcharExpression varchar(20));
              INSERT INTO dbo.test (varcharExpression)
              VALUES('11-1'),('12*2'),('2+9'),('15/3'),
              ('2^4'), --not supported in this example. SQL Server doesn't use ^, it uses POWER: POWER(2,4)
              ('DROP TABLE dbo.test;'); --Intentional example of an injection attempt.

              GO
              DECLARE @SQL nvarchar(MAX);

              SET @SQL = STUFF((SELECT NCHAR(10) + N'UNION ALL' + NCHAR(10) +
              N'SELECT ' + CONVERT(varchar(6),ID) + N' AS ID,' + NCHAR(10) +
              N' ' + CASE WHEN varcharExpression NOT LIKE '%[^0-9-+*/]%' ESCAPE '' THEN varcharExpression ELSE N'NULL' END + N' AS ExpressionValue'
              FROM dbo.test
              ORDER BY ID ASC
              FOR XML PATH(N'')),1,10,N'') + ';';
              PRINT @SQL;
              EXEC sp_executesql @SQL;
              GO
              DROP TABLE dbo.test






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 14:03

























              answered Nov 15 '18 at 13:31









              LarnuLarnu

              20.5k51732




              20.5k51732

























                  2














                  '11-1' is a string and can't be converted to INT. You can do that as



                  SELECT (CAST (11-1) AS INT)


                  Or by using Dynamic SQL as



                  DECLARE @SQL NVARCHAR(MAX) = N'SELECT 11-1';

                  EXECUTE sp_executesql @SQL;


                  Or even



                  CREATE TABLE Operations(
                  ID INT IDENTITY(1, 1),
                  Operation VARCHAR(45)
                  );

                  INSERT INTO Operations VALUES
                  ('10-1'),
                  ('10+1'),
                  ('(5+5) - 5'),
                  ('5+5');

                  DECLARE @SQL NVARCHAR(MAX) = N'SELECT ';

                  SELECT @SQL = @SQL + Operation + ' ,'
                  FROM Operations

                  SET @SQL = LEFT(@SQL, LEN(@SQL) - 1);

                  EXECUTE sp_executesql @SQL;


                  Or if you want to get it as rows



                  SELECT @SQL = @SQL + Operation + ' UNION ALL SELECT '
                  FROM Operations

                  SET @SQL = LEFT(@SQL, LEN(@SQL) - 17);

                  EXECUTE sp_executesql @SQL;


                  You can also declare the @SQL variable as follow if you want to name the column



                  DECLARE @SQL NVARCHAR(MAX) = N'SELECT Operations = ';


                  Demo



                  As a final note (or warning), be careful for SQL Injection if your column has other strings as it maybe malicious commands.






                  share|improve this answer






























                    2














                    '11-1' is a string and can't be converted to INT. You can do that as



                    SELECT (CAST (11-1) AS INT)


                    Or by using Dynamic SQL as



                    DECLARE @SQL NVARCHAR(MAX) = N'SELECT 11-1';

                    EXECUTE sp_executesql @SQL;


                    Or even



                    CREATE TABLE Operations(
                    ID INT IDENTITY(1, 1),
                    Operation VARCHAR(45)
                    );

                    INSERT INTO Operations VALUES
                    ('10-1'),
                    ('10+1'),
                    ('(5+5) - 5'),
                    ('5+5');

                    DECLARE @SQL NVARCHAR(MAX) = N'SELECT ';

                    SELECT @SQL = @SQL + Operation + ' ,'
                    FROM Operations

                    SET @SQL = LEFT(@SQL, LEN(@SQL) - 1);

                    EXECUTE sp_executesql @SQL;


                    Or if you want to get it as rows



                    SELECT @SQL = @SQL + Operation + ' UNION ALL SELECT '
                    FROM Operations

                    SET @SQL = LEFT(@SQL, LEN(@SQL) - 17);

                    EXECUTE sp_executesql @SQL;


                    You can also declare the @SQL variable as follow if you want to name the column



                    DECLARE @SQL NVARCHAR(MAX) = N'SELECT Operations = ';


                    Demo



                    As a final note (or warning), be careful for SQL Injection if your column has other strings as it maybe malicious commands.






                    share|improve this answer




























                      2












                      2








                      2







                      '11-1' is a string and can't be converted to INT. You can do that as



                      SELECT (CAST (11-1) AS INT)


                      Or by using Dynamic SQL as



                      DECLARE @SQL NVARCHAR(MAX) = N'SELECT 11-1';

                      EXECUTE sp_executesql @SQL;


                      Or even



                      CREATE TABLE Operations(
                      ID INT IDENTITY(1, 1),
                      Operation VARCHAR(45)
                      );

                      INSERT INTO Operations VALUES
                      ('10-1'),
                      ('10+1'),
                      ('(5+5) - 5'),
                      ('5+5');

                      DECLARE @SQL NVARCHAR(MAX) = N'SELECT ';

                      SELECT @SQL = @SQL + Operation + ' ,'
                      FROM Operations

                      SET @SQL = LEFT(@SQL, LEN(@SQL) - 1);

                      EXECUTE sp_executesql @SQL;


                      Or if you want to get it as rows



                      SELECT @SQL = @SQL + Operation + ' UNION ALL SELECT '
                      FROM Operations

                      SET @SQL = LEFT(@SQL, LEN(@SQL) - 17);

                      EXECUTE sp_executesql @SQL;


                      You can also declare the @SQL variable as follow if you want to name the column



                      DECLARE @SQL NVARCHAR(MAX) = N'SELECT Operations = ';


                      Demo



                      As a final note (or warning), be careful for SQL Injection if your column has other strings as it maybe malicious commands.






                      share|improve this answer















                      '11-1' is a string and can't be converted to INT. You can do that as



                      SELECT (CAST (11-1) AS INT)


                      Or by using Dynamic SQL as



                      DECLARE @SQL NVARCHAR(MAX) = N'SELECT 11-1';

                      EXECUTE sp_executesql @SQL;


                      Or even



                      CREATE TABLE Operations(
                      ID INT IDENTITY(1, 1),
                      Operation VARCHAR(45)
                      );

                      INSERT INTO Operations VALUES
                      ('10-1'),
                      ('10+1'),
                      ('(5+5) - 5'),
                      ('5+5');

                      DECLARE @SQL NVARCHAR(MAX) = N'SELECT ';

                      SELECT @SQL = @SQL + Operation + ' ,'
                      FROM Operations

                      SET @SQL = LEFT(@SQL, LEN(@SQL) - 1);

                      EXECUTE sp_executesql @SQL;


                      Or if you want to get it as rows



                      SELECT @SQL = @SQL + Operation + ' UNION ALL SELECT '
                      FROM Operations

                      SET @SQL = LEFT(@SQL, LEN(@SQL) - 17);

                      EXECUTE sp_executesql @SQL;


                      You can also declare the @SQL variable as follow if you want to name the column



                      DECLARE @SQL NVARCHAR(MAX) = N'SELECT Operations = ';


                      Demo



                      As a final note (or warning), be careful for SQL Injection if your column has other strings as it maybe malicious commands.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 15 '18 at 14:08

























                      answered Nov 15 '18 at 13:14









                      SamiSami

                      8,99831242




                      8,99831242























                          0














                          you can try like below, separate both part by using substring then
                          convert it into int then add both



                          SELECT convert(int, substring('11-1',0, charindex('-', '11-1')))+
                          convert(int, substring('11-1',charindex('-', '11-1'), len('11-1')))


                          It will return 10






                          share|improve this answer


























                          • You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

                            – arahman
                            Nov 15 '18 at 13:18






                          • 2





                            @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

                            – Zaynul Abadin Tuhin
                            Nov 15 '18 at 13:19


















                          0














                          you can try like below, separate both part by using substring then
                          convert it into int then add both



                          SELECT convert(int, substring('11-1',0, charindex('-', '11-1')))+
                          convert(int, substring('11-1',charindex('-', '11-1'), len('11-1')))


                          It will return 10






                          share|improve this answer


























                          • You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

                            – arahman
                            Nov 15 '18 at 13:18






                          • 2





                            @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

                            – Zaynul Abadin Tuhin
                            Nov 15 '18 at 13:19
















                          0












                          0








                          0







                          you can try like below, separate both part by using substring then
                          convert it into int then add both



                          SELECT convert(int, substring('11-1',0, charindex('-', '11-1')))+
                          convert(int, substring('11-1',charindex('-', '11-1'), len('11-1')))


                          It will return 10






                          share|improve this answer















                          you can try like below, separate both part by using substring then
                          convert it into int then add both



                          SELECT convert(int, substring('11-1',0, charindex('-', '11-1')))+
                          convert(int, substring('11-1',charindex('-', '11-1'), len('11-1')))


                          It will return 10







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 15 '18 at 13:18

























                          answered Nov 15 '18 at 13:15









                          Zaynul Abadin TuhinZaynul Abadin Tuhin

                          16k21033




                          16k21033













                          • You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

                            – arahman
                            Nov 15 '18 at 13:18






                          • 2





                            @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

                            – Zaynul Abadin Tuhin
                            Nov 15 '18 at 13:19





















                          • You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

                            – arahman
                            Nov 15 '18 at 13:18






                          • 2





                            @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

                            – Zaynul Abadin Tuhin
                            Nov 15 '18 at 13:19



















                          You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

                          – arahman
                          Nov 15 '18 at 13:18





                          You have to think, will that work for other arithmetic equations though...? The OP may have more equations and not just a subtraction.

                          – arahman
                          Nov 15 '18 at 13:18




                          2




                          2





                          @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

                          – Zaynul Abadin Tuhin
                          Nov 15 '18 at 13:19







                          @arahman i answered according to sample as there no other description and my answer met the op's requirement. am i wrong?

                          – Zaynul Abadin Tuhin
                          Nov 15 '18 at 13:19













                          0














                          Use dynamic SQL to parse expression



                          DECLARE @result int, @sql nvarchar(max);
                          SET @sql = N'SELECT ' + '11 - 1';
                          EXEC sp_executesql @sql


                          or



                          DECLARE @result int, @sql nvarchar(max);
                          SET @sql = N'SELECT @result = ' + '11 - 1';
                          EXEC sp_executesql @sql, N'@result int OUTPUT', @result = @result OUTPUT
                          SELECT @result


                          This will return 10






                          share|improve this answer




























                            0














                            Use dynamic SQL to parse expression



                            DECLARE @result int, @sql nvarchar(max);
                            SET @sql = N'SELECT ' + '11 - 1';
                            EXEC sp_executesql @sql


                            or



                            DECLARE @result int, @sql nvarchar(max);
                            SET @sql = N'SELECT @result = ' + '11 - 1';
                            EXEC sp_executesql @sql, N'@result int OUTPUT', @result = @result OUTPUT
                            SELECT @result


                            This will return 10






                            share|improve this answer


























                              0












                              0








                              0







                              Use dynamic SQL to parse expression



                              DECLARE @result int, @sql nvarchar(max);
                              SET @sql = N'SELECT ' + '11 - 1';
                              EXEC sp_executesql @sql


                              or



                              DECLARE @result int, @sql nvarchar(max);
                              SET @sql = N'SELECT @result = ' + '11 - 1';
                              EXEC sp_executesql @sql, N'@result int OUTPUT', @result = @result OUTPUT
                              SELECT @result


                              This will return 10






                              share|improve this answer













                              Use dynamic SQL to parse expression



                              DECLARE @result int, @sql nvarchar(max);
                              SET @sql = N'SELECT ' + '11 - 1';
                              EXEC sp_executesql @sql


                              or



                              DECLARE @result int, @sql nvarchar(max);
                              SET @sql = N'SELECT @result = ' + '11 - 1';
                              EXEC sp_executesql @sql, N'@result int OUTPUT', @result = @result OUTPUT
                              SELECT @result


                              This will return 10







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '18 at 13:23









                              sergeserge

                              70147




                              70147























                                  0














                                  The example below shows how you could evaluate the T-SQL expression stored in a table column. However, I would advise against this approach unless the value is from a source that is fully trusted. Otherwise, there is a SQL injection risk.



                                  Also, this method cannot be used easily to return values from multiple rows in a single result. You would need a cursor to evaluate and insert each result into a temp table/variable and then select from the table as the final result.



                                  DECLARE @Foo TABLE(
                                  FooID int
                                  , Expression nvarchar(10) NOT NULL
                                  )
                                  INSERT INTO @Foo VALUES(1, '11-1');
                                  DECLARE @sql nvarchar(MAX) = N'SELECT ' + (SELECT Expression FROM @foo WHERE FooID = 1) + N' AS Result;';
                                  EXECUTE (@sql);





                                  share|improve this answer




























                                    0














                                    The example below shows how you could evaluate the T-SQL expression stored in a table column. However, I would advise against this approach unless the value is from a source that is fully trusted. Otherwise, there is a SQL injection risk.



                                    Also, this method cannot be used easily to return values from multiple rows in a single result. You would need a cursor to evaluate and insert each result into a temp table/variable and then select from the table as the final result.



                                    DECLARE @Foo TABLE(
                                    FooID int
                                    , Expression nvarchar(10) NOT NULL
                                    )
                                    INSERT INTO @Foo VALUES(1, '11-1');
                                    DECLARE @sql nvarchar(MAX) = N'SELECT ' + (SELECT Expression FROM @foo WHERE FooID = 1) + N' AS Result;';
                                    EXECUTE (@sql);





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      The example below shows how you could evaluate the T-SQL expression stored in a table column. However, I would advise against this approach unless the value is from a source that is fully trusted. Otherwise, there is a SQL injection risk.



                                      Also, this method cannot be used easily to return values from multiple rows in a single result. You would need a cursor to evaluate and insert each result into a temp table/variable and then select from the table as the final result.



                                      DECLARE @Foo TABLE(
                                      FooID int
                                      , Expression nvarchar(10) NOT NULL
                                      )
                                      INSERT INTO @Foo VALUES(1, '11-1');
                                      DECLARE @sql nvarchar(MAX) = N'SELECT ' + (SELECT Expression FROM @foo WHERE FooID = 1) + N' AS Result;';
                                      EXECUTE (@sql);





                                      share|improve this answer













                                      The example below shows how you could evaluate the T-SQL expression stored in a table column. However, I would advise against this approach unless the value is from a source that is fully trusted. Otherwise, there is a SQL injection risk.



                                      Also, this method cannot be used easily to return values from multiple rows in a single result. You would need a cursor to evaluate and insert each result into a temp table/variable and then select from the table as the final result.



                                      DECLARE @Foo TABLE(
                                      FooID int
                                      , Expression nvarchar(10) NOT NULL
                                      )
                                      INSERT INTO @Foo VALUES(1, '11-1');
                                      DECLARE @sql nvarchar(MAX) = N'SELECT ' + (SELECT Expression FROM @foo WHERE FooID = 1) + N' AS Result;';
                                      EXECUTE (@sql);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 15 '18 at 13:29









                                      Dan GuzmanDan Guzman

                                      24k31642




                                      24k31642






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320220%2fgetting-an-error-while-converting-11-1-to-int-in-sql-server%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Xamarin.iOS Cant Deploy on Iphone

                                          Glorious Revolution

                                          Dulmage-Mendelsohn matrix decomposition in Python