DEV Community

Márlon Medeiros Brum
Márlon Medeiros Brum

Posted on

Buildmystring.com - Generate your string concatenation code

If you work with SQL sentences in your application, you probably write your query in a SQL Tool before, to develop and test it. You will write this query using line breaks and identation and end up with something like that:

select 
    t1.Field1, t1.Field2, t2.Field3, t2.Field4
from Table1 t1
    inner join Table2 t2 on t1.JoinField1 = t2.JoinField1
    inner join Table3 t3 on t1.JoinField2 = t3.JoinField2
where
    t1.Field1 = 'Condition' and t2.Field3 = 'Condition'
order by
    t1.OrderField

Well, now you'll have to move this query to your application code. If its a language that allows line breaks in the middle of the string, like php, you could just add a " before and after the string:

$sql = "select 
    t1.Field1, t1.Field2, t2.Field3, t2.Field4
from Table1 t1
    inner join Table2 t2 on t1.JoinField1 = t2.JoinField1
    inner join Table3 t3 on t1.JoinField2 = t3.JoinField2
where
    t1.Field1 = 'Condition' and t2.Field3 = 'Condition'
order by
    t1.OrderField";

But if you're using a language that don't allow linebreaks inside the string, like C#, you'll have to close the string at each line and reopening at the next line:

string sql = "select  " +
"    t1.Field1, t1.Field2, t2.Field3, t2.Field4 " +
"from Table1 t1 " +
"    inner join Table2 t2 on t1.JoinField1 = t2.JoinField1 " +
"    inner join Table3 t3 on t1.JoinField2 = t3.JoinField2 " +
"where " +
"    t1.Field1 = 'Condition' and t2.Field3 = 'Condition' " +
"order by " +
"    t1.OrderField ";

That's where this site I've just discoverd helps. In https://www.buildmystring.com/, you inform your multiline strings, and it generates the code for you, with all the concatenations you'll need. You can choose your language (C#, Java, Vb.NET, c++ or SQL), a series of additional options and it will generate the code for you.

I know its not that difficult to do that by yourself, but it will save you some work.

Top comments (0)