help with date query in access - ms-access

i have field Tdate (text type) on my Table MyTbl
i need query that sort by date, i try this:
select * from MyTbl order by Tdate
but because Tdate is Text i get wrong results
how to fix it ? is there any convert to date in access ?
thanks in advance

You can try using the CDate function like this:
select * from MyTbl order by CDate(Tdate)
Watch out for potential problems with the date format, e.g. "01/02/2011" could mean 1st Feb or 2nd Jan, the CDate function will use the locale settings on your system.

I'd run a query to update your text field to use a non-ambiguous, sortable format that works in text, e.g., ISO format, YYYY-MM-DD. A better long-term solution is to change the data type of the field so it's a date so you wouldn't have to muck about with these kinds of problems.

Related

I need to retrieve only date from login1 field which is varchar having value like this 1/24/2019 13:22:22?

I'm have a table where I'm capturing login and logout time from front end application. The login and logout time is sitting in the database in this format 1/24/2019 13:22:22, now I need only date from it.
So that I can use to retrieve data based on the particular date i.e., I can use select statement with where clause giving the date and getting the data.
Can anyone help me in converting this. I'm using varchar field to store the input value coming from front end.
This is the table and based on the login1 field date i need to retrieve the data.
I'm able to do the query required for getting the data as I needed
SELECT * FROM timesheet where login1 LIKE '%1/24/2019%'
You need to cast or convert as a VARCHAR datatype, you can try below query to get only the date.
SELECT CAST('1970-01-01 00:00:00-08:00' as DATE) as DATE1 FROM dual;
You can try below -
select date(STR_TO_DATE(login1, '%m/%d/%Y %H:%i:%s'))
You can do it in this way also-
select * FROM Login WHERE date_format(STR_TO_DATE(logindate, '%m/%d/%Y %H:%i:%s'), '%m/%d/%Y') = date('01/18/2019')

How to Query between a date range in MySQL

I'm querying
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '02-04-2017'
and the result is null. How to fix this. But
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '30-03-2017'
giving me the correct result.
Note:- tempLog is the table name.
You should store dates in date format or atleast correctly formatted string (YYYY-MM-DD).
For now you can use str_to_date to convert the string to date and compare:
select *
from tempLog
where str_to_date(date, '%d-%m-%Y') between '2017-03-23' and '2017-04-02';
However note that this will hinder the optimizer from using index on the column if any.
The correct remedy of the situation is fixing the table structure.
According to the documentation, you're supposed to use this format when writing a date: 'YYYY-MM-DD' (although it says it may accept 'YYYYMMDD' or even YYYYMMDD in some contexts).

Grouping only by date in a varchar column in mysql

I have a mysql column where the data is stored as VARCHAR though the data values are of datetime in the format of yyyy-mm-dd hh:mm:ss.
Now my task is to group by the date part i.e yyyy-mm-dd by converting VARCHAR to date-time and then just taking date part out of it
QUERY
SELECT SUM(value)
FROM table
GROUP BY name , [date part of the varchar field]
Please let me know if this is at all possible and if yes, how?
Assuming that your data in this varchar field is properly formatted, you can work with the left function, like this:
SELECT LEFT(mydate, 10) AS myval,
SUM(myvalue)
FROM mytable
GROUP BY myval;
If this isn't a big issue; I'd advise converting your varchar column to datetime or timestamp. If not only for the possibly better data storage usage, it'll be way easier to do work with date and time related functions.
Just use the left function. You can leave the date as a string:
SELECT left(datecol, 10) as YYYYMMDD, SUM(value)
FROM table
GROUP BY left(datecol, 10);
I removed name from the group by because it doesn't seem relevant to the question. You can, of course, add it back in.
By the way, MySQL understands this format for dates, so if you really, really want a date:
SELECT date(left(datecol, 10)) as RealDate, SUM(value)
FROM table
GROUP BY RealDate;

Converting date field to return only Day field

I am working for a POS company, i have a query in my program that gets date from multiple tables and one of the fields it returns is a date field that returns the the date in the format yyyy-mm-dd. Is there a way to get my query to bring this date field in the form 'dd'? if yes how?
You are looking for the date_format() function (see here):
select date_format(<datefield>, '%d');
This returns the value as a string. If you want it as a number, just use day():
select day(<datefield>)
You are looking for this:-
EXTRACT(unit FROM date)
or try this:-
SELECT DAY(your_date_field) AS dtDay FROM your_table
select DATE_FORMAT(your_date_column,' %d ') FROM your_table
As you mentioned Delphi, why not a Delphi solution:
open the query in a dataset
get the corresponding field
get the value with AsDateTime
use function DayOf from System.DateUtils to retreive the day
in case you need a dataset field for that, create a calulated field
move the day calculation into the OnCalcFields event of the dataset
There might still be plenty of other ways to do it.

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.