Is it possible to sort integers character by character?
So the list [110, 120, 10, 200, 20] would end up like this:
10,
110,
120,
20,
200
If you cast the integer as a varchar or text value, and order by that cast, it should work.
ORDER BY CAST(myField AS CHAR)
SELECT num
FROM your_table
ORDER BY CAST(num as CHAR)
Related
I have some data like this:
id heart_rate
1 {0: 28, 1: 25, 2: 38, 3: 42}
2 {0: 30, 1: 28, 2: 43, 3: 58}
3 {0: 42, 1: 29, 2: 98, 3: 38}
I'm trying to return an object with the averaged values, something like this:
{0: 32, 1: 26, 2: 58, 3: 43}
I tried script to loop through and analyze, but with the amount of data a loop through this could take too long and not be practical.
You need to extract all values, cast them to a number, calculate the average and then convert it back to a JSON value:
select to_jsonb(r)
from (
select avg((heart_rate ->> '0')::int) as "0",
avg((heart_rate ->> '1')::int) as "1",
avg((heart_rate ->> '2')::int) as "2",
avg((heart_rate ->> '3')::int) as "3"
from the_table
) r;
If you don't really know the keys, but you know that all of them can be cast to a number, you could do something like this:
select jsonb_object_agg(ky, average)
from (
select r.ky, round(avg(r.val::int)) as average
from the_table
cross join jsonb_each(heart_rate) as r(ky, val)
group by r.ky
) t;
Online example
I have select:
select regexp_replace(regexp_substr('[{"date": "01_2016", "val":"100_22"},{"date": "02_2016","val": "200.10"}]'
,'"val":\s*("(\w| )*")', 1, level)
,'"val":\s*"((\w| )*)"', '\1', 1, 1) val
from dual
connect by regexp_substr('[{"date": "01_2016", "val":"100_22"},{"date": "02_2016","val": "200.10"}]', '"val":\s*("(\w| )*")', 1, level) is not null
;
If my value have format 100_10 it is ok. But I want 100.10 and this select not support this. How to write regexp_replace?
Use (\d+)_(\d+) to match only the numeric values separated by an underscore:
SELECT REGEXP_REPLACE(
'[{"date": "01_2016", "val":"100_22"},{"date": "02_2016","val": "200.10"}]',
'"val":"(\d+)_(\d+)"',
'"val":"\1.\2"'
)
FROM DUAL;
Thanks everybody. I found the solution
select regexp_replace(regexp_substr('[{"date": "01-2016", "val":"100.22"},{"date": "02-2016","val": "200.10"},{"date": "03-2016","val": "200.15"}]','"val":\s*("(\w|[..])*")', 1, level),'"val":\s*"((\w|[..])*)"', '\1', 1, 1) val, regexp_replace(regexp_substr('[{"date": "01-2016", "val":"100.22"},{"date": "02-2016","val": "200.10"},{"date": "03-2016","val": "200.15"}]' ,'"date":\s*("(\w|[-])*")', 1, level) ,'"date":\s*"((\w|[-])*)"', '\1', 1, 1) date_period from dual connect by regexp_substr('[{"date": "01-2016", "val":"100.22"},{"date": "02-2016","val": "200.10"},{"date": "03-2016","val": "200.15"}]', '"val":\s*("(\w|[..])*")', 1, level) is not null
I have a columns that is for birthday and it's varchar type, I want to change in into date and add full year instead for only 2-digit.
if someone born on 05061985 the MySQL remove first 0 and show as 50685
Change 50685
To ==> 05061985
All users birthday are from 1900 until 1999
Lets do that step by step
We can have strings with len 5 or 6 so we ensure we have a len 6 string left padded with zero
select LPAD('50685', 6, '0');
Now we insert the '19' in the string between the 4th and 5th position
select CONCAT(LEFT(LPAD('50685', 6, '0'), 4), '19', RIGHT(LPAD('50685', 6, '0'), 2));
Now the last step we are going to update all the BIRTHDAY fields in the table FOOBAR
update FOOBAR set BIRTHDAY=CONCAT(LEFT(LPAD(BIRTHDAY, 6, '0'), 4), '19', RIGHT(LPAD(BIRTHDAY, 6, '0'), 2));
Anyway in this case you still have a string field, I suggest to modify the format even more to do a proper date field conversion, something like YYYY-MM-DD
update FOOBAR set BIRTHDAY=LPAD(BIRTHDAY, 6, '0');
update FOOBAR set BIRTHDAY=CONCAT('19' ,
RIGHT(BIRTHDAY, 2),
'-',
SUBSTR(BIRTHDAY, 3, 2),
'-',
LEFT(BIRTHDAY, 2));
alter table FOOBAR modify BIRTHDAY date;
I have date in integer format 140529(yymmdd). I want this date to be date format to be as 2014-05-29.
How can i do this . Please Help
Try:
SELECT CONCAT('20', SUBSTR ( 150817 , 0, 2 ), '-', SUBSTR ( 150817 , 3, 2 ), '-', SUBSTR ( 150817 , 5, 2 )) AS DATE;
And replace the "integer date" 150817 with your date
I have a column with values like this:
01709100011
I need to transform it to:
017.091.0001-1
The values have always the same characters number.
Both columns are varchar
Thanks in advance for any help.
SELECT CONCAT(SUBSTRING(test, 1,3),'.',SUBSTRING(test,4,3),'.',SUBSTRING(test,7,4),'-',SUBSTRING(test,11,1)) FROM test;
In the above example I used the table test and values in column test.
SELECT CONCAT_WS( "-", CONCAT_WS( ".", SUBSTRING( foo, 0, 3 ), SUBSTRING( foo, 3, 3 ), SUBSTRING( 6, 4 )), SUBSTRING( foo, 10 , 1 )) FROM bar WHERE 1=1;