In this question does CREATE_DATE refer to a user defined column or a pre-defined mySQL column? If it is user defined what is a good assumption on what type the column is?
Select all records from table REPORTS that have a CREATE_DATE before
July 9th 2006 in newest - oldest order.
CREATE_DATE is not a pre-defined column name; it would have been defined as part of the CREATE TABLE statement.
Most likely, it is going to be a MySQL DATETIME type, though it could also be the simpler DATE type. See MySQL date types for more information.
The difference between the DATETIME and DATE types is that DATETIME (as its name implies) includes a timestamp as well as the date, while DATE stores only the date part.
SELECT * FROM REPORTS WHERE CREATE_DATE < '2006-07-09' ORDER BY CREATE_DATE DESC
Related
I am trying to delete the record from the specific date range .
So I have used the below query
delete from `table_name` where `date`<1580947200
I have used datetime as data type for the date column
It takes long time to execute and sometimes it gets stuck while executing this query.
Can anyone say how to apply index for this query
Your issue is not indexing. Your issue is that your telling MySQL to numerically compare a non-numeric (DATETIME) column against a numeric value (Unix timestamp).
i have used datetime as datatype for date column
This means the column is a DATETIME column. But;
1580947200
looks to me like a Unix Timestamp value.
Unix Timestamp is not the same as a MySQL DateTime entity.
Therefore; you need to CAST these two types to the same for the comparison; To do this you can use the UNIX_TIMESTAMP() MySQL Function.
DELETE FROM `table_name` WHERE UNIX_TIMESTAMP(`date`) < 1580947200
If you do wish to add an index to the date column you can read this Q&A.
I have a table with a dateOfBirth column and a yearOfBirth column. The input only contains the DOB information. What I am looking for is to just insert DOB info into the db, and the mysql will auto update the year column field with the year extracted from the date inserted. Is it possible to be done?
Right now my MySQL query is just
INSERT INTO table
(name, dateOfBirth, yearOfBirth)
VALUES (?)
How should I change that?
It is possible to do this using a trigger, but I don't see any practical reason to do it. Instead, just leverage the YEAR() function if you want to present the year in a query:
SELECT
name,
dateOfBirth,
YEAR(dateOfBirth) AS yearOfBirth
FROM yourTable;
Storing the birthdates in a bona fide date column is the best design choice, and any derivative of that can be had using a function or cast.
Edit:
If your reason for wanting to do this is adding an index by year, you can instead just add an index on the dateOfBirth field:
CREATE INDEX idx ON yourTable (dateOfBirth)
If you wanted to query for a certain year, say 2018, you could try the following:
SELECT *
FROM yourTable
WHERE dateOfBirth >= '2018-01-01' AND dateOfBirth < '2019-01-01';
This in fact would be sargable, meaning that the index we created above could be used.
I have a MySQL database. I wanted to delete all unverified users from the beginning until a specific date (May 31,2019). I have a column for date_created (datetime in MySQL). How can I do it? I don't want to do it manually or one by one.
Dates are an ordinal data type. You can use a < operator:
DELETE
FROM mytable
WHERE date_created < STR_TO_DATE('2019-05-01', '%Y-%m-%d');
If I have a DATETIMEOFFSET column in a SQL Server table, what's the right way to filter results to a range of local (not UTC) dates? By "local" I mean date boundaries are relative to the timezone of each stored value (which may differ between rows), not relative to a single fixed local timezone.
I assume it's not this, because the DATE literals will be assumed to be UTC. Correct?
WHERE EventTime >= '20140401' AND EventTime < '20140501'
So is this the right solution?
WHERE CAST (EventTime AS DATE) >= '20140401' AND CAST (EventTime AS DATE) < '20140501'
Or does the CAST function prevent efficient use of indexes on the EventTime column? If so, what's the right way to "align" date literals on the same timezone as the DATETIMEOFFSET column?
Yes, you can cast the EventTime column to a date type, which will strip away all time and offset information, leaving you with the local date. But as you pointed out, there will be a performance cost if you cast the column at time of query.
You should consider creating a separate column of type date for this purpose. You can do this in a view, or you can simply create a computed column. You can then create an index on this column. For example:
CREATE TABLE Events (
EventID int NOT NULL,
EventTime datetimeoffset NOT NULL,
LocalDate AS (CONVERT(date, EventTime)), -- this is the computed column
CONSTRAINT PK_Events PRIMARY KEY CLUSTERED (EventID)
)
CREATE NONCLUSTERED INDEX IX_Events_LocalDate ON Events (
LocalDate
)
You can then query by the LocalDate field and it will have the info you need and the appropriate index.
I need to get the record from one table where date between June-30-2011 and June-30-2012.
the problem is that the result is just only display the records of year 2012 although the table has records for year 2011.
below is my code
SELECT * FROM tbl_name where date between '06/30/2011' and '06/30/2012'
you need to convert it bact to date using STR_TO_DATE, eg
SELECT *
FROM tbl_name
where STR_TO_DATE(date, '%m/%d/%Y') between '2011-06-31' and '2012-06-31'
STR_TO_DATE
It is not good to store Dates as string on database because as you see it is hard to search for it, you need some extra functions to convert it back to date and to which I think it kills the index.
If you have time or privilege to alter, fix the values and change it to DateTime data type.