How to add an HH:MM time value to an Access database? - delphi

I want to add time only as HH:MM to MS Access in Delphi.
It is shown as '21:21:00' in the Access table but it shows up as '30.12.1899 21:21:00' in my dbgrid.
ADOTable1.Active:=FALSE;
ADOTAble1.Open;
ADOTable1.Insert;
ADOTable1time.Value:=Strtotime(MaskEdit1.Text);
ADOTable1.post;
ADOTable1.Active:=TRUE;
Note: I changed editmask option to short time of TMaskEdit1
Thank in advance.

The data is correctly stored, it's just not displayed as you would like.
The Date/Time in Access as the Delphi TDateTime is storing casually spoken
the date in the integer part and the time in the fractional part.
To get the Time shown as desired just set the DisplayFormat property of your field to hh:nn:ss.
In the pictures below you can see the always same field formatted, not formatted and as float.

You need to tweak the formatting for that column of your dbgrid to only show the time component. Access only has a Date/Time column type (it does not have separate Date and Time column types) so a Date/Time value entered without a date part is stored as 1899-12-30 hh:mm:ss. If you are only interested in the time part then you just need to format the dbgrid column appropriately. (Access automatically does that for you in its own controls, ref: here.)

You can do it programatically, after you open the dataset:
TDateTimeField (FieldByName ('Time')).DisplayFormat:='HH:mm';

Related

Google Sheet-Date validation in specific format

how to use specific date formats in data validation in Google Sheets.
I set this date format in google sheet dd-mmm-yyyy (09-Jan-2023) but the user enters a different date format in the sheet so im want to use data validation for this the user must enter data in specific format dd-mmm-yyyy
i want that when user enter data in different format its auto rejects entry.
try:
=REGEXMATCH(A1&""; "\d{2}-.{3}-\d{4}")
or:
=REGEXMATCH(A1; "\d{2}-.[A-z]{3}-\d{4}")
If you previously set the date format in the range you want, then no matter in which way the date is inserted then it will be displayed as you wish. Specially with mmm format you'll be always at risk that they can mess with how the month is supposed to be written, I think it's preferably for them to insert the number and you choose how it is displayed

Datasnap issue (datasets displaying wrong field types)

I was basically following this example :
http://www.youtube.com/watch?v=B4uxLLIUddg
New -> Other ->Datasnap Server -> VCL Forms Application
(All default settings,Port 211 working, TDSServerModule).
Then created a table in SQLite :
CREATE TABLE [T1] (
[ID] INTEGER PRIMARY KEY AUTOINCREMENT,
[DATE] DATE,
[TIME] TIME);
On my ServerMethodsUnit1 I dropped a TSQLConnection.
Changed the Driver to Sqlite.
Removed LoginPrompt.
Connection connected OK.
Added TSQLDataset and connected it to my SQLITECONNECTION.
Set CommandText to my T1 (the table name).
Made it active without a problem.
Added a datasetprovider1 and lined it to my dataset (The table T1).
Saved all.
Run the server without a problem.With the server running,I constructed the client side:
To my project I added a new project (vcl forms application).
Added a SQLConnection component.
Set its driver name to Datasnap.
Removed loginprompt.
On the form I dropped DSProviderConnection1.
Connected it to my sqlconnection.
Set its ServerClassname to TServerMethods1.
Tested the connection - both work OK.
Dropped a Clientdataset. Connected its RemoteServer property to that of DSProviderConnection1.
ProviderName to DataSetProvider1.
Connection succeeded. Clientdataset is active.
Added a DataSource.Linked it to my Clientdataset.
All connections work. So I added a little GUI.
Dropped a TDBGrid and a TDBNavigator. Linked them to Datasource1.
The first strange thing I noticed is that all fields displayed Widememo.
Why is this when fields are completely different,I dont know.
Went to the fields editor,added the fields and when checking the BlobType
all displayed ftWideMemo.
I tried to insert todays date directly in the grid and somehow in my DB it ended up : 1899-12-30.
Checking the table (T! on the server side), the DATE,and TIME field also displays widememo.
What am I missing here ?
SQlite has very loose typing.
DATE and TIME are not part of SQLite.
And DBGrids don't know what to do with SQLite TEXT.
The way I got around this problem was to use VARCHAR(length) instead of TEXT when defining the fields.
Then those will display OK in a DBGrid. And I did dates also as VARCHAR().
See Also
Displaying and editing MEMO fields in Delphi's TDBGrid
http://delphi.about.com/library/weekly/aa030105a.htm
There is no such native DATE nor TIME type in SQlite3:
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic
Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.
Also ensure that you understood the "type affinity" feature of SQlite3 columns - which is pretty powerful, but may be confusing if you come from a "strongly typed" RDBMS.
What you can do is
Either uses Unix time and an INTEGER column - see UnixToDateTime function for instance in DateUtils.pas unit;
Or a TEXT column and ISO-8601 encoding (my preferred).
Then you will have to map this value in DataSnap - I know that our mORMot units allow this, and I do not know exactly for DataSnap.
Also take a look at all integrated date/time functions in SQlite3 which are pretty complete.
I had the same issue as all my SQLite fields were showing up as WideMemo fields in the DBGrid.
One simple solution is to populate the corresponding sqlite table with at least one (1) row of 'proper' data (no empty fields).
When VCL components are connecting (for the first time), they seem to be able to create fields with the right type.That is, if you right click on a TSQLDataSet component and select the Fields Editor -> Create fields, with a table that has at least one row of data, the fields will be correctly mapped.
I have not checked if that works for the DATE type, but it does work for integers, doubles and text.
For some reason, if the table is empty, all fields are created as WideMemo blobs.

change date format in sqlite

Change this date format which is in sqlite db 12/10/11 to 12-10-11 (mm-dd-yy) I am unable to do so .I am a noob in sqlite and have to parse this value SELECT strftime('%d-%m-%Y',Date) from report but I am getting null as sqlite db excepts value in mm-dd-yy so How do I convert format 12/10/11 to 12-10-11 (mm-dd-yy) .Thanks in advance .Really appreciate the help.
The short answer:
If you have a text string stored as "12/10/11" that you want reported as "12-10-11", you should use the replace(X,Y,Z) function, to replace occurrences of Y in X with Z. Thus:
SELECT replace('12/24/11','/','-');
will return:
12-10-11
The long answer:
First, dates do not actually exist as a proper datatype in SQLite. They're stored as either TEXT, REAL, or INTEGER values. See date and time datatype in SQLite. So it depends upon how your date was stored in the database.
Second, you seem to be implying that you stored the date in a "mm/dd/yy" format. That's not a valid/useful TEXT format to be storing date/time values (as the date cannot be sorted, cannot used in "greater than" and "less than" operations, cannot be used in SQLite date functions, etc.). You really want to store datetime values in one of the formats listed in the "Time strings" section of the date and time functions document.
So, generally you should store your date/time values in one of those formats, use NSDateFormatter to convert that to a NSDate when you retrieve it from the database. And when you want to display the date value in your app, use whatever format you want for output.
But, if you don't care that the dates are stored as text strings and are not effectively usable as dates in SQLite, then just treat it as a plain old TEXT string and use TEXT functions, such as replace(X,Y,Z) to replace occurrences of "/" with "-", as outlined above.

How to get the Date and time value from DateField in Blackberry Programming?

I am using DateField to display the Date and Time in my Blackberry Program,if the user changes the date it should be updated in the database.But the DateField component returns as a long value, i cannot do anything with this long value.If anyone knows how to get the Date and Time value as String please help me.
net.rim.device.api.i18n.SimpleDateFormat will let you easily format a long date/time into any String Date/Time you want.
If you want to just get the individual components of the long date/time as individual numeric values, use java.util.Calendar

How would I store a date that can be partial (i.e. just the year, maybe the month too) and output it later with the same specifity?

I want to let users specify a date that may or may not include a day and month (but will have at least the year.) The problem is when it is stored as a datetime in the DB; the missing day/month will be saved as default values and I'll lose the original format and meaning of the date.
My idea was to store the real format in a column as a string in addition to the datetime column. Then I could use the string column whenever I have to display the date and the datetime for everything else. The downside is an extra column for every date column in the table I want to display, and printing localized dates won't be as easy since I can't rely on the datetime value... I'll probably have to parse the string.
I'm hoping I've overlooked something and there might be an easier way.
(Note I'm using Rails if it matters for a solution.)
As proposed by Jhenzie, create a bitmask to show which parts of the date have been specified. 1 = Year, 2 = Month, 4 = Day, 8 = Hour (if you decide to get more specific) and then store that into another field.
The only way that I could think of doing it without requiring extra columns in your table would be to use jhenzie's method of using a bitmask, and then store that bitmask into the seconds part of your datetime column.
in your model only pay attention to the parts you care about. So you can store the entire date in your db, but you coalesce it before displaying it to the user.
The additional column could simple be used for specifying what part of the date time has been specified
1 = day
2 = month
4 = year
so 3 is day and month, 6 is month and year, 7 is all three. its a simple int at that point
If you store a string, don't partially reinvent ISO 8601 standard which covers the case you describe and more:
http://en.wikipedia.org/wiki/ISO_8601
Is it really necessary to store it as a datetime at all ? If not stored it as a string 2008 or 2008-8 or 2008-8-1 - split the string on hyphens when you pull it out and you're able to establish how specific the original input was
I'd probably store the datetime and an additional "precision" column to determine how to output it. For output, the precision column can map to a column that contains the corresponding formatting string ("YYYY-mm", etc) or it can contain the formatting string itself.
I don't know a lot about DB design, but I think a clean way to do it would be with boolean columns indicating if the user has input month and day (one column for each). Then, to save the given date, you would:
Store the date that the user input in a datetime column;
Set the boolean month column if the user has picked a month;
Set the boolean day column if the user has picked a day.
This way you know which parts of the datetime you can trust (i.e. what was input by the user).
Edit: it also would be much easier to understand than having an int field with cryptic values!
The informix database has this facility. When you define a date field you also specify a mask of the desired time & date attributes. Only these fields count when doing comparisons.
With varying levels of specificity, your best bet is to store them as simple nullable ints. Year, Month, Day. You can encapsulate the display logic in your presentation model or a Value Object in your domain.
Built-in time types represent an instant in time. You can use the built in types and create a column for precision (Year, Month, Day, Hour, Etc.) or you can create your own date structure and use nulls (or another invalid value) for empty portions.
For ruby at least - you could use this gem - partial-date
https://github.com/58bits/partial-date

Resources