you could combine the Replace
and cast
function
SELECT CAST(REPLACE(Price_amount, ',', '') AS int) AS Price_Number FROM per_prices
문제
i have written a query in which i am fetching an amount which is a number like '50,000','80,000'.
select Price_amount
from per_prices
As these values contain ',' these are considered to be varchar
.Requirement is to to print these as 'number' with ','
that is how can '50,000' be considered as number and not varchar
다른 팁
If a value has anything other than numbers in it, it is not an integer it is string containing characters. in your case you have a string containing character 5
, 0
and ,
.
If this is what is stored in your database and this is what you want to display then go ahead you do not need to change it to Integer or anything else. But if you are doing some calculations on these values before displaying them, Yes then you need to change them to an Integer values. do the calculation. Change them back to the varchar datatype to show ,
between thousands and hundred thousands and display/select them.
Example
DECLARE @TABLE TABLE (ID INT, VALUE VARCHAR(100))
INSERT INTO @TABLE VALUES
(1, '100,000'),(2, '200,000'),(3, '300,000'),(4, '400,000'),
(1, '100,000'),(2, '200,000'),(3, '300,000'),(4, '400,000')
SELECT ID, SUM(
CAST(
REPLACE(VALUE, ',','') --<-- Replace , with empty string
AS INT) --<-- Cast as INT
) AS Total --<-- Now SUM up Integer values
FROM @TABLE
GROUP BY ID