Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To remove double quotes from date string in SQL

I am using JSON_EXTRACT() to retrieve the date from json.

I want to get the date without double quotes.

Here is the example of what I am doing :

JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number") as xyz

my_number holds date string as "2016-01-01 11:31:25", I want this without the double quotes.

I tried using timestamp as :

timestamp(JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number"))

but it is returning a null value to xyz.

Thanks.

like image 825
Ankur Rathore Avatar asked Oct 18 '16 07:10

Ankur Rathore


People also ask

How do you remove double quotes from a string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How do you remove a quote in SQL?

The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.

How do you handle double quotes in SQL query?

Use two single quotes to escape them in the sql statement. The double quotes should not be a problem: SELECT 'How is my son''s school helping him learn?

How can remove double quotes from column in SQL Server?

REPLACE(item_description, '"', '') will absolutely remove any " from the column.


2 Answers

Try

JSON_EXTRACT_SCALAR(JSON_EXTRACT(events, "$.my_member"), "$.my_Number")

Also, you should be able to further "optimize" your expression by building proper JSON Path and using JSON function only ones. See "hint" below

SELECT 
  JSON_EXTRACT_SCALAR(
    '{"my_member":{"my_Number":"2016-01-01 11:31:25"}}', 
    "$.my_member.my_Number"
  )  

See more details and also difference between JSON_EXTRACT_SCALAR and JSON_EXTRACT at JSON functions

like image 199
Mikhail Berlyant Avatar answered Sep 22 '22 05:09

Mikhail Berlyant


Run REPLACE

REPLACE(JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number"),"\"","") as xyz
like image 44
Pentium10 Avatar answered Sep 22 '22 05:09

Pentium10