Query browser reading tags to where condition

I am trying to read a tag into the query browser.
Getting errors.
I thought {} encapsulating the tag path was enough.

select top 1 Machine from SpecStandards
where Machine like concat('X',substring (
 str({[Main]Location/Sublocation/Machine/Model}),
1,len(str({[Main]Location/Sublocation/Machine/Model})-1))

I don’t think you can do that directly into a query browser. Make a test window and then make a sql query binding with the tag paths, or look at the actual values in the tags and put those into the query browser directly (instead of the tag reference).

Also, str() is a python thing, I don’t think any db uses that as a function keyword (though I may be wrong). Same with len. I know MySQL uses LENGTH() for instance.

SQL uses STR()

STR() Function in SQL Server - GeeksforGeeks

How do I read a tag in the query browser?

You can’t.

You must either use values directly, or create a test view.

2 Likes

Oh ok nice. Just mentioned it as maybe an issue.

But I don’t think you can use tag references directly in the query browser tool. It would be nice but I don’t think that’s possible.

Edit for conciseness:

What I am trying to do:
Read in a tag like, 310200 from the plc, and use a query to match with X310207 from a table.

select top 1 Machine from SpecStandards
where Machine like concat('X',ltrim(substring(str({[.]model2}),1,(len(str({[.]model2}))-1))))

Gives me null instead of X310207

select concat('X',ltrim(substring(str({[.]model2}),1,(len(str({[.]model2}))-1)))) from SpecStandards

This gives me the “X31020” as a tag value.

select 1 top Machine from SpecStandards
where Machine like '%{[.]model3}%'

This will give me “X310207” which is what I want.

However, I wanted to do it with just one query tag.

select top 1 Machine from SpecStandards
where Machine like '%concat('X',ltrim(substring(str({[.]model2}),1,(len(str({[.]model2}))-1))))%'

This errors.


Finally figured it out. Needed to concat the percent symbols, and didn’t need the ‘X’.

select top 1 Machine from SpecStandards
where Machine like concat('%',ltrim(substring(str({[.]model2}),1,(len(str({[.]model2}))-1))),'%')