How can I add a character into a specified position into string in SQL SERVER?

前端 未结 9 2395
傲寒
傲寒 2021-02-19 07:48

I have a varchar field like:

195500
122222200

I need to change these values to:

1955.00
1222222.00
相关标签:
9条回答
  • 2021-02-19 07:57

    Please Try : select reverse(stuff(reverse(columnName),3,0,'.') ) from yourTable

    0 讨论(0)
  • 2021-02-19 07:57

    declare @a varchar(10) = 'aaa' select concat(@a,'.00')

    0 讨论(0)
  • 2021-02-19 08:05

    Query:

    SELECT col,
           LEFT(col,len(col)-2) + '.' + RIGHT(col,2) as newcol
    FROM Table1
    

    Result:

    |       COL |     NEWCOL |
    |-----------|------------|
    |    195500 |    1955.00 |
    | 122222200 | 1222222.00 |
    
    0 讨论(0)
  • 2021-02-19 08:08

    Please see the following code. You can choose the symbols and index in variable.

     declare @index int,@sym varchar(10)
     set @sym='#'
     set @index=2
     select left(195500,@index) +''+@sym+''+right(195500,len(195500)-@index) 
    
    0 讨论(0)
  • 2021-02-19 08:10

    try this

    Declare @s varchar(50) = '1234567812333445'
    Select Stuff(@s, Len(@s)-1, 0, '.')
    --> 12345678123334.45
    

    fiddle demo

    0 讨论(0)
  • 2021-02-19 08:14

    Please try:

    select 
       Col,
       REVERSE(STUFF(REVERSE(Col), 1, 2, LEFT(REVERSE(Col), 2)+'.'))
    from YourTable
    

    SQL Fiddle Demo

    0 讨论(0)
提交回复
热议问题