From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Constructing A string with Repeated Words or Phrases

Solved!
Go to solution

I would like to create a vi that fills out a long line of text with repeated variables. Something like 

select t.[B], t.[C], t.[D]
from [A] t
inner join (
    select [B], max([C]) as Max[C]
    from [A]
    group by [B]
) tm on t.[B] = tm.[B] and t.[C] = tm.[C]

Although I could wire the string variables to a format into string block, is there a better way that would be:

 

  1. more easy to read
  2. less prone to error
  3. acknowledge that the inputs are just 4 inputs repeated 13 times
0 Kudos
Message 1 of 10
(4,104 Views)
Solution
Accepted by topic author ATE-EGNE

On the subject of #3, have you considered numbering your inputs?

 

From Format Into String's help:

 

By default, this function uses the order of the inputs to populate the format specifiers, or percent codes in the Format String. However, you can use a number followed by a dollar sign ($) within a percent code to specify exactly which input to use for that percent code. For example, the percent code %3$d uses the third input regardless of how many percent codes appear before %3$d in the format string.

For example...

 

fstr.png

---
CLA
Message 2 of 10
(4,060 Views)

Something like that:

replace.png

0 Kudos
Message 3 of 10
(4,057 Views)

@PiDi wrote:

Something like that:

replace.png


Yep! That will work fine, although I imagine it's significantly more resource intensive than concatenating the string. 

0 Kudos
Message 4 of 10
(4,029 Views)

@thoult wrote:

On the subject of #3, have you considered numbering your inputs?

 

From Format Into String's help:

 

By default, this function uses the order of the inputs to populate the format specifiers, or percent codes in the Format String. However, you can use a number followed by a dollar sign ($) within a percent code to specify exactly which input to use for that percent code. For example, the percent code %3$d uses the third input regardless of how many percent codes appear before %3$d in the format string.

For example...

 

fstr.png


Yes! This is exactly what I would be looking for!

0 Kudos
Message 5 of 10
(4,028 Views)

ATE-EGNE wrote:

Yep! That will work fine, although I imagine it's significantly more resource intensive than concatenating the string. 


Hmm... A quick benchmark (attached) shows that search&replace version works about 1/3 faster than format into string version.

Message 6 of 10
(4,006 Views)

Sounds about right, and I bet it scales better too as you increase the number of inputs.

 

If I knew the incoming data were always in the correct data format, and I were building a set of static SQL queries, I'd still pick Format Into String as it aids in enforcing datatypes for each input. Incidentally, this is currently what I do for production software rather than using stored procedures, as I can version control and validate any queries through application versions, and there's no obviously sensible method for managing SP versions.

 

If I were making a generic query tool, I'd go with the Search and Replace approach, with the data conversion external to it.

---
CLA
0 Kudos
Message 7 of 10
(3,992 Views)

@thoult wrote:

Sounds about right, and I bet it scales better too as you increase the number of inputs.

 

If I knew the incoming data were always in the correct data format, and I were building a set of static SQL queries, I'd still pick Format Into String as it aids in enforcing datatypes for each input. Incidentally, this is currently what I do for production software rather than using stored procedures, as I can version control and validate any queries through application versions, and there's no obviously sensible method for managing SP versions.

 

If I were making a generic query tool, I'd go with the Search and Replace approach, with the data conversion external to it.


I'm making a set of predefined queries to use in my .vi's that display data from a SQL server. Interesting that you mentioned stored procedures as I had thought about abandoning this in favor of using them. From what I understand, there's a significant improvement in efficiency when using stored procedures, is this not the case?

0 Kudos
Message 8 of 10
(3,936 Views)

I would use stored procedures. I just add them to source code control and they are fully versioned. They are much more flexible than embedding the SQL since you can change the layout of the DB without impacting your application. Think of them as a documented API to the DB. In addition, they are easier to have people use who are not very familiar with SQL. In addition, you can often off load quite a bit of processing to the DB server which is optimized for various things like joins and sorting. You can put quite a bit of complexity in the stored procedures and hide that from the applications who simply want to get/set data.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 9 of 10
(3,928 Views)

It's a horses for courses thing.

 

Yes, I whole-heartedly agree that leveraging the database server for processing rather than query-filter-query is ideal. And yes, you can version control your stored procedures...but you can't immediately state which version of the stored procedure is actually deployed on the system for any given application version.

 

For lots of SQL interactions, this isn't a problem. Many of our internal data viewing and reporting tools call standard procedures for statistical calculations. We also have scheduled tasks which run entirely on the server to populate tables and views.

 

For software which requires rigorous validation - e.g. production software for saleable products, especially ones with regulatory constraints such as Copy Exactly! and Functional Safety - my preference will almost always be to embed SQL queries in the application, to ensure that those versions are strictly controlled and part of the validation process. That doesn't preclude me from using more advanced queries techniques, mind!

 

I'm sure you've encountered similar issues in your role - I'd be genuinely interested to hear how you and others approach issues like this, maybe on Breakpoint.

---
CLA
0 Kudos
Message 10 of 10
(3,912 Views)