Reputation: 230
Using below casting query to convert number as text in my application.
Select CAST(Orderid as TEXT) from orders
If i run this i am getting output like 1234.0 1098.0
Instead of this i need to get the result like 1234 1098
I have tried the below query. If i use the below query, it returns the result in number format instead of string format in datatable.
Select CAST(orderid as string) from orders
How to achieve this through query? It should satisfy the following cases too.
Actual Value
12345
12345.234
12345.023
Expected value
12345
12345.234
12345.023
Please suggest a way to get result as string type when converting number as text in Sqlite. Thanks in advance.
Upvotes: 1
Views: 522
Reputation: 180080
You can check whether removing the fractional digits would change the value:
SELECT CAST(CASE WHEN CAST(orderid AS INTEGER) = orderid
THEN CAST(orderid AS INTEGER)
ELSE orderid
END AS TEXT)
FROM orders;
Upvotes: 3