简体   繁体   中英

string concatenation and apostrophe marks

I have a long sql query that I am writing in MATLAB. To make it easier for people to read what is it doing I am trying to break the query on to a few lines rather than one long line.

Below is my code (names changed).

sql_statement = strcat('select some_feilds from'...
                       ,' databaseOne.tableOne'...
                       ,' join databaseTwo.tableTwo'...
                       ,' on databaseOne.tableOne.f1 = databaseTwo.tableTwo.f2'...
                       ,' where databaseTwo.tableTwo.f2 =','''', x_lbl(1, 1),'');

The issue I have is the x_lbl(1,1) needs an ' either side of it. ie f2 = 'Health'.

The code above gives me ''Health' which is rather fustrating as when I just the do the line below I get 'Health'. Maybe I'm going blind and losing my mind but can't see to get my query in the correct format?

 lbl = strcat('''', x_lbl(1,1), '''');

update

 x_lbl(1,1)
 ans = 'Health Care'


 sql_statement = [1x225 char]

that's the answer when using the last line,

,' where DVLP_QES.dbo.num_name_mapping.name = ','''', x_lbl(1, 1),'''');

If you need your sql_statement to finish like select [...] tableTwo.f2 ='Health' you need to add 2 more single quotes at the end of you strcat :

sql_statement = strcat('select some_feilds from'...
                       ,' databaseOne.tableOne'...
                       ,' join databaseTwo.tableTwo'...
                       ,' on databaseOne.tableOne.f1 = databaseTwo.tableTwo.f2'...
                       ,' where databaseTwo.tableTwo.f2 =','''', x_lbl(1, 1),'''');

I supposed x_lbl(1, 1) = Health without quotes, right?

EDIT:

Using MATLAB R2014a

>> x_lbl = 'Health';
>> x_lbl
x_lbl =
Health
>> sql_statement = strcat('select some_feilds from'...
                       ,' databaseOne.tableOne'...
                       ,' join databaseTwo.tableTwo'...
                       ,' on databaseOne.tableOne.f1 = databaseTwo.tableTwo.f2'...
                       ,' where databaseTwo.tableTwo.f2 =','''', x_lbl,'''');
>> sql_statement
sql_statement =
select some_feilds from databaseOne.tableOne join databaseTwo.tableTwo on databaseOne.tableOne.f1 = databaseTwo.tableTwo.f2 where databaseTwo.tableTwo.f2 ='Health'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM