SQL SUBSTRING 与 RIGHT - 最佳实践 [英] SQL SUBSTRING vs RIGHT - Best Practice

查看:29
本文介绍了SQL SUBSTRING 与 RIGHT - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出从字符串开头删除字符时的最佳做法.

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

在某些语言中,您可以使用不带长度参数的 MID,但在 TSQL 中需要长度.

In some languages, you can use MID without a length parameter however, in TSQL the length is required.

考虑以下代码,最佳实践是什么?(十六进制字符串是可变长度的)

Considering the following code, what is the best practise? (The hex string is variable length)

DECLARE @sHex VARCHAR(66)
SET @sHex = '0x7E260F3DA734AD4BD816B88F485CE501D843DF067C8EDCB8AC7DAD331852E04D'

PRINT RIGHT(@sHex,LEN(@sHex) -2)
PRINT SUBSTRING(@sHex,3,LEN(@sHex) -2)
PRINT SUBSTRING(@sHex,3,65535)

推荐答案

嗯,第一个更能表达您的意图.最后一个显然是混乱的(硬编码长度等).我怀疑你会发现第一个 & 之间有很大的性能差异.第二,所以我会使用最简单的 - RIGHT.

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.

当然,如果你做了这么多,你可以写一个 udf 来封装这个——那么如果你改变主意,你只有一个地方可以改变......

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...

这篇关于SQL SUBSTRING 与 RIGHT - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆