I have a table and have a column name as dates, data type is varchar(50).
like this:
dates
-------
16/06/2017
25/05/2017
03/06/2017
17/06/2017
03/06/2017
12/06/2017
05/06/2017
06/06/2017
15/06/2017
13/06/2017
29/04/2017
21/05/2017
I tried this:
select s.date
from add_vehicle a
left join services s
ON a.vid=s.vid
where date BETWEEN '01/04/2017' AND '19/06/2017'
I only get 6th month data only.
If I change the '19/06/2017' to '30/06/2017' then I get balance results.
What is the problem? How to fix it?
Guide me please.
Note:sqlfiddle not working
Fix your data! Databases support date/time data types for a reason. That is because you should use them. In your case, you can do:
UPDATE t
SET dates = date_format(str_to_date(date, '%d-%m-%Y'), '%Y-%m-%d');
Then, alter the data type:
ALTER TABLE t MODIFY dates date;
Voila! Your code will start working.
Don't write overly complicated queries to get around a problem in the data. Fix the data.
Related
I wish to sort my table with date order so that recently added data will be on the top of the table.
I have used query for sorting as:
select date from register_table order by date desc.
Currently table display data as:
date
02.04.2019
05.04.2019
09.04.2019
10.04.2019
06.02.2019
23.01.2019
11.01.2019
I expect my table to display as:
date
10.04.2019
09.04.2019
05.04.2019
02.04.2019
06.02.2019
23.01.2019
11.01.2019
How to display data in date order?
Your fundamental problem is not storing the date as a date. You should fix that.
For the query to work, use:
order by str_to_date(date, '%m.%d.%Y')
To fix the data, you can do:
update register_table
set date = str_to_date(date, '%m.%d.%Y');
alter table register_table
modify date date;
You can see how this works here.
I don't know why your date is stored like that but here, give this a try:
SELECT date FROM date ORDER BY STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') DESC;
If you want to see what exactly happen, run this query:
SELECT date,STR_TO_DATE(REPLACE(date,'.','-'),'%d-%m-%Y') FROM date;
In case you still don't quite understand, refer to MySQL STR_TO_DATE function and MySQL REPLACE function.
I know how using STR_TO_DATE(str,fmt); will change the format of a specific string, but if I have a column of all different dates in a text format (ex. 1/11/2016, 1/25/2016, 6/27/2015...) how do I convert all of the data in the column to DATE format? From everything I'm reading, it seems you need to provide the exact value to be converted each time.
You can use multiple ifnull fuction for this.
select ifnull(STR_TO_DATE(a,'%Y-%m-%d'),sTR_TO_DATE(a,'%Y-%d-%m')) from [YourTable];
update [YourTable] set timestamp=ifnull(STR_TO_DATE([Colum],'%Y-%m-%d'),sTR_TO_DATE(a,'%Y-%d-%m')) ;
Hope this helps.
Thanks Mark B.
str_to_date(yourfield, '%d/%m/%Y'), done. you can use fields in a record as part of the update query, e.g. update foo set bar=bar+1 is completely legitimate/valid.
So I used:
update [My_Table]
set Create_Date = str_to_date(Create_Date, '%d/%m/%Y')
I'm trying to select with reference to one column changing its time in MySQL, but I don't know how.
How will I do it?
Example:
Time original: 2015-07-20 22:10:52
Updated: 2015-07-20 23:59:59
You can use timestamp to join the current timestamp's date with the time you want to set:
UPDATE mytable
SET mytimestamp = TIMESTAMP(DATE(mytimestamp), '23:59:59')
To update with no reference to previous column value, it's no different from other columns. To update with it, and based on certain part (second, month, year, whatever), you can use DATE_ADD or DATE_SUB function. Any other function in the same page might be useful, too, depending on your needs.
It doesn't matter what type of column you have, your table can be modified with a standard UPDATE statement.
You can also use the some special time/date functions to help you get the right format.
UPDATE mytable SET mytimestamp = mytimestamp::date + '23:59:59'::time;
it should work.
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')
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.