Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate function for SQLite

Does there exist for SQLite something similar as the TRUNCATE function from MySQL?

SELECT TRUNCATE(1.999,1); # 1.9
like image 872
sid_com Avatar asked May 18 '13 19:05

sid_com


2 Answers

There is no built-in function that would do this directly. However, you can treat the number as a string, search for the decimal separator, and count characters from that:

SELECT substr(1.999, 1, instr(1.999, '.') + 1);

(This does not work for integers.)

like image 43
CL. Avatar answered Sep 18 '22 02:09

CL.


You can do this numerically by converting to an int and back:

select cast((val * 10) as int) / 10.0

You can do this using round() and subtraction:

select round(val - 0.1/2, 1)

Or, as another answer suggests, you can convert to a string.

like image 65
Gordon Linoff Avatar answered Sep 20 '22 02:09

Gordon Linoff