Why my string to decimal conversion failed in Mysql -


query:

select dsr,   replace(json_extract(json_extract(dsr, "$.*"), "$[0]"), '%', '') ret1,   convert(replace(json_extract(json_extract(dsr, "$.*"), "$[0]"), '%', ''), decimal(10,2)) ret2 shops dsr not null limit 1; 

result:

dsr: {"x": "44.92%", "y": "36.98%", "z": "27.10%"} ret1: "44.92" ret2: 0.00 

i tried convert ret1("44.92") decimal, gives 0.00, 44.92 expected.

could 1 please give me suggestions?

convert() not general-purpose text parsing solution, makes conversions when they're straightforward. ignores leading white-space , trailing non-numeric chars, that's all:

select convert('44.92', decimal(10,2)), convert('44.92cm', decimal(10,2)), convert('   44.92', decimal(10,2)), convert('mmm44.92', decimal(10,2)), convert('"44.92"', decimal(10,2)); 
+-------+-------+-------+------+------+ | 44.92 | 44.92 | 44.92 | 0.00 | 0.00 | +-------+-------+-------+------+------+ 

so should rid of leading double-quote rather trailing percent sign:

select convert(replace('"44.92%"', '"', ''), decimal(10,2)); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -