替换首次出现'。'在sql String中

时间:2021-04-08 16:55:46

Let's say we saved inside a table the following values on Column as String :

假设我们在Column中保存了以下列为String的值:

 Select ValuesT from TableT; 

  ValuesT
-9.827.08
-9.657.40
-80.000.00
-8.700.00
-8.542.43
-8.403.00

How could be replaced with nothing only the first occurance of '.' (dot) from the string?

怎么可能只被'。'的第一次出现所取代。 (点)从字符串?

Ex: for -9.827.08 should be -9827.08

例如:-9.827.08应为-9827.08

I tried with stuff function but this won't work for -80.000.00

我尝试使用stuff函数,但这不适用于-80.000.00

 select stuff( ValuesT ,3,1,'') from TableT

2 个解决方案

#1


12  

Use STUFF function

使用STUFF功能

Find the first occurance of . using CHARINDEX and remove it using STUFF

找到第一次出现。使用CHARINDEX并使用STUFF删除它

SELECT STUFF(valuesT, CHARINDEX('.', valuesT), 1, '')
FROM TableT

#2


1  

Another way.

其他方式。

WITH sampleData AS
( 
  SELECT val FROM (VALUES 
  ('-9.827.08'), ('-9.657.40'), ('-80.000.00'), ('-8.700.00'),
  ('-8.542.43'),('-8.403.00')) x(val)
)
SELECT SUBSTRING(val, 1, d1.d-1)+SUBSTRING(val, d1.d+1, 100)
FROM sampleData
CROSS APPLY (VALUES (CHARINDEX('.',val))) d1(d);

Its a little more code but just as efficient. There's a lot more you can do with this technique.

它的代码多一点,但同样有效。你可以用这种技术做更多的事情。

#1


12  

Use STUFF function

使用STUFF功能

Find the first occurance of . using CHARINDEX and remove it using STUFF

找到第一次出现。使用CHARINDEX并使用STUFF删除它

SELECT STUFF(valuesT, CHARINDEX('.', valuesT), 1, '')
FROM TableT

#2


1  

Another way.

其他方式。

WITH sampleData AS
( 
  SELECT val FROM (VALUES 
  ('-9.827.08'), ('-9.657.40'), ('-80.000.00'), ('-8.700.00'),
  ('-8.542.43'),('-8.403.00')) x(val)
)
SELECT SUBSTRING(val, 1, d1.d-1)+SUBSTRING(val, d1.d+1, 100)
FROM sampleData
CROSS APPLY (VALUES (CHARINDEX('.',val))) d1(d);

Its a little more code but just as efficient. There's a lot more you can do with this technique.

它的代码多一点,但同样有效。你可以用这种技术做更多的事情。