What is the best way to store a number in SQL SERVER so that if i have a number like...
192,234.35
It will get stored as such?
Will Numeric as DataType do the trick?
What is the best way to store a number in SQL SERVER so that if i have a number like...
192,234.35
It will get stored as such?
Will Numeric as DataType do the trick?
Don't store numerical data as text. I prefer decimal but anything is almost better than text. Here's a simple idea to consider.
declare @someDecimal decimal (8,2) = 192234.35
declare @someText varchar(16) = '192,234.35'
--here's how you would have to compare your text to an actual number
select 'True' where @someDecimal = convert(decimal(8,2),replace(@someText,',',''))
--then if you wanted to do any math on it, you would also have to convert it.
select convert(decimal(8,2),replace(@someText,',','')) * 2
--versus just
select @someDecimal * 2
--If you want to display it, use format...
select FORMAT(@someDecimal,'#,0.0000')
Ordering your data is where you would find big problems since it's done character by character. Again, this is why you don't store it as text.
declare @tableOfText table (d varchar(16))
insert into @tableOfText (d)
values
('001'),
('010'),
('10'),
('110'),
('011'),
('1'),
('12')
select * from @tableOfText order by d desc
Recommended way would be to store the number without commas as a DECIMAL type (assuming that in question, 192,234.35 is a single number) and for displaying it to user, just add commas to make the number readable.
However, you are free to store it like a VARCHAR, if you are not going to do any mathematical operation on it (like average, sum, etc.)
I would suggest to store it as decimal.
In SQL Server 2012+ we have the format function, which can be used for formatting with thousand separator
FORMAT(@d,'#,0.0000')
In prior version we can take recourse to some custom function