0

I need to output from a custom SQL query a string that includes a newline into a custom field of a fragment template. I was hopping that something like this would work:

Select concat('\nHello World', field1) from table1

... except is doesn't. I just get the '\nHello World' string in the doc without the line break. I also already tried with char(10), char(13) and a lot of other (instead of \n) and the result is the same.

How can I add a newline at the beginning of my field in an SQL fragment template?

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
vascobnunes
  • 211
  • 1
  • 10
  • have you tried with a non-leading line break? `concat(hello,char,char, world, field1)` – lptr Nov 26 '20 at 18:33
  • You need to clarify how you are pulling and using this data. It seems likely your front end is replacing/displaying these characters not as you would wish. – Dale K Nov 26 '20 at 20:12
  • In SQL `\n` isn't special in any particular way, just a normal string like any other, unlike in C-based languages which use the backslash as an escape character. Be sure to not to mix both language concepts. – Alejandro Nov 26 '20 at 20:20

2 Answers2

2

CHAR(10) is a line break and CHAR(13) is a carriage return in SQL. '\n' is just "{backslash}n", it doesn't mean a new line. You would want:

SELECT CONCAT(CHAR(13),CHAR(10),'Hello World', field1)
FROM table1;

db<>fiddle

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • It does, @vascobnunes . I use that method ***all*** the time. Literally. I even have an [article](https://wp.larnu.uk/why-i-declare-my-carriage-returns-and-line-breaks-in-dynamic-sql/) about it, and the fiddle demonstrates it working as well. – Thom A Nov 26 '20 at 18:05
  • @vascobnunes if you are copying the value from SSMS in the grid view, you *have* enabled [retain line breaks in SSMS](https://stackoverflow.com/a/53115559/2029983), right...? (I assume that's the IDE you're using. If it's ADS I recall it's enabled by default.) If it's something else, then the line breaks *are* there and whatever application you are using is stripping them (and you should be posting a separate question about that instead). – Thom A Nov 26 '20 at 18:06
  • I'm not copying. I'm using the query as a custom query to populate a custom field in a EA template fragment. I don't know what I'm missing but this "CONCAT(CHAR(13),CHAR(10),'Hello World', field1)" approach is really not working for me... :( – vascobnunes Nov 26 '20 at 18:20
  • Then the application, whatever it is (as you suggest it's not SSMS), is stripping them, @vascobnunes. As I said, I would suggest asking a new question about the application you are using, as the above works; that I can both guarantee and have evidenced. – Thom A Nov 26 '20 at 18:25
  • This is about Enterprise Architect. It behaves differently to any other know system on this world. Literally! – qwerty_so Nov 27 '20 at 10:30
  • *"This is about Enterprise Architect"* I'd already surmised that, which is why I suggested posts mg a new question about the application, as it wasn't SQL Server that was doing it. – Thom A Nov 27 '20 at 10:32
  • @Larnu it is already tagged with [enterprise-architect]. I don't see how a new question could be different from this one. EA uses a database as backend, and SQL Server is one of the options, so the [SQL Server] tag is on point as well. – Geert Bellekens Nov 27 '20 at 10:43
  • Yes, but you make no mention of the product in the question, in the question you only ask how to add the line breaks in SQL. That was the problem. People tag random things all the time on [so], so as there was no mention of EA, I ignored the tag as it seemed unrelated – Thom A Nov 27 '20 at 10:50
1

SQL Fragment

You can add newlines in an SQL Fragment on SQL Server using CHAR(10) in your query.

But EA helpfully trims your output and removes any and all whitespace at both ends of your field.

select ' ' + CHAR(10) + 'line 1' + CHAR(10) + 'line 2'  + CHAR(10) + ' ' as test

is stripped from leading and trailing whitespace resulting in just

line 1 
line 2 

The same happens with the TAB character.

Script fragment

An alternative to SQL fragments are Script fragments. With these fragments you return an XML string that is that printed in your template.

Unfortunately the exact same thing happens with Script fragments. EA still trims leading and trailing whitespace from the fields.

Document script fragment

As a last attempt to beat EA and keep the elusive whitespace in the template I tried a Document Script Fragment. This is a new(ish) feature (introduced in v13 or v14 I believe) where you call a script that returns raw RTF code.

So what I did was open up WordPad and type the content I needed to show up in the template enter image description here

Then saved it as .rtf and opened the file with notepad and copied the rtf code

{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.18362}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\f0\fs22\lang9\par
\par
line 1\par
line 2\par
\par
}

Notice the two \par before the start of line 1

Then I create a script called DocumentScriptFragment in EA to return this exact string

option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Script Name: DocumentScriptFragment
' Author: Geert Bellekens
' Purpose: test a document script fragment by returning raw RTF
' Date: 2020-11-27
'
function MyRTFData()
    MyRTFData = "{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Calibri;}}" & vbNewLine & _
                "{\*\generator Riched20 10.0.18362}\viewkind4\uc1" & vbNewLine & _
                "\pard\sa200\sl276\slmult1\f0\fs22\lang9\par" & vbNewLine & _
                "\par" & vbNewLine & _
                "line 1\par" & vbNewLine & _
                "line 2\par" & vbNewLine & _
                "\par" & vbNewLine & _
                "}" & vbNewLine
end function

Created a document script template fragment that calls my script

enter image description here

And finally a template that uses my fragment

enter image description here

Using this template resulted finally in a document containing this so much needed leading newline enter image description here

Notice however how only one of the \par survived. So always add an extra \par at the beginning of your RTF.

Conclusion

  • Is it possible to add a leading newline in an SQL fragment? NO
  • Is it possible to use a Script fragment as an alternative? NO
  • Is it possible to use a Document script fragment as an alternative? YES as long as you add an extra \par at the beginning of your rtf code.
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50