How can I pass from seconds to time on mysql?
the date format of out is hh:mm:ss.ms
Sorry. I need for example 0.98 sec -> 00:00:00.980; With sec_to_tiem return 00:00:01.
thanks.
I have implemeted of this way:
select concat(if(floor(seconds/(60 * 60)) = 0,'00',lpad(floor(seconds/(60 * 60)),2,'0')),
':',
if(floor(seconds/60) = 0,'00',lpad(floor(seconds / 60),2,'0')),
':',
seconds % 60)
but it have to exist other way more efficient
other way:
CONCAT(lpad(floor(seconds / 3600), 2, '0'), ':',
lpad(floor(seconds / 60), 2, '0'), ':',
lpad(floor(seconds % 60), 2, '0'), '.',
lpad(SUBSTRING_INDEX((seconds * 1000) % 1000, '.', 1), 3, '0'))
SELECT MAKETIME (<yourseconds>/(60*24), <yourseconds>/60, <yourseconds>%60)
or with format
SELECT TIME_FORMAT( MAKETIME( <yourseconds>/ ( 60 *24 ) , <yourseconds>/60, <yourseconds>%60 ) , '%T.%f' )
Related
I wrote a formula using the values of other textboxes in a textbox, but the system does not accept it. The formula is as below
=IIF(ReportItems!Textbox66.Value>=1,IIF(ReportItems!Textbox257.Value>=500,CInt((ReportItems!Textbox66.Value*100-100)/5),IIF(CInt((ReportItems!Textbox66.Value*100-100)/5)>=4,4,CInt((ReportItems!Textbox66.Value*100-100)/5))),IIF(ReportItems!Textbox257.Value<500,IIF(CInt((100-ReportItems!Textbox66.Value*100)/5)>-4,-4,IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))),(CInt((100-ReportItems!Textbox66.Value*100)/5*(-1)))))
It looks like there's an IIF statement with only one of the three arguments.
IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))
It should have an argument for the value when the IIF expression is True of False.
IIF((100-ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
It's easier to find if you use returns and tabs to break up the expression. A few spaces don't hurt either.
=IIF(ReportItems!Textbox66.Value >= 1,
IIF(ReportItems!Textbox257.Value >= 500,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
IIF(CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4,
4,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5)
)
),
IIF(ReportItems!Textbox257.Value<500,
IIF(CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
IIF((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
),
CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)
)
It might be a bit more tedious to use a SWITCH but it may be easier to read and make it work the way you want.
=SWITCH(ReportItems!Textbox66.Value >= 1 AND ReportItems!Textbox257.Value >= 500, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox66.Value >= 1 AND CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4, 4,
ReportItems!Textbox66.Value >= 1, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox257.Value < 500 AND CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
ReportItems!Textbox257.Value < 500 AND (100 - ReportItems!Textbox66.Value * 100) / 5 * (-1) = ?????, ?????,
ReportItems!Textbox257.Value < 500, CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)
I am stuck at a point where i have to increment a string, and my strings are of type C001,SC001,B001
in my data base they are defined like
what i am trying to do do is write a query which check the previous highest code present into my db and the incriment it to +1
for example C001 -> C002,C009->C010,C099`->C100 and so on
Similarly for SC001->SC002,SC009->SC010,SC099->SC100 and so on
Similarly fro B001 -> B002,B009->B010,B099`->B100 and so on
I have a query which my friend has suggested me to use but that query only incriminating AAAA->AAAA01 , AAAA09->AAAA10
query is
SELECT id AS PrevID, CONCAT(
SUBSTRING(id, 1, 4),
IF(CAST(SUBSTRING(id, 5) AS UNSIGNED) <= 9, '0', ''),
CAST(SUBSTRING(id, 5) AS UNSIGNED) + 1
) AS NextID
FROM (
-- since you allow strings such as AAAA20 and AAAA100 you can no longer use MAX
SELECT id
FROM t
ORDER BY SUBSTRING(id, 1, 4) DESC, CAST(SUBSTRING(id, 5) AS UNSIGNED) DESC
LIMIT 1
) x
when i am replacing ID with CategoryCode it is giving me PrevID-C004 NextID-C00401 which is not my requirement i want PrevID-C004 and NextID->C005
NOTE i am using my sqlServer 5.1
Just try this one ,
SELECT
CategoryCode,CAST(CONCAT(LPAD(CategoryCode,1,0),LPAD(MAX(RIGHT(CategoryCode,
3)) + 1, 3, 0) ) AS CHAR),
FROM test
SELECT
SubCategoryCode,CAST(CONCAT(LPAD(SubCategoryCode,2,0),
LPAD(MAX(RIGHT(CategoryCode, 3)) + 1, 3, 0) ) AS CHAR),
FROM test
SELECT
BrandCode,CAST(CONCAT(LPAD(BrandCode,1,0), LPAD(MAX(RIGHT(BrandCode, 3)) +
1, 3, 0)) AS CHAR) FROM test
This is the query, simplified.
SELECT `a`, TRUNCATE(`b` / 1000, 3) AS `b`
FROM (
...
) AS `m`
GROUP BY `a`
ORDER BY `a`
What i'm trying to do is change the number of decimal places (actual 3) based on the value of b.
So i tried this:
SELECT `a`, TRUNCATE(`b` / 1000, IF(`b` < 10, 2, 3)) AS `b` ...
and this
SELECT `a `, IF(`b ` < 10, TRUNCATE(`b ` / 1000, 2), TRUNCATE(`b ` / 1000, 3)) AS `b `
If b is less than 10, i want 3 decimal places, otherwise 2.
But this doesn't seem to work ...
Resources : https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#function_if
just change the values position that you put in your query
SELECT `a `, IF(b < 10, TRUNCATE(b / 1000, 3), TRUNCATE(b / 1000, 2))
AS b
if(a<1,2,3) means if a<1 then 2 will come as a value in your result so you have to switch your values position
use round
SELECT a , IF(b < 10, round((b / 1000), 2), round((b / 1000), 3) ) AS b
The ROUND() function rounds a number to a specified number of decimal places.
example SELECT ROUND(345.156, 2); result = 345.16
SELECT ROUND(345.156, 2); result = 345.156
If you don't want round then TRUNCATE will shown 0.00 in case of b value less than 10, so what do you mean by not working ?
You need 3 decimal place when b<10 so you have to change the position of yours query result
You have misplaced the order of queries to run, in case of true/false evaluation in If(). Following may work:
SELECT `a `,
IF(`b ` < 10,
TRUNCATE(`b ` / 1000, 3),
TRUNCATE(`b ` / 1000, 2)
) AS `b `
select CONCAT(
Lpad(
FLOOR(HOUR(TIMEDIFF('2015-06-25 12:10', '2011-06-21 10:10')) / 24),
4,0),':',
Lpad(
MOD(HOUR(TIMEDIFF('2014-06-25 12:10', '2015-06-21 10:10')), 24),
4,0),':',
Lpad(
MINUTE(TIMEDIFF('2014-06-25 12:10', '2015-06-21 10:15')),
4,0), '')
The above MySQL select statement gives
incorrect report.Required format of date = days:hours:minutes
The functions you are using are limited by the TIME type that may range from '-838:59:59' to '838:59:59'. you should use TIMESTAMPDIFF which return integers.
select
TIMESTAMPDIFF(DAY,'2011-06-21 10:10','2015-06-25 12:10') as c_days,
TIMESTAMPDIFF(HOUR,'2014-06-25 12:10', '2015-06-21 10:10') as c_hrs,
TIMESTAMPDIFF(MINUTE,'2014-06-25 12:10', '2015-06-21 10:15') as c_mnt;
but this doesn't make much sense you've probably have some typos in your formula, the code bellow is probably what you're looking for.
set #a_mnt := TIMESTAMPDIFF(MINUTE,'2014-06-25 12:10', '2015-06-21 10:10');
select
concat(
lpad(floor(#a_mnt := #a_mnt / 1440 ), 4, 0), ':',
lpad(floor(#a_mnt := ((#a_mnt - floor(#a_mnt)) * 24)), 4, 0), ':',
lpad(floor((#a_mnt - floor(#a_mnt)) * 60 ), 4, 0)) as result;
I'm attempting to write a MySQL stored function to generate v4 UUIDs as described in RFC 4122's section 4.4 ( http://www.ietf.org/rfc/rfc4122.txt ). My initial naive effort after a few tweaks is the following:
CREATE FUNCTION UUID_V4()
RETURNS BINARY(16)
READS SQL DATA
BEGIN
SET #uuid = CONCAT(
LPAD( HEX( FLOOR( RAND() * 4294967296 ) ), 8, '0' ),
LPAD( HEX( FLOOR( RAND() * 4294967296 ) ), 8, '0' ),
LPAD( HEX( FLOOR( RAND() * 4294967296 ) ), 8, '0' ),
LPAD( HEX( FLOOR( RAND() * 4294967296 ) ), 8, '0' )
);
SET #uuid = CONCAT(
SUBSTR( #uuid FROM 1 FOR 12 ),
'4',
SUBSTR( #uuid FROM 14 FOR 3 ),
SUBSTR( 'ab89' FROM FLOOR( 1 + RAND() * 4 ) FOR 1 ),
SUBSTR( #uuid FROM 18 )
);
RETURN UNHEX(#uuid);
END
The above function is quite slow: almost 100 times slower than the built-in UUID(), according to MySQL's BENCHMARK() feature. Short of writing a UDF using MySQL's C API, are there any improvements I can make here to, say, shave off an order of magnitude from its runtime?
If there is an already existing, well-regarded UUID UDF or stored procedure, I'd be happy to hear about that, too.
I didn't test this for correctness or for performance. It is just the idea of doing one only concatenation in instead of two.
create function uuid_v4()
returns binary(16)
begin
set #h1 = lpad(hex(floor(rand() * 4294967296)), 8, '0');
set #h2 = lpad(hex(floor(rand() * 4294967296)), 8, '0');
set #h3 = lpad(hex(floor(rand() * 4294967296)), 8, '0');
set #h4 = lpad(hex(floor(rand() * 4294967296)), 8, '0');
set #uuid = concat(
#h1,
substr(#h2 from 1 for 4),
'4',
substr(#h2 from 6),
substr('ab89' from floor(1 + rand() * 4) for 1 ),
substr(#h3 from 2),
#h4
);
return unhex(#uuid);
end
;
Also why do you use READS SQL DATA in your function?