0

Is there a way to use a regex for defining all the columns in a select statement. something like

select myColumnPrefix* from myTable

that would show all columns that start with myColumnPrefix?

2 Answers 2

3

Not using qSQL, however you can use a regex to get the column names and then use a functional select. For example,

c: cols[myTable] where cols[myTable] like "myColumnPrefix*";
?[myTable;();0b;c!c]

Or as a one-liner,

?[myTable;();0b;{x!x@:where x like "myColumnPrefix*"} cols myTable]
Sign up to request clarification or add additional context in comments.

Comments

1

If you really want to use qSQL (not advised - difficult to read/maintain) then you could expand on what @ostewart suggested:

Define table:

q)t:([]foo1:1 2;foo2:3 4;foo3:5 6;bar1:1 2;bar2:3 4;bar3:5 6)
q)t
foo1 foo2 foo3 bar1 bar2 bar3
-----------------------------
1    3    5    1    3    5
2    4    6    2    4    6

Extract columns of interest and prepare as string:

q)c:", " sv string cols[t] where cols[t] like "foo*";
q)c
"foo1, foo2, foo3"

Join columns to select query and value expression:

q)value "select ",c," from t"
foo1 foo2 foo3
--------------
1    3    5
2    4    6

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.