SQL SUBSTRING vs RIGHT - Best Practice

后端 未结 4 1274
日久生厌
日久生厌 2021-02-07 00:06

I\'m trying to find out the best practice when removing characters from the start of a string.

In some languages, you can use MID without a length parameter however, in

相关标签:
4条回答
  • 2021-02-07 00:26

    I would use the right function as it better shows your intent. The substring function is best used for extracting strings from the middle of a longer string.

    0 讨论(0)
  • 2021-02-07 00:27

    Well, the first is more expressive of your intent. The last is clearly messy (hard-coded length etc). I doubt you'd find much performance difference between the first & second, so I'd use the simplest - RIGHT.

    Of course, if you are doing this lots, you could write a udf that encapsulates this - then if you change your mind you only have one place to change...

    0 讨论(0)
  • 2021-02-07 00:28

    I use the second SUBSTRING because it is more portable. Not all dbms had LEFT and RIGHT.

    0 讨论(0)
  • 2021-02-07 00:47

    +1 on the right function, it is much clearer what you want to do

    0 讨论(0)
提交回复
热议问题