Getting years 2014+ from csv import to mysql - mysql

I used phpmyadmin for the csv import to mysql. The data contains date of birth (DOB). Some of the dates go over the current date. Ex: 2035-06-15. I am trying to find a way to fix it. Ex: 2035 to 1935. One approach is to UPDATE query all dates over the year 2013/2014. Is there a way I can make that a statement? I read around and heard that using wildcards for date types is a no-no.

Instead of a wildcard, you can use the YEAR function to get the year:
YEAR(DOB)
Then you can use the DATE_SUB function to subtract the 100 years:
DATE_SUB(DOB, INTERVAL 100 YEAR)
The whole query would look something like this:
UPDATE myTable
SET DOB = DATE_SUB(DOB, INTERVAL 100 YEAR)
WHERE YEAR(DOB) > 2013

Related

Get last 3 months record from paid salary table where month and year is stored in separate column

I need help with the MySQL query. I have a table like this in MySQL
What I want is to get the last 3 months' records from the table. But I don't understand how to make this MySQL query.
I have tried to contact both columns and tried to generate a MySQL date to compare but it returns me null.
I also tried this query and it works for current data. But i am not sure if it is the only way to do this or is there any better way.
What I want is to get the last 3 months' records from the table.
I think you want a where clause like this:
date(concat_ws('-', salary_year, salary_month, 1)) >= (curdate() - interval (1 - day(curdate()) day) - interval 2 month
The key idea here is to convert the string to a date so it is handled correctly.
If you have a fixed date, then you would represent that as:
date(concat_ws('-', salary_year, salary_month, 1)) >= '2021-04--01'

how to convert from one date format to another in mysql

I am using MySQL and I want to get all the dates in the previous week from a column, say xyz, which is of type date and has the format YYYY-MM-DD. However, when I use
select
*
from
tablename
where
xyz > date_sub(curdate(),INTERVAL 1 week);
I get dates which are not within the past 1 week. I get dates which are one month from now and some random dates.
You query is pulling all records in which xyz (your date column I assume) is newer than one week ago. This could in fact be future records. If you want to exclusively view things from the previous week, you need to use the between keyword.
select
*
from
tablename
where
xyz between date_sub(curdate(), INTERVAL 1 week) and curdate()

Convert ODBC Canonical date to binary, or using a ODBC Canonical function

I have a query where I am trying to pull information where the firstdate is less than the seconddate, however most date times are pretty close to each other, so I tried:
select id from entries where firstdate [less than] seconddate -2000000000
However it doesn't seem to provide the information I would like. I have two thoughts:
I could convert the ODBC Canonical dates to binary so I can use a variable like in the example above.
Use a ODBC Canonical function where I could simply state that the second date is at least 10 days behind.
firstdate < DATE_ADD(seconddate, INTERVAL -10 DAY)
EDIT:
In your case will be:
select id from entries where firstdate < DATE_ADD(seconddate, INTERVAL -10 DAY)

How to get rows after a time of day using datetime column

I am running queries for a ticketing system. I want to extract all tickets created after 6:00PM or 18:00:00 in database/military time.
Could I use a DATEPART function or EXTRACT function?
Something like this may work:
SELECT *
FROM ticket
WHERE TIME IS AFTER 6PM
The issue is that the datetime format is as '2014-12-01 16:13:38' so I would need to specify for only the characters after the DATE section.
In MySQL, just use the hour() function:
SELECT *
FROM ticket
WHERE hour(time) >= 18;

MySQL - DATETIME greater than given time regardless of date

I have a table which stores time data in the DATETIME format. I would like to build a query to return just the values after a give time (for example 15:00 - i.e. 3pm) for each day, not just for a given date.
Is this possible in MySQL. Does anyone have any examples?
You can use the hour() function:
where hour(col) >= 15
You can do this:
WHERE TIME(column) >= '15:00:00'
or even
WHERE TIME(a.column) >= TIME(b.cutoff)