0

I have below code executing perfectly fine in Sybase database but same query is giving error in MsSQL server.

select
  column1,
  (
    Case when  ABC < 0 then CONVERT(varchar(50), Convert(int,PQR)) + "/" + CONVERT(varchar(50), Convert(int,ABC)) 
        else CONVERT(varchar(50), Convert(int,PQR)) 
    end
  )  as column2, 
  column3,
  column4
FROM SAMPLE_TABLE

Error is : Invalid column name '/'.

1
  • 8
    Put the slash in single quotes. Commented Jan 8, 2016 at 16:07

1 Answer 1

3

In MSSQL (and apparently is the standard - see comments), strings are encapsulated in single quotes, like this... 'here is a string'.

You double them up to escape them... 'here''s another string that includes a single quote'

Double quotes refer to objects. It's not commonly used, at least in my experience. I much prefer square brackets, but they are equivalent. For example, these two are exactly the same thing...

SELECT
    "Some Field"
FROM
    YourTable

and

SELECT
    [Some Field]
FROM
    YourTable

So, in your code, "/" is looking for a field called fowardslash. Simple correction...

select
  column1,
  (
    Case when  ABC < 0 then CONVERT(varchar(50), Convert(int,PQR)) + '/' + CONVERT(varchar(50), Convert(int,ABC)) 
        else CONVERT(varchar(50), Convert(int,PQR)) 
    end
  )  as column2, 
  column3,
  column4
FROM SAMPLE_TABLE
Sign up to request clarification or add additional context in comments.

3 Comments

This isn't actually SQL Server specific, this is how string literals are defined in the SQL standard.
@a_horse_with_no_name Thanks for the clarification. I never remember what is MSSQL specific and what deviates from the standard. I just know what works in MSSQL, and cringe every time I see a query with a backtick in it!
Well, I always cringe when I see a query with square brackets ;) (non-standard as well)

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.