assign default value to DateTime property in table entity of azure storage table - azure

How can I assign default value to DateTime property in table entity of Azure Storage Table.
I am trying the code below, but I'm not able to set value (throwing an error from Azure Table while inserting, as DateTime taking has 1/1/0001).
[DefaultValue(DateTime.Now]
public DateTime LastUpdate { get; set; }

As Per msdn, the value that can go in datetime property is "A 64-bit value expressed as Coordinated Universal Time (UTC). The supported DateTime range begins from 12:00 midnight, January 1, 1601 A.D. (C.E.), UTC. The range ends at December 31, 9999."
So you cannot assign DateTime.MinValue. Lowest you can store is new DateTime(1601, 1, 1)

As mentioned by #Aravind in the answer, minimum value for DateTime type attribute supported by Azure Tables is Jan 1, 1600 UTC. Thus setting the default value to DateTime.MinValue will result in an error.
One possible solution for you is to keep this attribute nullable in your model.
public DateTime? LastUpdate { get; set; }
So, when no value is supplied the default value for this would be null. You would need to remove DefaultValue attribute as mentioned in comments above.

Related

sqlite3.OperationalError: default DATE value of column is not constant

I am creating a sqlite3 table that accepts records from a server. There should be one date/text column that also has a datetime DEFAULT value, so I can sync a record which times differ from the server's record.
I found a solution on this forum from here. The problem is it gives me the following error on executing the table creation script: sqlite3.OperationalError: default value of column [updated_at] is not constant.
Table is created:
cur.execute('CREATE TABLE IF NOT EXISTS emp_tb(\
emp_id INTEGER PRIMARY KEY NOT NULL,\
emp_names TEXT NOT NULL,\
emp_number TEXT NOT NULL UNIQUE,\
ent_id INTEGER NOT NULL,\
active INTEGER NOT NULL DEFAULT "0",\
updated_at TEXT NULL DEFAULT (datetime("now", "localtime")),\
syncstatus INTEGER NOT NULL DEFAULT "0")')
Should I create a trigger? or How can I have a default value in format ("YYYY-MM-DD HH:MM:SS.SSS") in case the update misses a spot?
Use single quotes (') for the datetime options. As mentioned in the comments, they will have to be escaped (because the query is delimited with single quotes).

Recognize date format '14-August-2020 12:30:42' as type datetime

I'm trying to convert a datetime column in format '2020/08/14 12:30:42' to a datetime column with format '14-August-2020 12:30:42' using the following function:
select
*,
date_format(starttimecet,'%d-%M-%Y %h:%i:%s) as test
from table
While this function in successful in doing that, the '14-August-2020 12:30:42' format of the test column no longer recognizes it as a datetime column. Is there a way to have a column in this format but still have it recognized as type datetime?
No, once you convert your bona fide datetime literal into a text string, it is no longer datetime. Your current query is actually along the lines of what you should be doing in general. Specifically, you should keep the original datetime column throughout your query as needed, and then display the text you want in the final outer select.

Cassandra - using "date" vs "text" types for a partition date key

We have a schema where the partition key will be a date (yyyy-MM-dd) and we are thinking about choosing the data type between text and date for this partition key.
Does one data type provide benefits over the other and how would they differ in querying/storage?
Here is an example schema.
CREATE TABLE test.user_sessions (
sess_date date (or text),
sess_starttime timestamp,
event_type text,
total_req int,
ended_at timestamp
PRIMARY KEY (sess_date, sess_starttime)
);
Cassandra Date Type :
Value is a date with no corresponding time value; Cassandra encodes date as a 32-bit integer representing days since epoch (January 1, 1970)
Cassandra Text Type :
UTF-8 encoded string; 16 bit for each character
If you store date (yyyy-MM-dd) as date data type each entry will only take 32-bit. On the other hand if you store the date as text it will take 10*16 = 160 bit storage.
As per your comments, if you need maximum portability simply store the information as a timestamp (that is a 64-bit number) corresponding to the something like yyyy-MM-dd 00:00:00 (a truncated time stamp). You can't go wrong with an "universal" number...

Reading excel time value using OpenXML API

I have an excel sheet which has a time column. Its time column is currently in the data type 'Time' (6:00:00PM), however, I've tried with 'Custom' data type (6:00PM) as well.
I read this cell value using openXML library as follows:
row.XCells[9].GetValue()
The value I read is .75. This is the value I see when I change the data type to number. I want to convert this to a timespan in my C# backend. How do I do that?
var ts = TimeSpan.Parse(row.XCells[9].GetValue());
doesn't work.
Excel dates and times can be converted to C# DateTime using DateTime.FromOADate. Once you have the DateTime you can use the TimeOfDay property to get a TimeSpan (the DateTime will have a date of 30-Dec-1899 which is the OLE automation base date)
TimeSpan t = DateTime.FromOADate(row.XCells[9].GetValue()).TimeOfDay;
Console.WriteLine(t.ToString(#"hh\:mm\:ss")); // prints 18:00:00

How to define a class that represents an age group?

I need to be able to define various age groups, i.e. 10 - 11, 12 - 13, etc.
How can I represent an AgeGroup class such that that the age can appropriately group people together today, tomorrow, and in the future, i.e. today's 11 year old could 12 tomorrow (in which case he would be grouped into the 12-13 group instead of the 10-11 year old group, as he was yesterday).
So far I have this:
public class AgeGroup
{
public string Name { get; protected set; }
public DateRange BirthDateRange { get; protected set; }
public Guid Id { get; protected set; }
public Status Status { get; protected set; }
public virtual DateRange ApplicableDateRange { get; set; }
public AgeGroup(Guid id, string name, DateRange birthDateRange,DateRange applicableDateRange)
{
Id = id;
Name = name;
Status = status;
BirthDateRange = birthDateRange;
ApplicableDateRange = applicableDateRange;
}
}
public class DateRange
{
public DateRange(DateTime startDate, DateTime endDate)
{
StartDate = startDate;
EndDate = endDate;
}
DateTime _startDate;
DateTime _endDate;
public virtual DateTime StartDate
{
get { return _startDate; }
set { _startDate = value.ToUniversalTime(); }
}
public virtual DateTime EndDate
{
get { return _endDate; }
set { _endDate = value.ToUniversalTime(); }
}
}
This class defines a range of time where this agegroup is applicable. When the applicable date range is passed, we would need to shift everything in time. But is there a better way to do this?
If you're thinking of storing the age range within a person object of some description, I wouldn't, since (1) it's an easily calculable value from the birth date; and (2) it's not really a property of the object (it could be considered so but its transient nature will make things more complex than need be).
Instead, store only the birth date then provide some code which can give an age range based on that birth date and the current date.
You can still have age ranges but the people themselves do not exist "inside" them. Instead, you would be able to pass a person object to a function and have it give you back the relevant range.
And, if possible, the age ranges should be based on ages rather than birth dates, since the former is unchanging - you don't have to "move" objects between ranges since that's automatic when their range is calculated from their birth date and the current date.
If that's not possible (and it appears from your supplied chart that it may be difficult):
then you can use birth dates as you currently are but with a slight modification to allow for things to change year to year.
Have a season object which has the following fields:
Start date.
A collection of range objects.
This will allow you to have multiple season objects if, for example, the age ranges change in following years. As long as they don't change, you'll only have a single season object with a start date of whenever your 2014/15 season starts.
The range objects themselves are then a collection of objects of the form:
Start birth date End birth date Range name
---------------- -------------- ----------
1993-01-01 1993-07-31 U22
1993-08-01 1994-07-31 U21
1994-08-01 1995-07-31 U20
:
2009-08-01 2010-07-31 U5
2010-08-01 2010-12-31 U4
Then, in order to find out which age group someone belongs to, you:
Find the current season object, based on the date of the season (usually today, though it's conceivable you may want to look at earlier seasons as well).
Go through all the range objects for that season until you find one where the persons birth date falls between the start and end dates for the range. If you don't find an eligible range, then they're not allowed to play.
Then extract the range name.

Resources