How to compare text in sql server

  • I would like to detect any changes done on update to a text column 'comments' through a trigger.

    The column should be no longer than 200 characters.

    Do I define the column as varchar or text ?

    In my trigger: Would old.comments <> new.comments be okay. Is there a better more efficient way to do the comparison ?

    Thanks

  • Field type text is deprecated, I would definitely use VARCHAR or NVARCHAR. ntext, text, and image (Transact-SQL)

    Are you going to be comparing any other fields as well? As you're on 2016, have you considered Temporal (History) Tables instead of a trigger?

    In a simple answer, yes, where new != old is fine, but thought best to bring up other solutions.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • so I'm assuming you want a soft fix to the issue, where you are detecting and maybe truncating in the trigger to make it no more than 200 characters, even though the column supports more?

    adding a hard fix, like setting the column size to 200 is not an option, right?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I haven't decided. What's your suggestion ?

  • See if this is helpful to you .

    https://msdn.microsoft.com/en-us/library/ms186329.aspx

    The article has an example that makes use of a audit table. I have to wonder if you are not concerned about trimming important data from a field. Why does the field have to be less than 200 characters in the table of the database?

    ----------------------------------------------------

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply