Select Date query from MS-Access DATETIME column - delphi

I am currently working on "Delphi XE4" with "MS Access" Database.Tools I am using is ADOCommand, ADOQuery, ADOConnection,ADOTable and Datasource
In my user interfaced project, User is selecting range of Dates and using that date I am selecting a records from database.
But problem is,in my database column is in DATETIME Format (5/21/2015 02:30:00 PM) and user is selecting only a DATE eg., 5/21/2015.
Now how can I select records from database using DATE only.
Please Note that I already tried to fire Select query for example,
ADOQuery.SQL.ADD('SELECT * From Tablename') ;
ADOQuery.SQL.ADD('WHERE Cast(DateTimeField As DATE) = #5/21/2015# ');
and this also
ADOQuery.SQL.ADD('SELECT * From Tablename') ;
ADOQuery.SQL.ADD('WHERE Convert(DATE,DateTimeField, 101) = #5/21/2015# ');
but this give to error "Undefined Function 'Cast/Convert' in expression"
Is there any other way to do this??
Can anybody suggest me how can I fetch records by fetching Date from "DATETIME fields" using select query?

ANd welcome to SO.
You should not try to parse the date you self you should use a parameterized query instead.
So
ADOQuery.SQL.ADD('SELECT * From Tablename') ;
ADOQuery.SQL.ADD('WHERE Convert(DATE,DateTimeField, 101) = #5/21/2015# ');
Should be changed to
ADOQuery.SQL.ADD('SELECT * From Tablename') ;
ADOQuery.SQL.ADD('WHERE DATE = :DateValue');
Then when you call your Query do as following:
ADOQuery1.Close;
ADOQuery1.Parameters.ParamByName('DateValue').Value := Now;
ADOQuery1.Open;
The the SQL engine does the trick for it. Try it and if you can't make it work I'll make a complete example for you

The way I use dates in MSAccess with Delphi is
ADOQuery.SQL.ADD('SELECT * From Tablename') ;
ADOQuery.SQL.ADD('WHERE DateTimeField = Format(''21/05/2015'', ''dd/mm/yyyy'') ');
I hope it helps!

Related

Calculate week end date

I am working on HANA and I am not able to figure it out how to get the expected output.
Let me throw some light on getting the output. I am working on a procedure and my job is to get the data inserted in that particular table which I am successfully inserting, But the data is not in a correct manner.
I have 3 columns in my output named: report_date, report_week, week_end.
If suppose I start my data from 2010-01-01 the week end should start from 2010-01-06. And In this way I want my data from 2010 till 2030. It should show every week start in report_date and every week end start in week_end. The role of report_week is to show which week is going on currently.
My error output:
I have my procedure which I am posting below:
create procedure bhavya.zz_get_series()
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
AS
LV_START_DATE date := '2010-01-01' ;
LV_END_DATE date := current_date ;
LV_WEEK_END NVARCHAR(10) := 'FRIDAY';
lv_report_date Date ;
Begin
TT_SERIES = (SELECT GENERATED_PERIOD_START AS REPORT_DATE,
week(add_days(generated_period_start , 2)) as report_week,
current_date as week_end --added ABHOOT
FROM SERIES_GENERATE_DATE ( 'INTERVAL 1 DAY', :LV_START_DATE,
ADD_DAYS(coalesce(current_date,:LV_END_DATE), 1)));
TT_WEEK_END = select report_week, max(report_date) as week_end
from :TT_SERIES
group by report_week ;
insert into "BHAVYA"."AFS_BASE.KPI.TABLES::DB_WEEK_SERIES"
(REPORT_DATE,REPORT_WEEK,WEEK_END)
select S.report_date, w.report_week, w.week_end
from :TT_SERIES S
left join :TT_WEEK_END W
on w.report_week = s.report_week;
end;
call bhavya.zz_get_series
Any help is really appreciated.
I think the problem is here:
week(add_days(generated_period_start , 2)) will return the week number within a year, so it will always be between 1 and 52. As week N will occur in 2010, 2011,...,2017 the max(report_date) will always be in the last year it occurs (so either 2016 or 2017).
TT_WEEK_END = select report_week, max(report_date) as week_end
from :TT_SERIES
group by report_week ;
So you have to extract the year as well from the report date and include it in the group by of the TT_WEEK_END select as well as in the join clause used for the INSERT statement.

Query influxdb for a date

I have a table in influxdb that has a column called 'expirydate'. In the column I have afew dates e.g. "2016-07-14" or "2016-08-20". I want to select only the 2016-07-14 date, but I am unsure how?
My query is currently:
SELECT * FROM tablee where expirydate = '2016-07-14' limit 1000
But this does not work. Can someone please help me?
Assuming the value table**e** is a valid measurement...
If you are looking at selecting all of the points for the day '2016-07-14', then your query should look something like.
Query:
SELECT * FROM tablee where time >= '2016-07-14 00:00:00' and time < '2016-07-15 00:00:00'
You might also be interested in the influx's date time string in query.
See:
https://docs.influxdata.com/influxdb/v0.9/query_language/data_exploration/#relative-time
Date time strings Specify time with date time strings. Date time
strings can take two formats: YYYY-MM-DD HH:MM:SS.nnnnnnnnn and
YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ, where the second specification is
RFC3339. Nanoseconds (nnnnnnnnn) are optional in both formats.
Note:
The limit api could be redundant in your original query as it is there to impose restriction to the query from returning more than 1,000 point data.
I had to force influx to treat my 'string date' as a string. This works:
SELECT * FROM tablee where expirydate=~ /2016-07-14/ limit 1000;

How do you apply a Delphi ADOTable filter to a date data type

in MS Access I am able to filter a date in a query like this:
ex.
SignUpDate > #31/12/2013#
this will make the database only show records where SignUpDate is in 2014 or newer
How will I do this in delphi?
dmGym.tblMembers.filter := 'SignUpDate > ''#31/12/2013#''' doesn't seem to work
please help it wil be greatly appreciated
You can try :
dmGym.tblMembers.Filter:='SignUpDate > 31/12/2013';
dmGym.tblMembers.Filtered:=True;
This will make the database only show records where SignUpDate is in 2014 or newer.
Good Luck.
I think you don't need the #s, try
[...].Filter := '12/31/2013'; // this is for Sql Server and tested
// for UK locale, for Access you may need to swap the dd and mm,
//and maybe even the yyyy as suggested by #kobik in a comment.
If you use a TDateTime field in a TAdoDataSet.Locate(), the function GetFilterStr plugs the # signs in for you (and makes a hash of it (spot the pun) when doing similar for string fields - see # signs in ADO locates (Delphi XE5)).
But setting a simple filter on a TAdoTable seems to bypass ADODB.GetFilterStr and does a direct assignment to its recordset's Filter property, so I'm guessing that if the #s are needed, they must be plugged in by the ADO/MDac layer.
Delphi stores the datetime as a real number. Today is 05 June 2016 and the interger part of the DateTime is 42,526. Date zero is the start of 1900. You need to generate a variable called DateInt.
Var
DateInt: integer
Date1: TDate;
Begin
DateInt := Trunc(Date1)
When you save a date, save DateInt in the BeforePost event handler. This is an extra field, but filtering is now easy. For example, your filter can now be
NewDateInt := Trunc(Date1);
Filter := ‘NewDate = DateInt’;
Try dmGym.tblMembers.filter := 'SignUpDate > #yyyy/mm/dd#' (2013/12/31)
-credit to kobik's comment

How to get data from sqlite table by date recent in iphone application

I have an iPhone application in which I have data in the Content table and there is column in table ContentAddedDateTime where is the date time ContentAdded I want to select in query the most recently added contents in ContentTable any idea how to get this.
This is the table structure:
NSInteger contentID;
NSString*categoryID;
NSString*topicID;
NSString*contentType;
NSString*contentTitle;
NSString*contentAddedByUserID;
NSString*contentDescription;
NSString*contentFileName;
NSString*categoryName;
NSString*topicName;
NSString*contentSavedFileLocation;
NSString*contentSize;
NSString*contentAddedDateTime;
I got link that we can use start date and end date but I have only only one column ContentAddedDateTime.
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster Where ContentAddedDateTime='%#'",currentDateTime];
Please use below structure.
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
eg:
SELECT * FROM ContentMaster
ORDER BY ContentAddedDateTime DESC;
Add an order by ContentAddedDateTime desc to the query. The first result will be the most recent.
All the above queries will not work in this case first of all let me clear you 1 thing that sqlite treats the 'Date' type as as String and all the comparisons are done as with Strings type so you have to convert the date to UNIX Milli seconds and then sort them manually according to the UNIX timestamp
time_t unixTime = (time_t) [dateObject timeIntervalSince1970];

sqlite Date Sorting

I am a parsing a file into a sqlite database that contains dates in the YYYY-MM-DD format. I want to store the entries into sqlite in such a way that I can sort the entries by date (strings not cutting it). What is the normal protocol for storing and ordering dates in sqlite? Should convert the dates into a number. Is there a way to convert YYYY-MM-DD dates into timestamps?
SQLite supports "DATE" in table creation. (More about that later.)
CREATE TABLE test (dt DATE PRIMARY KEY);
INSERT INTO "test" VALUES('2012-01-01');
INSERT INTO "test" VALUES('2012-01-02');
INSERT INTO "test" VALUES('2012-01-03');
SELECT dt FROM test ORDER BY dt;
2012-01-01
2012-01-02
2012-01-03
Values in the form yyyy-mm-dd sort correctly as either a string or a date. That's one reason yyyy-mm-dd is an international standard.
But SQLite doesn't use data types in the way most database workers expect it. Data storage is based on storage classes instead. For example, SQLite allows this.
INSERT INTO test VALUES ('Oh, bugger.');
SELECT * FROM test ORDER BY dt;
2012-01-01
2012-01-02
2012-01-03
Oh, bugger.
It also allows different date "formats" (actually, values) in a single column. Its behavior is quite unlike standard SQL engines.
INSERT INTO test VALUES ('01/02/2012');
SELECT * FROM test ORDER BY dt;
01/02/2012
2012-01-01
2012-01-02
2012-01-03
Oh, bugger.
You don't have to do anything special to store a timestamp in a date column. (Although I'd rather see you declare the column as timestamp, myself.)
INSERT INTO test VALUES ('2012-01-01 11:00:00');
SELECT * FROM test ORDER BY dt;
2012-01-01
2012-01-01 11:00:00
2012-01-02
2012-01-03
Oh, bugger.
SQLite will try to do the Right Thing as long as you feed consistent data into it. And it will sort dates correctly if you use the standard format.
Instead of storing date in format "YYYY-MM-DD", store the time-stamp of that date and that will help you to sorting the table.
If You want to Current TimeStamp then use
SELECT strftime('%s','now');
If You want toYYYY-MM-DD date TimeStamp then use
SELECT strftime('%s','YYYY-MM-DD');
where %s=seconds since 1970-01-01
i have the date field store in this way DD/MM/YYYY.
For sorting the date ( date field is a string ) i have to convert it before order it.
select (substr(date, 7, 4) || '-' || substr(date, 4, 2) || '-' || substr(date, 1, 2)) as new_date from work_hour order by new_date desc

Resources