Skip to content
Advertisement

How to Fetch specific number of characters from a string using gorm?

I am using SQLite, if I have a text post or an article that is a 400 character string for example, I want to extract only the first 100 character from it to be used in the front-end.

In line 4 i extracted the latest 5 posts from the db, but i want to limit the body of the post to the first 100 characters only

How can i do that ?

Advertisement

Answer

What you want is to either use Select or Raw to run the substr sqlite function on the post’s body.

Like this:

Or with Raw:

Key aspect to remember is to make sure the column is named body so that scanning into your model works as before.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement