• How do I remove the first characters of a specific column in a table?


    How do I remove the first characters of a specific column in a table?

    问题

    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

    Please guide me

    回答1

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

    Edit: 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".

    Hope this makes sense.

    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)

    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.

    回答2

    Stuff(someColumn, 1, 4, '')

    This says, starting with the first 1 character position, replace 4 characters with nothing ''

  • 相关阅读:
    (转)IDEA ERROR:找不到或无法加载主类
    Piggy-Bank
    Monkey and Banana
    Max Sum Plus Plus
    Doing Homework
    繁繁的游戏
    看试卷
    繁繁的队列
    大整数乘法
    文件操作(c++)
  • 原文地址:https://www.cnblogs.com/chucklu/p/16356061.html
Copyright © 2020-2023  润新知