Farrux Choriyev
Farrux Choriyev

Reputation: 329

How to update json data in postgresql database using laravel?

how to change next data in database:

jsonData = [{"given_date": "2 1.05. 2002 year"}] 

to

jsonData = [{"given_date": "21.05.2002"}]

Upvotes: 0

Views: 347

Answers (1)

TsaiKoga
TsaiKoga

Reputation: 13394

Postgresql update with regex:

UPDATE table
SET given_date = regexp_replace(given_date, '(\s|[a-zA-Z])', '','');

regexp_replace takes the value in given_date and be replaced by third parameter (empty string), according to the second regular parameter (match the spaces and alphabets). The fourth parameter is option like 'g(global)', 'i(ignore case)';

Postgresql regexp_replace reference

Laravel update with regex:

\DB::table('tablename')
->where(...)
->update([
'given_date' => \DB::raw("regexp_replace(given_date, '(\s|[a-zA-Z])', '','')")
]);

Upvotes: 1

Related Questions