Search code examples
sqlsql-serversql-server-2014sqlcommand

SQL change word command


How to change the location of the letters 5 and 6 in sql. For example:

word:weather  new word:weatehr

Solution

  • You can try to use SUBSTRING with LEN function to make it.

    CREATE TABLE T(
       col varchar(40)
    );
    
    INSERT INTO T VALUES ('weather')
    

    Query 1:

    SELECT 
     col 'word' ,CONCAT(SUBSTRING(col,1,4),SUBSTRING(col,6,1),SUBSTRING(col,5,1),SUBSTRING(col,7,LEN(col) - 5)) 'new word'
        FROM T
    

    Results:

    |    word | new word |
    |---------|----------|
    | weather |  weatehr |