如何删除表中特定列的第一个字符? [英] How do I remove the first characters of a specific column in a table?

查看:23
本文介绍了如何删除表中特定列的第一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQL 中,如何删除表中特定列的值的前 4 个字符?列名称为 Student Code,示例值为 ABCD123Stu1231.我想从我的表中删除所有记录的前 4 个字符

In SQL, how can I remove the first 4 characters of values of a specific column in a table? Column name is Student Code and an example value is ABCD123Stu1231. I want to remove first 4 chars from my table for all records

请指导我

推荐答案

SELECT RIGHT(MyColumn, LEN(MyColumn) - 4) AS MyTrimmedColumn

解释一下,RIGHT 需要 2 个参数 - 要操作的字符串(或列),以及要返回的字符数(从字符串的右侧"开始).LEN 返回列数据的长度,我们减去 4,以便我们的 RIGHT 函数将最左边的 4 个字符留在后面".

To explain, RIGHT takes 2 arguments - the string (or column) to operate on, and the number of characters to return (starting at the "right" side of the string). LEN returns the length of the column data, and we subtract four so that our RIGHT function leaves the leftmost 4 characters "behind".

希望这是有道理的.

再次编辑 - 我刚刚阅读了 Andrew 的回复,他的解释很可能是正确的,我可能会弄错.如果是这种情况(并且您想更新表而不是仅仅返回修改后的结果),您可以这样做:

Edit again - I just read Andrew's response, and he may very well have interperpereted correctly, and I might be mistaken. If this is the case (and you want to UPDATE the table rather than just return doctored results), you can do this:

UPDATE MyTable
SET MyColumn = RIGHT(MyColumn, LEN(MyColumn) - 4)

他在正确的轨道上,但他的解决方案将保留字符串开头的 4 个字符,而不是丢弃所述 4 个字符.

He's on the right track, but his solution will keep the 4 characters at the start of the string, rather than discarding said 4 characters.

这篇关于如何删除表中特定列的第一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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