BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
qt
qt
Community Trekker

How to use column name as a variable.

I want to select some rows from a column ( I have a column named as 'RESOLUTION' in the current data table)

This two lines will work:

======================================

dt = Current Data Table();

dt<<Select Where(RESOLUTION > 0.);

======================================

If I define 'RESOLUTION' as another variable, it doesn't work:

============================================

dt = Current Data Table();

colA="RESOLUTION";

dt<<Select Where(column(dt, colA) > 0.);

============================================

This will not select any row.

Could someone kindly tell me how could I do this?

Thanks.

0 Kudos
3 REPLIES 3
pmroz
Super User

Re: How to use column name as a variable.

qt,

This will do the trick.

dt = Current Data Table();

colA = "Resolution";

col_expr = "dt << Select Where(:name(\!"" || colA || "\!") > 0.)";

eval(parse(col_expr));

Basically you're creating some dynamic code, and then executing it.

0 Kudos
qt
qt
Community Trekker

Re: How to use column name as a variable.

It works!!

Many thanks!

0 Kudos
David_Burnham
Super User

Re: How to use column name as a variable.

Alternatively use the AsColumn function:

colA="RESOLUTION";

dt<<Select Where(AsColumn(colA) > 0.)

-Dave
0 Kudos