我如何使用 LEFT &SQL 中的 RIGHT 函数获取最后 3 个字符? [英] How can I use LEFT & RIGHT Functions in SQL to get last 3 characters?

查看:40
本文介绍了我如何使用 LEFT &SQL 中的 RIGHT 函数获取最后 3 个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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

使用 RIGHTRTRIM(以避免固定长度列的复杂性),以及 LEFTLEN(只抓取你需要的,不包括最后 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).

如果出现长度 <= 3 的情况,那么您可能必须使用 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 &amp;SQL 中的 RIGHT 函数获取最后 3 个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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