How can I resolve this issue using an sql query - mysql

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in Layman's expression ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.

Remove the quotes around the field names
SELECT
DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) AS cap_maturity FROM capital
I think this should work.
I know its is an old question, but just trying to help others

Related

MySQL query - check between two dates without needing to retrieve rows

I have a table that includes dates, I'm trying to check if a date I have falls between the dates in the table. My query is working, but it doesn't return anything. This seems like it should be very simple, but I can't wrap my head around it.
SQL query looks like this:
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
The dates are generated dynamically in my script so I can ascertain if what I'm passing into it falls between the beginning and end dates in my table. I don't need any specific data from the table, just a boolean telling me whether the date is between the beginning and end dates or not.
You are looking for EXISTS:
SELECT
EXISTS(
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
) AS hasValue
Hope this helps,
Check your MySQL server's date format and your generated date format.
MySQL date format is like that: '2019-01-30 18:19:52'
Also you can try change
(beginning_date_from_table) and (end_date_from_table)
to
(end_date_from_table) and (beginning_date_from_table)
Check: How do I query between two dates using MySQL?
I phrased the question slightly wrong, in that I was trying to return the result of a conditional. Once I realised how to ask google the right thing, I quickly came up with this solution:
SELECT (CASE WHEN this_date BETWEEN beginning_date AND end_date THEN 1 ELSE 0 END) AS date_result
Thanks to the other people who answered, your input put my thinking on the right track.

Unable to get Results of a query

I am newbie to SQL.
I have this query
I am not sure where i am doing it wrong.
Can you guys help me out?
thanks.
Three mistakes:
You need single quotes
MySql expects two-digit months
The 2015-09-30 end date includes an implicit midnight for the time component, which excludes most of the day
Put it all together you get this:
SELECT * FROM results WHERE played_on BETWEEN '2015-09-01' AND '2015-10-01';
While I'm here, I prefer to avoid BETWEEN in favor of explicit bounds. There's always that chance someone codes a game for exactly midnight October 1:
SELECT * FROM results WHERE played_on >= '2015-09-01' AND played_on < '2015-10-01';
And you would have had this answer faster if I could have copy/pasted the query text from your post instead of having to re-type. Posting images of sample data or code instead of the text is considered very rude here.
i think you can use MySql Date Function like MONTH() and YEAR() for this query :
SELECT * FROM results WHERE MONTH(played_on) = 9 AND YEAR(played_on) = 2015
I hope this answer can help you.

count(*) on mysql giving me value 0

select count(*) FROM antecedente_delito WHERE rut_polichile = NEW.rut_polichile
this statement is giving de value 0, when it should give me 18 :/ ive been trying a lot to find any bug in it.
Here's the working solution that I mocked up using/changing your code in SqlFiddle. http://sqlfiddle.com/#!2/ac2e9/1
To trouble shoot this, I would view your actual values and verify that NEW. is returning what you think it should. Sometimes it may be doing some trims or removal of special characters, especially the _ and % signs are likely to stripped in subprocedures.
I would start with the query:
select top 50 rut_polichile, NEW.rut_plichile FROM antecedente_delito
If the issue is not obvious from that add in a varbinary check:
select top 50 cast( rut_polichile as varbinary), cast(NEW.rut_plichile as varbinary) from antecedente_delito
If the table only has 18 records, then you should be good to go with the above troubleshooting, but if there is more data, I would suggest limiting your results from the above by the rowid or other identifier in a where statement.
It's not the answer, but I hope it helps you find the answer.
The SELECT privilege for the subject table if references to table columns occur via OLD.col_name or NEW.col_name in the trigger definition.
but in your trigger i can't see any trigger definition. so try without NEW.
for more info: http://www.sqlinfo.net/mysqldocs/v51/triggers.html or
http://bugs.mysql.com/bug.php?id=31068

MySQL "date_sub(now(), INTERVAL X" Error Code: 1366 Incorrect string value: '\xEF\x80\xA6Top...' for column 's' at row 3

I have a problem here thats really puzzling me. I have a MySQL query, that works fine.. for the most part. In the query, Im looking at dates and date ranges from a table, and returning results that are X days old from today. It looks like this:
WHERE tickets.date_created > date_sub(now(), INTERVAL 30 DAY)
Under most conditions, the query works fine. However, if I change the interval to a number between 80 to 97 my script fails to execute. The error I get is:
Error Code: 1366 Incorrect string value: '\xEF\x80\xA6Top...' for column 's' at row 3
Also, tryin 3 months, rather than 90 days doesnt work. There appear to be other numbers in the 100's that dont work too, however, if I set something like 10000 days, all results return ok.
Is this something in my database? Is it my query? Has anyone seen this with a date interval? What am I doing wrong?
Any thoughts anyone has :)
Many thanks!
I've found the answer!
There is a function built into our MySQL to strip out HTML and other characters from a returned result. Whilst its not performing any action on the date area (or shouldn't be) as its in my select statement:
mycleanup (left(tickets_messages.message, 250))
It appears that if I change this to simply
left(tickets_messages.message, 250)
My query then works! Very odd, but I guess Ill have to find another way to work around stripping HTML and the like.

mysql calculation

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in layman's term ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
It sounds like you want this:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR)
...
This DATE_ADD function adds the given number of years to the starting date, thus producing the maturity date — just as you've described.
Please show the errors you get. The query
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) FROM capital
should work.