Gateway Error 500 on SQL Query

I have the following query which runs fine in MSSQL Management Studio, but will not even let me accept the query in Ignition.

Select COALESCE(CAST (CASE WHEN (DatePart(mi,max(t_stamp) - min(t_stamp))) <= 0 THEN 0 ELSE (max(Good_Bags)-min(Good_Bags)) / cast((DatePart(mi,max(t_stamp) - min(t_stamp)))as decimal(4,1)) END AS DECIMAL(4,1)),0) as "BPM" FROM BagCount WHERE EquipID like '%MET_02' and t_stamp > DateAdd(mi,-5,GetDate()) and GOOD_BAGS > 0

Any Ideas? I will need to replace the hard coded EquipID with a Tag, but I can handle that if the query will run otherwise.

Are you using this query in a script or in binding? If you are using it in scripting you need to escape the % in the like clause with another % only if you are doing string formatting using %s or %d. You also want to make sure you escape the single or double quotes depending on what you are using for the string.

I am trying to create a new SQL Tag based on this query. Can you show me what the escaped query would look like? I haven’t used that function before and seem to be struggling with the syntax. I think the help file assumes I am smarter that I really am. :scratch:

Oh, in that case you need to make sure your db connection is the default for the SQLTags provider. You can check this in the gateway config section under SQLTags Providers -> Realtime by editing your provider.