I am trying to retrieve tickets from a database that are created in the last 6 weeks. Therefore i am trying to use an delimiter which filters that period but the database time is based on an UNIX timestamp and i want to search on a logical date.
I've tried date(), timestamp(), CAST() in the last line of my query but most throw errors or don't actually filter the wanted results
SELECT ticket,
company,
title,
status,
department,
user,
(FROM_UNIXTIME(timestamp,"%d-%m-%Y")) AS 'Start date',
(FROM_UNIXTIME(close_time,"%d-%m-%Y")) AS 'Close date'
FROM ticket_database
WHERE department IN ('stack_mysql')
**AND DATE_FORMAT(timestamp,'%d-%m-%Y') > '11-02-19'**
;
I expect that all tickets created on or after 11-02-19 are shown but instead it ignored this date and throws everything at me since 2006.
Help is appriciated
It is not the best idea to use a function on a field in the WHERE Clause. This will generate a FULL TABLE SCAN. MySQL must read each ROW, run the function and the compare.
Better is to use the function on the constant part of the compare, but here
is it is not nesecary
SELECT ticket,
company,
title,
status,
department,
user,
(FROM_UNIXTIME(`timestamp`,"%d-%m-%Y")) AS 'Start date',
(FROM_UNIXTIME(close_time,"%d-%m-%Y")) AS 'Close date'
FROM ticket_database
WHERE department IN ('stack_mysql')
AND `timestamp` > unix_timestamp('2019-02-11 00:00:00');
Note: Make your life easier. Dont use reserved Keywords like timestamp not as fieldnames. if you do that you must quote it in backticks
You have a couple of issues. Firstly, based on this line:
(FROM_UNIXTIME(timestamp,"%d-%m-%Y")) AS 'Start date',
the timestamp column is a unix timestamp, not a date and thus can't be passed to DATE_FORMAT. Secondly, 11-02-19 is not a valid MySQL date format and thus can't be compared to a date directly; you need to use STR_TO_DATE to convert that to a date instead. So, to make it work, try this:
AND FROM_UNIXTIME(timestamp) > STR_TO_DATE('11-02-19', '%d-%m-%y')
To allow MySQL to use an index on the timestamp column, remove the FROM_UNIXTIME call and convert the date into a timestamp instead:
AND timestamp > UNIX_TIMESTAMP(STR_TO_DATE('11-02-19', '%d-%m-%y'))
Related
I am trying to pull the value from the 'dwgm_10am_final_price' column with today's current date (2021-01-06). In this case it will be null until a value is inserted into the table at 10am. as depicted in the image below.
This is the current query I am trying but it is returning 'No Data' on my Grafana dashboard. How can I edit my query to always pull the data with today's timestamp, regardless of it is NULL or a numerical value.
SELECT gas_date AS time, dwgm_10am_final_price
FROM gas_market_prices
WHERE DATE('time') = CURDATE()
You are trying to get a date from the String time, which doesn't exist
You need to use backticks for your query
SELECT gas_date AS time, dwgm_10am_final_price
FROM gas_market_prices
WHERE DATE(`time`) = CURDATE()
You can read this canonical thread When to use single quotes, double quotes, and backticks in MySQL
I'm trying to query a MySQL database and return all records within a given date range and which also contain the substring 'bank' in the content column.
The format of the 'time' field I refer to is mm/dd/yyyy hh:mm:ss.
Here's the statement I've come up with but MySQL Workbench is giving me issues:
SELECT *
FROM blogs
WHERE ((‘time’ BETWEEN “04/01/2011 00:00:00” AND “04/15/2011 23:59:59”)
AND (‘content’ LIKE ‘%bank%’))
How about trying this:
SELECT *
FROM blogs
WHERE `time` BETWEEN '2011-04-01 00:00:00' AND '2011-04-15 23:59:59'
AND `content` LIKE '%bank%';
This works if your time field is in fact a timestamp. If time is not a timestamp then you will have to go with something like the answer from McAdam331 but I'm hoping your database is using the correct types for the kind of data you are asking it to store.
single ' or double " quotes around values and ticks ` around field names. I also changed the date format to yyyy-mm-dd hh:mm:ss and eliminated some unnecessary parentheses.
http://sqlfiddle.com/#!9/730bd/1/0
It would be helpful if you posted the structure of the table when posting questions like this so we can be sure to give the right answer.
It isn't a good idea to store dates like that in MySQL. The DBMS has Date and Time Types you can use to store that information.
If changing the database isn't an option, you can convert a string to a date object using the STR_TO_DATE function, which takes in a date string and the format that it is in already and returns a date.
MySQL stores dates in the 'YYYY-MM-DD' format, so to get that format you can try something like this:
SELECT STR_TO_DATE('04/01/2011', '%m/%d/%Y');
Which will return a date object for that day. Note the capital Y.
Then it becomes much easier to query between dates, like this:
SELECT *
FROM myTable
WHERE STR_TO_DATE(dateString, '%m/%d/%Y %H:%i:%s') BETWEEN STR_TO_DATE('04/01/2011 00:00:00', '%m/%d/%Y %H:%i:%s') AND STR_TO_DATE('04/15/2011 23:59:59', '%m/%d/%Y %H:%i:%s')
AND content LIKE '%bank%';
Here is an SQL Fiddle example, and here is a link that has the formatting characters you need.
Help! One column in my database is for dates. All of my dates are unfortunately in the String form (YYYY.MM.DD). I have a MASSIVE database (300+GB) so ideally would like to avoid transformations.
Is there a way I can select rows for dates in between YYYY.MM.DD and YYYY.MM.DD? What would the script look like?
Thank you!
If the months and days are stored with leading zeroes, the BETWEEN operator will work as expected. So will ORDER BY.
create table your_table (
date_value varchar(10) not null
);
insert into your_table values
('2013.01.01'), ('2013.01.13'), ('2013.01.30'), ('2013.01.31'),
('2013.02.01'), ('2013.02.13'), ('2013.02.28'), ('2013.02.31'),
('2013.03.01'), ('2013.03.15'), ('2013.03.30'), ('2013.03.31');
select date_value
from your_table
where date_value between '2013.01.01' and '2013-01-31'
order by date_value;
2013.01.01
2013.01.13
2013.01.30
One of the main problems with your structure is that you lose type safety. Look at this query.
select date_value
from your_table
where date_value between '2013.02.01' and '2013.02.31'
order by date_value;
2013.02.01
2013.02.13
2013.02.28
2013.02.31
If you'd used a column of type date or datetime or timestamp, the dbms would not have allowed inserting the values '2013.02.31', because that's not a value in the domain of date. It is a value in the domain of varchar. (And so is "Arrrrgh!", unless you've got a CHECK constraint on that column that severely restricts the acceptable values.)
Not good solution, but works (cost much performance).
You have formated date in order year, month, day (good order to compare strings, without transformation to datetime), so you can try
SELECT * FROM Table WHERE StringDate > '2013.07.10' AND StringDate < '2013.07.14'
It returns bad results if there are dates before year 1000 without leading zero ('999.07.14').
But I dont know how it works on big database.
SQL Fiddle
Between in SQL is inclusive of both bounds. If that is what you want, you can just use between:
where col between 'YYYY.MM.DD' and 'YYYY.MM.DD'
Where the two constants are whatever values you are looking for.
If you have an index on the column, then between (as well as >, >=, and so on) will use the index. You do not need to transform the values. If your constants are dates of one form or another, then you can use date_format() to create a string in the right format. For instance, to get dates within the past week:
where col >= date_format(adddate(now(), -7), '%Y.%m.%d')
Is it valid to do this to change the date format while inserting data?
$sqlCommand = "INSERT INTO articles (id, title, content, publicationDate) VALUES ('NULL', '".$title."', '".$content."', DATE_FORMAT(NOW(), '%b, %e, %Y'))";
If not, how do I change the default date format (which is currently year-month-day)?
the format you use to insert is not related to the format you get when selecting.
you can choose the format of the date column in the SELECT by using
TO_CHAR( publicationDate, 'yyyy mm/dd' )
or whatever format you like.
I agree with Randy,more over "Date" is a Data type defined in MySQL whose format cannot be changed.instead change it the way you want while u retrieve it in your query..
but 1 issue with the Date Data type is, every time you insert the date you have to give it in the yyyy-mm-dd format,if you find that weird,then i suggest you use a string datatype and form your queries accordingly.but of course i.e only if your database queries don't have any significant need of Publication Date attribute
We have a legacy database (SQLServer 2008) with thousands of rows in it. Each record has a logdate field which is a date but stored as a varchar in the format 21/04/2010 16:40:12.
We only need to return the rows where the logdate is in the future, and order them by date. We could pull back all the rows and filter on the server but this seems wrong and won't scale.
Is there a way of doing the filtering and ordering in Entity Framework 4.
This is what we thought might work but it's failed.
from c in db.changes
where [DateTime]c.logdate > DateTime.Today()
orderby [DateTime]c.logdate
select c;
Any help is appreciated.
You can't parse a string into a date on the DB server with any built-in L2E function.
You can:
map a DB function yourself,
write SQL and execute it with ObjectContext.ExecuteStoreQuery, or
fix the metadata.
I'd pick the latter, if it were me.
I'm not sure you can do it through pure LINQ unless you create your own LINQ functions. If you can execute an ad-hoc query and have EF4 translate it back into objects, like you can on the DataContext.Translate LINQ2SQL method, you can convert it like this:
CONVERT(datetime, logdate, 103)
And thus your query would be:
SELECT
*
FROM
changes
WHERE
CONVERT(datetime, logdate, 103) > GETDATE()
ORDER BY
CONVERT(datetime, logdate, 103)
Alternatively, if you can add to the schema (I assume you can't modify the varchar column to store it as a datetime natively), you could add a computed column like so:
ALTER TABLE
changes
ADD
logdateDatetime AS CONVERT(datetime, logdate, 103) PERSISTED
And then query the logdateDatetime column instead of logdate.
The order in a varchar field will be considerably different than the order in date field. Fix your structure to correctly store dates or add an additional date field that is populated through a trigger. Likely you have bad dates in there as well since there are no controls on a varchar field to diallow dates from being put in. You will need to fix these as well.