Imagine scenario: need to strip out spaces from a string to be used lets say in url.

Now would it be smart to do this once before saving in DB, and than use raw db results when outputting. Or would it be smart to write helper function and just do space removal when outputting and keep data saved raw in DB?

  • You don't provide enough information for us to provide a meaningful answer. As it's currently written, your question is entirely opinion based.
    – MetaFight
    Commented Oct 29, 2016 at 20:13
  • 2
    You write the data once, but use it often. So it is better to clean the data once when storing instead of clean them every times you read them from DB.
    – knut
    Commented Oct 29, 2016 at 21:11

2 Answers 2


It makes more sense to cleanup the data and store it into the database instead of storing it "raw" and then cleaning it up on reads.

It's like with every other piece of information you store in a database:

  • you validate the data;
  • sanitize it if needed (in this case removing unnecessary spaces);
  • persist it;
  • if needed, assert it has the format you expect when you read it back.

So clean it up before saving it to the DB.


Try as much as you can not to store garbage in your database. If you can trim off leading or trailing spaces from a URL before inserting it into a database table, do it.

In the other hand, you could write a trigger in the table that does that before inserting the value.

Any solution can potentially be comprised of more that just one front end (one web front-end, one Android front-end, one iPhone front-end, etc.). So relying in "the app", assumming that no other front-end will ever, never exist to the same back-end is something we should not assume.

So my recommendation is try not to insert garbage into tables only to clean it before outputing it to the user. You could potentially end up writing space-stripping code in a whole lot of places when you output that value from DB instead of doing it in just before inserting.

  • 1
    Whoah there. If I came in to work one day and learned that another application was writing to my database without my knowledge I would stir up the appropriate shit storm. That's because, in order to make the complexity of development manageable, I would have set-up a private database. If other applications need my data then my team needs to be involved in providing it. That's to say, only in a really chaotic work environment will other people just randomly invalidate data in your database. And if that's common where you work, then consider getting another job!
    – MetaFight
    Commented Oct 29, 2016 at 20:17
  • @MetaFight You get me wrong. A technical solution is usually comprised of more that front end. I edited my answer to make that clear though. Commented Oct 29, 2016 at 20:20
  • Yes, you may have more than one front end (client) but you really should only have one server serving your data. And that server should be the place where data is guaranteed to be valid before entering the database.
    – MetaFight
    Commented Oct 29, 2016 at 20:23
  • @MetaFight By server you mean database server or application server? Commented Oct 29, 2016 at 20:25
  • Application server. I'll admit to having very little experience with web and app development, but is it common for web/app clients to write directly to the database without going through an application server?
    – MetaFight
    Commented Oct 29, 2016 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.