0

This is driving me pretty nuts but cannot get it to work.

I want to paste the following into a SQL query within R.

UKWinnersID<-c("'1WKX6'", "'ULTY8'", "'JNZX0'", "'8J4D8'", 
"'KZJAJ0'", "'W8BH47'", "'CP8RPW9'", "'52TD5'", "'TLKV4'")



sqlQuery(myConn, paste("SELECT TOP 10000 [AxiomaDate]
                      ,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]
                      FROM [PortfolioAnalytics].[Data_Axioma]
                     Where  AxiomaID IN (",(paste(UKWinnersID, collapse = ","),")")))

I am using the paste function now which doesn't seem to work. Anyone got an idea? The input for the last line would be like this (with the data stored in a) for the query to work..

AxiomaID IN ('1WKX6', 'ULTY8', 'JNZX0', '8J4D8', 
'KZJAJ0', 'W8BH47', 'CP8RPW9', '52TD5', 'TLKV4')

The current output is

sqlQuery etc Where AxiomaID IN ( '1WKX6','ULTY8','JNZX0','8J4D8','KZJAJ0','W8BH47','CP8RPW9','52TD5','TLKV4' )'"

Essentially I want to remove the last two characters in this paste which is a ' and ".

2
  • try using paste(UKWinnersID, collapse = ",") instead of print. Commented Oct 2, 2017 at 13:54
  • This gets me close but not quite there - please see updated question above. I essentially need to remove the last two characters for the query to work. Commented Oct 2, 2017 at 14:06

2 Answers 2

1

I'm not able to recreate your exact output. Try the following; it produces the SQL code I would expect.

UKWinnersID<-c("'1WKX6'", "'ULTY8'", "'JNZX0'", "'8J4D8'", 
               "'KZJAJ0'", "'W8BH47'", "'CP8RPW9'", "'52TD5'", "'TLKV4'")

sqlQuery(myConn, paste("SELECT TOP 10000 [AxiomaDate]
                      ,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]
                       FROM [PortfolioAnalytics].[Data_Axioma]
                       Where  AxiomaID IN (",paste(UKWinnersID, collapse = ","),")"))
Sign up to request clarification or add additional context in comments.

1 Comment

Now my output is closer but there is still one ' at the end outside the bracket as : sqlQuery etc Where AxiomaID IN ( '1WKX6','ULTY8','JNZX0','8J4D8','KZJAJ0','W8BH47','CP8RPW9','52TD5','TLKV4' )'
0

I recommended a solution to a similar question. see Pasting Values in SQL Query through R

You could try defining a character variable with the query value. Then you can call function InsertListInQuery passing query and vector as it arguments as follow:

UKWinnersID<-c("'1WKX6'", "'ULTY8'", "'JNZX0'", "'8J4D8'", 
               "'KZJAJ0'", "'W8BH47'", "'CP8RPW9'", "'52TD5'", "'TLKV4'")

query <- "SELECT TOP 10000 [AxiomaDate]
                      ,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]
                      FROM [PortfolioAnalytics].[Data_Axioma]
                     Where  AxiomaID IN ()"

SQLQuery <- InsertListInQuery(query, UKWinnersID)

SQLQuery
[1] "SELECT TOP 10000 [AxiomaDate]\n,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]\nFROM [PortfolioAnalytics].[Data_Axioma]\nWhere  AxiomaID IN ( '1WKX6' , 'ULTY8' , 'JNZX0' , '8J4D8' , 'KZJAJ0' , 'W8BH47' , 'CP8RPW9' , '52TD5' ,'TLKV4')"

I hope it helps.

Comments

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.