Mysql Error Code : 1292 Incorrect datetime value : '' - mysql

I'm trying to insert a record to a table and I get the following error.
Mysql Error Code : 1292 Incorrect datetime value : ''
Mysql code snip-let is as follows
INSERT INTO tbl_dashboard (avg_response)
SELECT cast(ifnull(floor(avg(5 * (DATEDIFF(substring(im.inq_managerreply,-10), im.inq_managerdate) DIV 7)
+ MID('0123444401233334012222340111123400001234000123440',
7 * WEEKDAY(im.inq_managerdate) + WEEKDAY(substring(im.inq_managerreply,-10)) + 1, 1))),'not_applicable')AS CHAR(45)) 'average_response_time_in_working_days'
FROM inq_manager im
There are no errors in executing the select statement which gives the average response time excluding the weekends but when I try to insert the above to my table the error is given.
The data type of the tbl_dashboard is avg_response char(45)
How can I overcome this . Please help

I am not sure this will matter, but it is worth a shot. And i'll delete this if it doesn't help but maybe
INSERT INTO tbl_dashboard (avg_response) VALUES
(SELECT cast(ifnull(floor(avg(5 * (DATEDIFF(substring(im.inq_managerreply,-10), im.inq_managerdate) DIV 7)
+ MID('0123444401233334012222340111123400001234000123440',
7 * WEEKDAY(im.inq_managerdate) + WEEKDAY(substring(im.inq_managerreply,-10)) + 1, 1))),'not_applicable')AS CHAR(45)) 'average_response_time_in_working_days'
FROM inq_manager im)

Related

How to punctuate an INSERT statement that uses FROM_UNIXTIME

In a Python environment, I've got the following variable:
post_time_ms="1581546697000"
which is a Unix-style time with milliseconds.
In my table, "created_date_time" is defined as a datetime column.
I'm trying to use an INSERT statement of the form:
sql_insert_query = "INSERT INTO myTable (id_string, text,
created_date_time) VALUES ('identifier', 'text_content',
FROM_UNIXTIME('post_time/1000')"
I can't figure out how I'm supposed to punctuate that. If I run the query as shown above, I get:
"Failed to insert record 1292 (22007): Truncated incorrect DECIMAL value: 'post_time/1000'
I've tried every variation of single quotes/no quotes I can think of but I always get errors.
For example if I do:
sql_insert_query = "INSERT INTO myTable (id_string, text,
created_date_time) VALUES ('identifier', 'text_content',
FROM_UNIXTIME('post_time'/1000)"
I get:
Failed to insert record 1292 (22007): Truncated incorrect DOUBLE value: 'post_time'
I've gone so far as to try and convert the Unix-style "1581546697000" value as follows:
post_time_mysql = datetime.fromtimestamp(int(post_time)/1000)
and then:
sql_insert_query = "INSERT INTO myTable (id_string, text,
created_date_time) VALUES ('identifier', 'text_content',
'post_time_mysql')"
and even though
print(post_time_mysql)
outputs "2020-02-14 09:25:28",
I still get this error for the above query:
Failed to insert record 1292 (22007): Incorrect datetime value: 'post_time_mysql' for column `myDatabase`.`myTable`.`created_date_time` at row 1
Any ideas/suggestions?
==========
My workaround is to do the following:
sql_insert_query = "INSERT INTO myTable (id_string, text, created_date_time) VALUES (%s, %s, %s)"
sql_insert_data = (identifier, text_content, post_time_mysql)
cursor.execute(sql_insert_query, sql_insert_data)
But I still don't understand how to successfully do it with one query statement - if it's possible.

different results for two queries

Query 1 :
SELECT
SUM(aol_int) AS AOL,
SUM(android_phone_int) AS Android_Phone,
SUM(androidTablet_int) AS Android_Tablet,
SUM(apple_mail_int) AS Apple_Mail,
SUM(blackberry_int) AS Blackberry,
SUM(Eudora_int) AS Eudora,
SUM(gmail_int) AS Gmail,
SUM(hotmail_int) AS Hotmail,
SUM(lotus_notes_int) AS Lotus_Notes,
SUM(other_int) AS Other,
SUM(other_webmail_int) as Other_Web_Mail,
SUM(Outlook_int) AS Outlook,
SUM(Postbox_int) AS Postbox,
SUM(sparrow_int) AS Sparrow,
SUM(thunderbird_int) AS Thunderbird,
SUM(windowsLiveMail_int) AS Windows_Live_Mail,
SUM(yahoo_int) AS Yahoo,
SUM(iPad_int) AS iPad,
SUM(iPhone_int) AS iPhone,
SUM(iPod_int) AS iPod
FROM mytable;
Query 2:
select sum(aol_int + android_phone_int + androidtablet_int+apple_mail_int+blackberry_int+Eudora_int+gmail_int+hotmail_int+lotus_notes_int+other_int+other_webmail_int+Outlook_int+Postbox_int+sparrow_int+thunderbird_int+windowsLiveMail_int+yahoo_int+iPad_int+iPhone_int+iPod_int)
as total_percentage
FROM mytable;
When I am summing up the results of Query 1 I am getting different sum as compared to what I am getting via Query2. The value in Query2 is less than Query 1. Why is it like that?
TROUBLESHOOTING:
I tried to write my query like this:
SELECT SUM( ISNULL(aol_int,0) +
ISNULL(android_phone_int,0) +
ISNULL(androidtablet_int,0) +
ISNULL(apple_mail_int,0) +
ISNULL(blackberry_int,0) +
ISNULL(Eudora_int,0) +
ISNULL(gmail_int,0) +
ISNULL(hotmail_int,0) +
ISNULL(lotus_notes_int,0) +
ISNULL(other_int,0) +
ISNULL(other_webmail_int,0) +
ISNULL(Outlook_int,0) +
ISNULL(Postbox_int,0) +
ISNULL(sparrow_int,0) +
ISNULL(thunderbird_int,0) +
ISNULL(windowsLiveMail_int,0)+
ISNULL(yahoo_int,0) +
ISNULL(iPad_int,0) +
ISNULL(iPhone_int,0) +
ISNULL(iPod_int,0) )AS total_percentage
FROM mytable;
However, I am getting an error after running above query in the MySQL workbench:
Error Code: 1582. Incorrect parameter count in the call to native function 'ISNULL'. What is wrong here?
This could happen if some columns in some of the rows contain nulls. When this happens, all columns in a row with even a single null column would produce null in the chain of additions, so the row will add nothing to the total.
Here is a short demo of this effect. Setup:
create table test(x int null, y int null);
insert into test(x,y) values (1,null);
insert into test(x,y) values (null,2);
insert into test(x,y) values (3,3);
Queries:
select sum(x+y) from test; -- Shows 6
select sum(x)+sum(y) from test -- Shows 9
See this demo on sqlfiddle: link.

MySQL Error: Operand Should Contain at least 1 column (Comma vs Point)

SELECT (0,14285714285714285714285714285714*(5 - 2) + 2)
What does this mean?
The actual line giving me trouble is:
SET #a13 = (0,14285714285714285714285714285714*(#a - #tmpv) + #tmpv)
Both #a and #tmpv have been calculated previously in the trigger
Any insight would be helpful
There is a syntax error in your query: You use the comma as a decimal separator. You have to use a dot instead. This works just fine:
SELECT ( 0.14285714285714285714285714285714 * ( 5 -2 ) +2 )

summing time column in mysql

I have a column in one of my tables, which is TIME format (00:00:00). I am trying to sum the entire column and display it as same (00:00:00).
I have tried using the following but it is not giving me anywhere near the correct answer.It's giving me 22.12:44:00 and manual calcaulation tells me it should be close to 212:something:something
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( vluchttijd ) ) ) AS totaltime FROM tbl_vluchtgegevens
Any recommendations?
You can try like this:-
SELECT SEC_TO_TIME(SUM(SECOND(vluchttijd ))) AS totaltime FROM tbl_vluchtgegevens;
or try this(althoug this is not a good approach):
SELECT concat(floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/3600),":",floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/60)%60,":",SUM( TIME_TO_SEC( `vluchttijd ` ))%60) AS total_time
FROM tbl_vluchtgegevens;
Edit:-
Try this:-
select cast(sum(datediff(second,0,dt))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,dt))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,dt))%60 as varchar(2)),2)
from TestTable
Working SQL Fidlle
In MySQL, the TIME type is rather limited in range. Moreover many time function do not accept values greater that 23:59:59, making it really usable only to represent the time of the day.
Given your needs, your best bet is probably to write a custom function that will mimic SEC_TO_TIME but allowing much greater range:
CREATE FUNCTION SEC_TO_BIGTIME(sec INT)
RETURNS CHAR(10) DETERMINISTIC
BEGIN
SET #h = sec DIV 3600;
SET #m = sec DIV 60 MOD 60;
SET #s = sec MOD 60;
RETURN CONCAT(
LPAD(#h, 4, '0'),
':',
LPAD(#m, 2, '0'),
':',
LPAD(#s, 2, '0')
);
END;
And here is how to use it:
create table tbl (dt time);
insert tbl values
('09:00:00'), ('01:00:00'), ('07:50:15'), ('12:00:00'),
('08:30:00'), ('00:45:00'), ('12:10:30');
select SEC_TO_BIGTIME(sum(time_to_sec(dt))) from tbl;
Producing:
+--------------------------------------+
| SEC_TO_BIGTIME(SUM(TIME_TO_SEC(DT))) |
+--------------------------------------+
| 0051:15:45 |
+--------------------------------------+
See http://sqlfiddle.com/#!8/aaab8/1
Please note the result is a CHAR(10) in order to overcome TIMEtype limitations. Depending how you plan to use that result, that means that you may have to convert from that string to the appropriate type in your host language.
This worked for me:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(vluchttijd))) AS totaltime FROM tbl_vluchtgegevens;

SQL select where like using query string

if i have these two complicated query strings
ALM_frmTopAlarmHistoryReport.aspx?strAlarmConnection=AlarmSystem
AND
http://1.1.4.1/xyz/ALM_frmAlarmHistoryReport.aspx?ViewPDF=
1&dtmStartDate={0}&dtmEndDate={1}& + '&lngAlarmGroup=' +
$('#ddlAlarmGroup').val() + '&lngProcessor=' + $('#ddlProcessor').val() +
'&intCategory=' + $('#ddlCategory').val()
how can i excuete a select WHERE LIKE condition
i tried
SELECT * FROM [tablename] WHERE string1 LIKE '%string2%'
and i got the following error
Msg 103, Level 15, State 4, Line 1
The identifier that starts with'%string2 ' is too long. Maximum length is 128.
any help will be very welcome thanks in advance
Use locate or instr instead:
select *
from tablename
where instr(string2, string1) > 0