如何将字符添加到 SQL SERVER 中的字符串中的指定位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20121323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:11:41  来源:igfitidea点击:

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

sqlsql-server

提问by Joao Paulo

I have a varchar field like:

我有一个 varchar 字段,如:

195500
122222200

I need to change these values to:

我需要将这些值更改为:

1955.00
1222222.00

采纳答案by Filipe Silva

If you want to add a '.' before the last two digits of your values you can do:

如果你想添加一个'.' 在您的值的最后两位数字之前,您可以执行以下操作:

SELECT substring(code,0,len(code)-1)+'.'+substring(code,len(code)-1,len(code))
FROM table1;

sqlfiddle demo

sqlfiddle demo

回答by vhadalgi

try this

try this

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

fiddle demo

小提琴演示

回答by Justin

Query:

询问:

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

Result:

结果:

|       COL |     NEWCOL |
|-----------|------------|
|    195500 |    1955.00 |
| 122222200 | 1222222.00 |

回答by Chris Rodriguez

CREATE TABLE #T ( Value VARCHAR(20) )
INSERT INTO #T ( Value ) VALUES ( 195500 ), ( 122222200)

SELECT 
    Value
,   NewValue = CONVERT(DECIMAL(17,2),CONVERT(DECIMAL,Value) / 100)
FROM #T

|     Value |   NewValue |
|-----------|------------|
|    195500 |    1955.00 |
| 122222200 | 1222222.00 |

回答by Arunkumar Sundar

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

请尝试:从 yourTable 中选择 reverse(stuff(reverse(columnName),3,0,'.'))

回答by TechDo

Please try:

请尝试:

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

SQL Fiddle Demo

SQL 小提琴演示

回答by Arun Singh

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) 

回答by Indranil.Bharambe

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

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