# String concatenation issue using tag value in an SQL query

**URL:** <https://forum.inductiveautomation.com/t/string-concatenation-issue-using-tag-value-in-an-sql-query/107128>\
**Category:** Ignition\
**Created:** [June 6, 2025, 2:27pm UTC](https://forum.inductiveautomation.com/t/string-concatenation-issue-using-tag-value-in-an-sql-query/107128 "2025-06-06T14:27:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigl](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/craigl/32/5010_2.png) [@craigl](https://forum.inductiveautomation.com/u/craigl)\
**Post date:** [June 6, 2025, 2:27pm UTC](https://forum.inductiveautomation.com/t/string-concatenation-issue-using-tag-value-in-an-sql-query/107128/1 "2025-06-06T14:27:40Z")

</div>

I’m trying to create a database tag that using a string substitution from a tag value.

Here’s what I’m trying, but it doesn’t work. The only difference between these 2 statements is the tag value vs a string literal. I'm hoping I'm missing something obvious/easy.

This is the query from the dataset tag I'm trying to populate.

SELECT  
JSON\_VALUE(a.value, '$.areaName') AS AreaName,  
JSON\_VALUE(a.value, '$.equipPath') AS AreaPath,  
JSON\_VALUE(l.value, '$.lineName') AS LineName,  
JSON\_VALUE(l.value, '$.equipPath') AS LinePath  
FROM OPENJSON(  
(SELECT parameter FROM [dbo].[ProductionModel] WHERE name = concat( **'Marion'** ,'ProductionModel')),  
'$.areas'  
) AS a  
CROSS APPLY OPENJSON(a.value, '$.lines') AS l  
WHERE JSON\_VALUE(a.value, '$.areaDisplayOrder') \>= 0;

This doesn’t.

SELECT  
JSON\_VALUE(a.value, '$.areaName') AS AreaName,  
JSON\_VALUE(a.value, '$.equipPath') AS AreaPath,  
JSON\_VALUE(l.value, '$.lineName') AS LineName,  
JSON\_VALUE(l.value, '$.equipPath') AS LinePath  
FROM OPENJSON(  
(SELECT parameter FROM [dbo].[ProductionModel] WHERE name = concat(**{[.]Location}**,'ProductionModel')),  
'$.areas'  
) AS a  
CROSS APPLY OPENJSON(a.value, '$.lines') AS l  
WHERE JSON\_VALUE(a.value, '$.areaDisplayOrder') \>= 0;

The error from the second one results in **Invalid column name ‘Marion’** , so it’s reading the tag value but not concatenating it. The first one works fine, so concat works with two strings, but not if one value is from a tag.

What am I missing?

Thanks!!!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 6, 2025, 4:37pm UTC](https://forum.inductiveautomation.com/t/string-concatenation-issue-using-tag-value-in-an-sql-query/107128/2 "2025-06-06T16:37:05Z")

</div>

With a SQL tag, substitutions are done **before** the SQL is built.

That is, a tag substitution is not a safe parameterized substitution - it is a **direct** string replacement.  
`(SELECT parameter FROM [dbo].[ProductionModel] WHERE name = concat({[.]Location},'ProductionModel')),`  
Is becoming  
`(SELECT parameter FROM [dbo].[ProductionModel] WHERE name = concat(Marion,'ProductionModel')),`

You need to add the quotes around the tag value yourself:  
`(SELECT parameter FROM [dbo].[ProductionModel] WHERE name = concat('{[.]Location}','ProductionModel')),`  
So that the end result i what you want:  
`(SELECT parameter FROM [dbo].[ProductionModel] WHERE name = concat('Marion','ProductionModel')),`

---

<div class="post-metadata">

**Author:** ![craigl](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/craigl/32/5010_2.png) [@craigl](https://forum.inductiveautomation.com/u/craigl)\
**Post date:** [June 6, 2025, 5:21pm UTC](https://forum.inductiveautomation.com/t/string-concatenation-issue-using-tag-value-in-an-sql-query/107128/3 "2025-06-06T17:21:11Z")

</div>

That did the trick.

Ugh. I know I tried that at one point, but I must have had a typo or something. At least it's working now.

Thanks!!!!
