I've found this from this link:
http://www.sqlite.org/datatype3.html
1.2 Date and Time Datatype
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.date and time functions.
I'm a bit confused as to which I should use. I'm creating a Windows Forms C# application and will be using the built DateTime control.
Which option would best suit me? I think TEXT would, but maybe I'm mistaken. I'm new to SQLite.
Thank you.
I assume you are using System.Data.SQLite? Even if SQLite itself doesn't recognize datetime data type, this data provider recognizes it.
For example, if you use SQLiteDataAdapter to populate a datatable with a select statement, if the data type of a column is datetime, the returned datacolumn will be datetime.
There is limitation to the provider: it can't guess data type when your select contains more than one table. In this case, you can declare the returning data type yourself by prepending your query like this:
types [integer], [text], [boolean], [datetime];
select A.id, A.subject, B.isactive, B.due_date from ...
The provider stores the data as text like 2009-04-01 17:42:38.828125. SQLite is fine with this format, for example you can calculate the next day with:
select datetime('2009-04-01 17:42:38.828125', '+1 days');
EDIT: you specify data type as datetime like this:
create table C ( d datetime );
I have been using Text and haven't had any problem with it.
Related
consider me a beginner in c#
I am doing some changes in a pre developed software (C#.Net) , we are saving data by datewise , Currently in insert query (build in c#) we are passing GETDATE() to save today date , but now we have to save data on the basis of a different date.
When I am building query in c# , I m passing that a datetime variable into query
after conversion , conversion as follow
Date_Stamp = DateTime.ParseExact(dt.Rows[0][0].ToString(), "MM/dd/yyyy HH:mm:ss tt", new CultureInfo("en-IN"));
but it is showing error "String was not recognized as a valid DateTime.".
The reason to convert is coz these date field are getting displayed in format ToString("yyyy-MM-dd"));
Which will give 2017-07-13 14:56:30.233 as 13-jul-2017 on front end (as per requirement). We cant change this part of code as it is being used in lot of places , hard to change .
Problem is
variable storing value as
2017-12-07 00:00:00.000
which give after conversion 07-Dec-2017 [wrong - it is needed as 12-jul-2017]
GETDATE storing value as
2017-07-12 14:56:30.233
which is after conversion coming right as 12-jul-2017
I know there is no datetime format in sql server when it come to storing data
But can we store value from variable [2017-12-07 ] as [2017-07-12 ] ?
How GETDATE() give us date in year-month-date format
?
Neither .NET's nor SQL Server's date related type have any format. All of them are binary values, just like integers and decimals. Formats apply only when they are explicitly or implicitly converted to strings, or parsed from strings.
Assuming your query looked something like SELECT GETDATE(), ... and you loaded the results to an DataTable, the values will be returned as DateTime values. If you used a strongly-typed DataTable you could just use the value. With a generic DataTable the value will be boxed and return as an object.
All you have to do is just cast the field value to DateTime :
Date_Stamp = (DateTime)dt.Rows[0][0];
This will also work for date and datetime2 types. datetimeoffset is returned as DateTimeOffset. time is returned as TimeSpan.
The problem in the original is caused because the field value is formatted into a string using the current culture dt.Rows[0][0].ToString() first. Then ParseExact is called trying to parse it using a different format. A simple DateTime.Parse(dt.Rows[0][0].ToString()) would have worked (even though it would be wasteful), since both DateTime.Parse and DateTime.ToString() use the same culture.
UPDATE
Reading date fields from a table has no issues - the values are returned using the appropriate date type. For example, running SELECT StartDate from ThatTable will return DateTime if the table's schema is :
CREATE TABLE ThatTable
(
ID int,
StartDate datetime
)
Problems are caused if, instead of using the correct type, dates are stored as strings in VARCHAR columns. That's a serious bug that needs to be fixed. There is NO assurance that the strings can be parsed to dates at all, or that they follow the same format. It's all too easy for some faulty application code to use eg DateTime.Now.ToString() and store a localized string in there.
Even if the format is the same, it's just wasteful and unreliable. The string takes more storage than the equivalent type, introduces conversion issues, prevents the use of date functions, and the server can't apply date optimizations to queries and indexing.
This happened today and it's the weirdest thing.
I'm using Entity Framework + LINQ to insert DB Values. There is a datetime column and it was working until 2016/04/01.
Today, when i insert some rows and tried to select them with:
Where BulletingDate = Convert(DateTime, '2016-03-01 00:00:00')
This works, but somehow now when i tried to insert values with this:
a.BulletingDate = DateTime.Parse(txtBulletinDate.Text);
entities.Auctions.Add(a);
entities.SaveChanges();
It adds datetime like: 2016-01-04 00:00:00.000
But Bulletin Date Text is: 2016-04-01 00:00:00
I can't seem to figure out.
Please Help.
Date formats are notoriously fragile; it's very easy for queries that you think are running YYYY-MM-DD to suddenly start doing YYYY-DD-MM or similar over something as minor as a different user logging in to the terminal.
Don't rely on dates passed into databases as strings being parsed exactly as you expect. Pass them in as date objects and you'll get much better results.
The datetime type in SQL Server uses fractional seconds. That's the .000 on the end.
If you can, I suggest switching to datetime2 in SQL Server 2008 and up, which has optional fractional seconds. If you don't need to track fractions of a second, then you can use datetime2(0). It plays nicely with C# DateTime.
Bouild your datetime object like this
DateTime x = new DateTime(2016,01,04);//year month day
so you dont have problems when passing it to entity framework
also if your string is in this format "20140104 00:00:00" you should be safe too (at least MSSQL have no problem with that format, not sure if C# is cool too)
I am debugging a query that is getting built in C# maybe with EntityFrameWork - not sure - but it doesn't return any records although it should.
The query has some DateTime fields and they are like this:
personStartDate {4/3/2013 12:00:00 AM}
The value for Date is getting from user interface date picker but I guess it also defaults the time part to 12:00:00 AM somehow.
Then I go to SQL Server and view the table rows and values on their datatime field data looks like this example: '2013-04-23 09:20:38.897'
Do you see any obvious problem right there?
Also I am trying to take the generated SQL from my breakpoint in C# and post it to SQL Server to see what does it return so for a value like {4/3/2013 12:00:00 AM} I am replacing it with 2013-04-03 12:00:00.000 Is that even correct?
Formatting is irrelevant. Internally it won't be in a text format at all, and I'd hope that the query doesn't end up sending the query value to the database as text either.
If you're only interested in the date part, you need to say that in the query:
where foo.Start.Date == personStartDate
(for example - we don't know what your query looks like).
If your field in the database is logically just a Date but is currently a DateTime, you should consider changing your schema to match your logical data model. It'll make things simpler.
If you create a DateTime using DateTime.Today then the time part will default to midnight. That is what the date picker is doing.
Your database contains a time portion too. If that is incorrect you can convert it in sql: Best approach to remove time part of datetime in SQL Server
2013-04-03 12:00:00.000 is noon is 12:00:00 PM. You want 12:00:00 AM which is midnight. You should use 2013-04-03 00:00 in your test or 2013-04-03 or '3 April 2013'. Again, the time portion will default to 00:00.
To get the query to work in c# with the "bad" data in the database, make the query less precise by doing "less than tomorrow more than or equal to today" rather than "equals". Or make the database more precise by dropping the time part - then you can use "equals". If you are using Entity Framework and Linq-to-Entities you may need to use EF's DbFunctions
Ideally you should track down the inserts and updates that are setting the time in the database and stop that happening, then fix the existing data. Then you could change the data type in the database from DateTime to Date.
A Sql Server DateTime value is a pair of 32-bit integers. The first is the count of days from the SQL Server calendar's epoch (1 Jan 1900 00:00:00.000); the second is the count of milliseconds since start of day.
The string representation of that is dependent on (A) the default language setting for your SQL Server instance, (B) the current language setting for the session, (C) the current set dateformat setting, and probably a few other options I've forgotten.
If you care about the string representation, explicitly convert it to a string using convert(varchar(X),your-datetime-value-here,style) using the style of your choice.
Note that SQL Server Date and DateTime values are converted to/from System.DateTime values by the runtime.
I want to get records from sql server 2005 in datetime format like this "dd/mm/yyyy h:m:s"
My database date is default datetime format
AttLogId int
...
DownloadLog datetime
I am trying to retrieve the datetime like this:
SELECT AttLogId ,convert(varchar,DownloadLogDate,103) FROM AttLog
ORDER BY DownloadLogDate
but I only get the date and not the time.
I suggest you don't try to get the values in a particular string format. Fetch them as DateTime values and then format them in the .NET code using DateTime.ToString("dd/MM/yyyy H:m:s").
It's almost always worth keeping data in its "natural" data type (date/time here) for as long as possible, only converting to text when you really need to. So your SQL should just be:
SELECT AttLogId, DownloadLogDate FROM AttLog
ORDER BY DownloadLogDate
How you then retrieve the data will depend on how you're talking to SQL (e.g. LINQ to SQL, using SqlDbReader etc). But you should be able to get it as a DateTime and then format it locally. This will make it easier to test, easier to debug, give you more control over cultural aspects (date separators, possibly specifying a standard specifier instead of a custom one, etc).
it is because you are using 103 , so it will give only date .
if you want more format check this :
http://msdn.microsoft.com/en-us/library/ms187928.aspx
According to need, u can try this
SELECT AttLogId , convert(varchar(10),DownloadLogDate,103) +' '+ convert(varchar(8),DownloadLogDate,108) FROM AttLog ORDER BY DownloadLogDate
Note : firstone is for date and second one is for time in H:M:S
hope this help u..
In a MySQL table, I need to have two fields "StartTime" and "EndTime". My requirements are:
If I do StartTime - EndTime, I need to get the time elapsed (when I subtract them I need to get time in seconds or minutes or whatever).
From "StartTime" I need to get the date (the date when my object was started).
So my question is in what format (or what type) should the fields "StartTime" and "EndTime" be in my MySQL table to meet the above two conditions? Also to which format in C# should I be retrieving the value from MySQL table?
MySQL offers two very similar data types DATETIME and TIMESTAMP. For many applications, either will work, but in some cases, one works better than the other...
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
in general if you can use TIMESTAMP you should, as it is more
space-efficient than DATETIME (it takes half the size of DATETIME).
here is a link to all the possible operations on the date type
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html
Generally in MySql the DateTime will be in 'YYYY-MM-DD HH:MM:SS' format. Refer to this to get the Required.
In c# you should use DateTime. I believe the mySQL equivalent is TimeStamp.