how do I wrap an sql quesry?

3 views (last 30 days)
David malins
David malins on 6 Mar 2012
Hi I have an SQL query that I wish to split over several lines. However, because the quesry needs to go inside quotes I have tried terminating each line with and closing quote and the three dots '...
This is giving a parsing error. Can someone help please
SELECT T1.TIME, MAX(CASE WHEN T2.ID = 1 THEN CAST(T1.VALUE AS FLOAT32) ELSE NULL END) AS VALUE1, MAX(CASE WHEN T2.ID = 2 THEN CAST(T1.VALUE AS FLOAT32) ELSE NULL END) AS VALUE2, MAX(CASE WHEN T2.ID = 3 THEN CAST(T1.VALUE AS FLOAT32) ELSE NULL END) AS VALUE3 FROM PIARCHIVE..PIINTERP2 T1 INNER JOIN (SELECT 1 ID, 'WWL_T1_WindSpeed_10MIN_AVG' TAG UNION SELECT 2 ID, 'WWL_T2_WindSpeed_10MIN_AVG' TAG UNION SELECT 3 ID, 'WWL_T3_WindSpeed_10MIN_AVG' TAG) T2 ON T2.TAG = T1.TAG WHERE T1.TAG IN ('WWL_T1_WindSpeed_10MIN_AVG', 'WWL_T2_WindSpeed_10MIN_AVG', 'WWL_T3_WindSpeed_10MIN_AVG') AND T1.TIME BETWEEN '*-7d' AND '*' AND TIMESTEP = '10m' GROUP BY T1.TIME ORDER BY T1.TIME
cheers dave

Accepted Answer

the cyclist
the cyclist on 6 Mar 2012
Here is my technique for wrapping SQL queries:
queryText = '';
queryText = [queryText,'select field from table ']; % Note the space at the end
queryText = [queryText,'where constraint is true '];
queryText = [queryText,'and another_constraint is true '];
queryText = [queryText,'order by field '];
This produces one long string with the whole query, but keep the different parts of the query manageable. It is also convenient for being able to paste the query into other software for checking.
Also, bear in mind that for the single quotes that appear inside the query, you will need to double those for MATLAB to parse it correctly. For example
queryText = ['select * from table where name=''the cyclist'' ']

More Answers (1)

David malins
David malins on 27 Apr 2012
Hi there, thank you for the help with SQL wrapping, works a treat
regards Dave

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!