Help on SQL syntax

Kalbubu 20 Reputation points
2023-06-27T13:02:08.3433333+00:00

I am trying to pass the sql query to pass to dynamic SQL. I keep getting error again and again. Please anyone who have idea please help me.

thank you

select * from openquery(DWH_medw1,	'SELECT activity.the_date AS Activity_Date, LOWER(tag.name) AS tag_name, count(activity.id) AS record_count FROM agencies.activity_activity AS activity INNER JOIN agencies.activity_activity_tags AS connection    ON activity.id = connection.activity_id INNER JOIN agencies.activity_activitytag AS tag     ON connection.activitytag_id = tag.id WHERE activity.franchisor_id IN (48, 61)   AND the_date >= DATE'2023-01-01'   AND (LOWER(tag.name) LIKE '%end of life care%'    OR LOWER(tag.name) LIKE '%hospital discharge%'    OR LOWER(tag.name) LIKE '%decline in mental ability%'    OR LOWER(tag.name) LIKE '%fall prevention%'    OR LOWER(tag.name) LIKE '%non-medical companion%')  GROUP BY activity.the_date,  LOWER(tag.name)'
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. LiHongMSFT-4306 31,616 Reputation points
    2023-06-28T01:45:23.7133333+00:00

    Hi @Kalbubu

    There is no DATE function in T-SQL. You may use CONVERT or CAST to convert an expression of one data type to another.

    Regarding the quotes, try this:

    select * from openquery
    (DWH_medw1,	
    'SELECT activity.the_date AS Activity_Date, LOWER(tag.name) AS tag_name, 
            count(activity.id) AS record_count 
     FROM agencies.activity_activity AS activity 
     INNER JOIN agencies.activity_activity_tags AS connection    
       ON activity.id = connection.activity_id 
     INNER JOIN agencies.activity_activitytag AS tag     
       ON connection.activitytag_id = tag.id 
     WHERE activity.franchisor_id IN (48, 61)   
       AND the_date >= ''2023-01-01''   
       AND (
            LOWER(tag.name) LIKE ''%end of life care%''    
            OR LOWER(tag.name) LIKE ''%hospital discharge%''    
            OR LOWER(tag.name) LIKE ''%decline in mental ability%''    
            OR LOWER(tag.name) LIKE ''%fall prevention%''    
            OR LOWER(tag.name) LIKE ''%non-medical companion%''
    	   )  
     GROUP BY activity.the_date,  LOWER(tag.name)')
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,586 Reputation points
    2023-06-27T13:07:42.7266667+00:00

    Syntax highlight already shows you the failure; you have to quote the apostrophe.

    And what is DATE? The is no T-SQL with this name

    User's image


  2. Kalbubu 20 Reputation points
    2023-06-27T13:23:39.8966667+00:00

    I am trying to pull data from linked amazon athena. could you please send me the correct queted one Please. tried to do that also.

    0 comments No comments

  3. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2023-06-27T21:32:39.16+00:00

    The problem is that your query string from Athena includes single quotes. But as Olaf's screenshot shows, the first single quote in the query string, terminates the string literal you are passing to OPENQUERY, and that results in a syntax error.

    No matter which language you work in, you always need to escape nested quotes in some way. In some languages do do this with the backslash, but in SQL you simply double it, as in this example:

    SELECT 'Brian O''Brien'
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.