user3800799
user3800799

Reputation: 538

How to save a string as decimal in mysql?

I have a table called "Prices" with one field name "value" and set to decimal. I want to be able to insert a string such as "3.5$" and that mysql will insert the number "3.5". No success so far.

Upvotes: 1

Views: 293

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Use floatval

$NumberWithString = 3.5$;
floatval($NumberWithString);

Example

<?php
    $var = '122.34343The';
    $float_value_of_var = floatval($var);
    echo $float_value_of_var; // 122.34343
?>

EDIT 01

Or use preg_replace

$newNumber = preg_replace('/&#36;/', '', $NumberWithString);

Upvotes: 1

Hotdin Gurning
Hotdin Gurning

Reputation: 1819

update yourtable 
set yourdecimal = cast('3.7$' as decimal(10,2)) 
where id = 1

Upvotes: 1

Related Questions