1

I need to query the database row results is about 300,000 rows (growing).

The call is made from a controller in my main view ( MVC .net core) One of the columns comes in this format:

  • [ ] The cat is black
  • [ ] here is the change
  • [G ] Some other text

I need to strip the [ ] or [G ]

Would I removed it at database level eg:

SELECT [Id]
      ,[Name]
      ,[Text] =  Right([Text], LEN([Text]) - 8) 
FROM MyDatabase

This seems incorrect – although it works – I have to put a magic number 8. What if this changes in the future? Developers insist in not having numbers that have no meaning?

Or do it in the controller :

viewModel.List = data.Select(val => new ViewModel
                    {
                        Id = val. Id,
                        Name = val.Name,
                        Text = val.Text.Substring(val.Text.IndexOf(']'))
                    }).ToList();

If you can shade some light or have a better idea I would appreciate it.

7
  • 1
    This is very complicated as you might not know what happens in the future with the table or the code. I would recommend cleaning up the table, and work your magic when data gets inserted, unless those are not garbage Commented Feb 3, 2020 at 10:25
  • Thank you - but I cannot clear the database table as data is important. I have no access on how data or when is inserted. Commented Feb 3, 2020 at 10:29
  • I said clean up the data, not clear the data. If the data is important how can you clean it in a query or controller? Commented Feb 3, 2020 at 10:40
  • Ok sorry - when data is displayed on the view they dont want to see [G] or [] as it is not relevant anymore. Commented Feb 3, 2020 at 10:43
  • Understood. The question remain, is this data relevant? (referring to the [] or [G] Does it have to stay in the database. If so. Will the way this is stored change in the future? Can these characters be somewhere else in the text? Commented Feb 3, 2020 at 10:46

2 Answers 2

3

Based on your usage of = and [] It's a pretty safe guess that the database you're using is SQL Server.
In that case, you could do it in the database with charindex - instead of 8 use charindex(']', [Text]):

SELECT [Id]
      ,[Name]
      ,[Text] =  RIGHT([Text], LEN([Text]) - CHARINDEX(']', [Text])) 
FROM MyDatabase
Sign up to request clarification or add additional context in comments.

5 Comments

Yes sorry - I am using SQL server
@Zohar the problem will remain, if there is data that did not start with a [, but there might be a [ in the data somewhere else, it will remove relevant data that is not intentional. Doing this is dangerous, and should be avoided.
@Jaques this can be avoided using all kind of techniques, but the OP didn't specify data without the [] or [G ], so...
I will try this solution, will let you know if it gets approved. Thanks
Thank you - it got approved :-)
1

Safer alternative with REPLACE :

SELECT [Id],
       [Name],
       [Text] = REPLACE(REPLACE([Text], '[] ', ''), '[G ] ', '') 
FROM MyDatabase

or probably a bit more efficient in C# :

Text = val.Text.Replace("[] ", "").Replace("[G ] ", "")

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.