问题描述
我有一个 Char(15)
字段,在这个字段中我有以下数据:
I have a Char(15)
field, in this field I have the data below:
94342KMR
947JCP
7048MYC
我需要打破这个,我需要得到最后的右边 3 个字符,我需要得到左边的任何东西.我的问题是左边的代码并不总是和你看到的一样长.
I need to break down this, I need to get the last RIGHT 3 characters and I need to get whatever is to the LEFT. My issue is that the code on the LEFT is not always the same length as you can see.
如何在 SQL 中完成此操作?
How can I accomplish this in SQL?
谢谢
推荐答案
SELECT RIGHT(RTRIM(column), 3),
LEFT(column, LEN(column) - 3)
FROM table
使用 RIGHT
和 RTRIM
(以避免固定长度列的复杂性),以及 LEFT
与 LEN
(只抓取你需要的,不包括最后 3 个字符).
Use RIGHT
w/ RTRIM
(to avoid complications with a fixed-length column), and LEFT
coupled with LEN
(to only grab what you need, exempt of the last 3 characters).
如果出现长度 CASE 语句,以便 LEFT
调用不会'不要贪心.
if there's ever a situation where the length is <= 3, then you're probably going to have to use a CASE
statement so the LEFT
call doesn't get greedy.
这篇关于我如何使用 LEFT &SQL 中的 RIGHT 函数获取最后 3 个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!