Reputation: 1051
For example I want to insert something like this into a table and have it retain the formatting properties of the text for later retrieval:
Ex.
**This** is a *sentence*.
Also, would it still work if the size of the letters are altered?
Upvotes: 1
Views: 28806
Reputation: 86
To store formatting, there will need to be a markup of some kind. HTML might work. If this will be displayed in a browser, sanitize the value to avoid any possible security issues.
If you use HTML, the literal stored value might be:
<strong>This</strong> is a <i>sentence</i>
Edit: noticed the WPF tag, which has different formatting markup from HTML. The idea is still the same.
<Bold>This</Bold> is a <Italic>sentence</Italic>
Edit2: Not sure why the WPF tag was removed and "formatted" example were changed in the question, lol. Stack Overflow seems like a strange place.
Since it's been brought up, yes, text with markup is not pure relational data, so it theoretically has no business being in a relational database.
However, realistically, there are scenarios that storing the formatting in an alternate way would be counterintuitive/costly (Content Management Systems with Rich Text Boxes that modify dynamic data come to mind)
So, it could be used as long as the developer knows what the consequences are, it's not used badly and blindly (throwing static/never changing content in DB instead of files), and he/she gets the blessing from his/her friendly neighborhood DBA.
Upvotes: 6
Reputation: 25132
Store it as NVARCHAR
and it will retain what ever you insert. If by size you mean length not something like FONT SIZE then this is limited by the size of the column you set. 8000 bytes is the max for NVARCHAR
or VARCHAR
which is NVARCHAR(MAX)
or NVARCHAR(4000)
Any visual formatting like font type, font size, color, emphasis, etc should be handled by your front end application. Remember SQL Server stores data... It's not a GUI. Thus, storing HTML or CSS formatting is poor practice and isn't what it is intended for.
Upvotes: 0