Replace the first occurrence of "." in sql line

Suppose we saved the tablefollowing values ​​in Columnas 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 can one replace only the first occurrence of '.' (period) from a string?

Example: for -9.827.08should be-9827.08

I tried with the function stuffbut this will not work for-80.000.00

 select stuff( ValuesT ,3,1,'') from TableT
+5
source share
2 answers

Use function STUFF

Find the first space .with CHARINDEXand remove it withSTUFF

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

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 bit more code, but just as effective. There you can do a lot with this technique.

+1
source

Source: https://habr.com/ru/post/1661432/


All Articles