2

When I'm trying to retrieve data by this, It works.

SELECT 
CONVERT(VARCHAR(10),BH.Doc_Date,(120))AS Doc_Date
,LGH.Node_Code
,LGH.Node_Name AS Location_Name
,GD.Nationality
,GD.Guest_Name
,DBOGD.Name AS Gender
,CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + ' yrs' AS Age
,'++' + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
,GD.NIC_No
,GD.Language
FROM [booking].[Guest_Details] AS GD ...
            

But when I'm trying to do like this,

SET @query = 'SELECT 
                CONVERT(VARCHAR(10)
                ,BH.Doc_Date,(120))AS Doc_Date
                ,LGH.Node_Code
                ,LGH.Node_Name AS Location_Name
                ,GD.Nationality
                ,GD.Guest_Name
                ,DBOGD.Name AS Gender
                ,(CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + " yrs") AS Age
                ,"++" + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
                ,GD.NIC_No
                ,GD.Language
                FROM [booking].[Guest_Details] AS GD ...'
            

It's not working. I'm getting Errors like:

Invalid column name ' yrs'.

Invalid column name '++'.

5
  • why have you switched to the " speech marks rather than '? Commented Jun 24, 2016 at 7:14
  • 1
    Instead of double quotes ", use two single quotes '' Commented Jun 24, 2016 at 7:16
  • I tried with single quotations also. But it didn't work. Commented Jun 24, 2016 at 7:23
  • Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes are usually used to object names (e.g. column_name as "Emp Name"). Commented Jun 24, 2016 at 7:36
  • Watch out for (and read up on) sql injection. Check out the quoteidentifier and similar functions. Commented Jun 24, 2016 at 9:09

3 Answers 3

2

Yrs part is causing you issue..Modify it like below

   ,DBOGD.Name AS Gender
                ,(CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + '' yrs'') AS Age
                ,CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
                ,GD.NIC_No
                ,GD.Language
                FROM [booking].[Guest_Details] AS GD ...'
Sign up to request clarification or add additional context in comments.

2 Comments

That works. Can u explain me about that. Better ' ' ' ' than " " . Btw Thank you very much.
@TKrish: your are tyring to use strings like in c#.in sql you have set strings with single quote and there was a redundant ++.always print your SQL so that you can catch errors yourself
1

Try this code, hope this helps you.

SET @query = 'SELECT 
                CONVERT(VARCHAR(10)
                ,BH.Doc_Date,(120))AS Doc_Date
                ,LGH.Node_Code
                ,LGH.Node_Name AS Location_Name
                ,GD.Nationality
                ,GD.Guest_Name
                ,DBOGD.Name AS Gender
                ,(CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + '' yrs'') AS Age
                ,''++'' + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
                ,GD.NIC_No
                ,GD.Language
                FROM [booking].[Guest_Details] AS GD ...'

5 Comments

That works. Can u explain me about that. Better ' ' ' ' than " " . Btw Thank you very much.
In SQL Server the Double Quotes are just consider as a String value, But Single Quotes are used to Hold the String values. That's the problem in your Code.
@DineshDB: In SQL Server the Double Quotes are just consider as a String value ..? this is not correct
@TheGameIsWar: Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes are usually used to object names (e.g. column_name as "Emp Name").
Hope my next comment is Correct.
1

Instead of double quotes ", use two single quotes '' in these places " yrs", "++".

SET @query = 
    'SELECT 
    CONVERT(VARCHAR(10), BH.Doc_Date,(120)) AS Doc_Date
    ,LGH.Node_Code
    ,LGH.Node_Name AS Location_Name
    ,GD.Nationality
    ,GD.Guest_Name
    ,DBOGD.Name AS Gender
    ,(CAST(DATEDIFF(yy, GD.Birth_Day,getdate()) AS NVARCHAR(100)) + '' yrs'') AS Age -- in this line
    ,''++'' + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile -- and in this line
    ,GD.NIC_No
    ,GD.Language
    FROM [booking].[Guest_Details] AS GD ...'

1 Comment

That works. Can u explain me about that. Better ' ' ' ' than " " . Btw Thank you very much.

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.