Question

I have a field in an Azure table that holds date times in string format. I want to replace this with a DateTime field and convert the values in the string column. What's the best way to approach this?

In a SQL I'd do this by creating a new column and run an update statement...

Was it helpful?

Solution

I haven't tried this myself, but here are the steps that /should/ work. This needs to be a C#/VB/etc script.

  1. Change the definition of the class to contain a /new/ member of type datetime.
  2. Run the linq query to load all the rows from the table, and run a for loop which will populate new datetime variable based on old string variable.
  3. Save the objects to table. //now your table storage contains objects with new and old definition of that variable
  4. Remove the old field from class definition
  5. Load the objects from table again and save them w/o doing anything but marking them dirty. This /should/ get rid of the old member from serialized structures inside the table. Now, if you want the new datetime field to take the name of the original string variable do steps 1 thru 5 again, this time moving the data from one field into another. The only difference is that you don't have to do conversion again.

In general, this is a complete PITA and one of the banes of object databases and storage services.

OTHER TIPS

The Azure Table Storage Client natively understands DateTime (but not DateTimeOffset), so you may want to consider simply letting the SDK take care of the matter for you.

Otherwise, the best way to represent DateTimes as strings is to explicitly store and retrieve them as UTC values written and parsed using the "u" format, e.g.

var dateAsString = myDateTime.ToString("u");

I have no idea if it'll work, but you could try "blissful ignorance":

Real Table store doesn't actually care about the shape of your entities (Dev fabric may be different). Change your entity property to DateTime, but in the Setter test the value is a Datetime, if not parse the value into a DT. Something like

private DateTime myDT
public DateTime MyDT
{
    get
    {
        return myDT;
    }
    set
    {
       DateTime = null;
       if(value is DateTime)
       {
           myDT = value;
       }
       else
       {
           myDT = DateTime.Parse(value);
       }
    }

}

Hopefully over time, or explicitly in a batch, if you simply load the entities and save them again it'll set them all to DateTime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top