Link to home
Start Free TrialLog in
Avatar of Shaggy1
Shaggy1Flag for Afghanistan

asked on

CONVERT varchar to integer contains commas display in GridView

I have a VARCHAR field that contains numbers representing SQ.FT. I would like to convert that to an INT and round up or down the number by 500. The results will be displayed in a GridView.

example of the field:  10,000 or 145,500....

Would really appreciate the help. I've been trying to CAST, MATH.ROUND, etc, just can't get anything to work.

Avatar of Mez4343
Mez4343

There is no varchar datatype in c#, so i assume it is a string data type. If so, you can just replace the commas like this.

string mystring = "145,000";
mystring = mystring.Replace(",", "");
Avatar of Shaggy1

ASKER

VARCHAR datatype is in SQL not C#. Writing a query in C#
Avatar of Shaggy1

ASKER

So I'm retreiving the value in FIELD1 which is VARCHAR. When i run the query, i need to convert that to an INT so that i can ROUND it up or down..

Thanks!
Hi,

Try this

SELECT CAST(ROUND(CAST(FIELD1 AS DECIMAL) / 500, 0) * 500 AS INT)
FROM yourtable

/peter
Once you have the value out of your database you could use the following, although it is quite ugly I must admit.
int converted = Convert.ToInt32(double.Parse("15,000"));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pivar
pivar
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shaggy1

ASKER

pivar,

If i already have the SELECT statement in place (SELECT DISTINCT) , can i still use the part CAST(ROUND(CAST etc..trying to add that in there and I am getting errors.

THANKS!

Yes, you should be able to do that. What is the errordetails?
SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial