I have an application with limited support for running in different time zones. Essentially, the application has one database/environment for the west cost, and one for the east cost.
The applications figures the current time using getdate().
We don't have source code, so replacing getdate with a custom function isn't an option. Is there some way over overriding the Getdate function? Or is it possible to configure a different time zone for different databases? What about different instances?
SQL Server takes its time directly from the host operating system - it is not timezone-aware and there is no way, on one server, to say this database or this instance is in this timezone, and this other database or other instance is in this other timezone. The only way you would be able to accomplish that is to have completely different servers and turn off any date/time synchronization services (including from the host if they're virtual machines).
You should store UTC time using GETUTCDATE() or SYSUTCDATETIME(). Then you don't need to know which timezone a piece of data came from, and it is always easy to convert it to either of these timezones (or any others you need to support later). ASP.NET, for example, has very extensive timezone support.
You can override whatever the source code is sending using an INSTEAD OF INSERT trigger and ignoring anything where they sent GETDATE(). A quick example:
CREATE TRIGGER dbo.whatever
ON dbo.tablename
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.tablename(col1, col2, date_created)
SELECT col1, col2, SYSUTCDATETIME() FROM inserted;
-------------------^^^^^^^^^^^^^^ this overrides what they passed
END
GO
You can also consider the new TIMEZONEOFFSET data type, but since it's not DST-aware, I'm not sure if it is valuable or not for your scenario.
Related
I am developing an application in which user schedules his date, time and event.
I was wondering if there is any possible way that I can store time in hh:mm:ss AM/PM format rather than 24 hour.
I think my question wasn't clear enough , adding some stuff
Problem Definition : Migration from PHP-MSSQL (Windows) web service to PHP-MYSQL (Linux)
Back End was written before y2k its an old program launced as an single platform , prior to me developers ported this program on the web but did not ported the DMS (database management system aka utility for data entry guys) i am not sure about the reason behind this.
Old procedure to enter data , data entry guy used to log in on windows server start the application and enter data.
After migration we can no longer use old DMS program hence i have to write new DMS program.
I asked few question about migration from mssql to mysql before you all can have look here https://stackoverflow.com/questions/22603868/converting-porting-mssql-table-to-mysql-table
Biggest problem that i have facing is data entry guys want their dms just like before not a inch less or more (cant blame them for this).
old dms view
new dms
I am trying my level best to give them old look feel and functionality back as well as wanted to reduce their work since most of the times they have to update an old entry with new dates only , before that they used to do it by deleting whore record and recreation it again.
front end view of date added:
mssql db structure
mysql db structure
You are probably looking for DATE_FORMAT(date,format)
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
You should store dates and times in a database as either a Date, Time or DateTime datatype (depending on what types your db provider supports (MySql reference)). Never store these as a string.
The way the user inputs the value should be determined by their culture settings on the machine:
dateTimePicker1.Format = DateTimePickerFormat.Custom
dateTimePicker1.CustomFormat = Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern
That way if they prefer 12h format, they just set that in control panel
You can get the inputted value like so:
Dim ts As TimeSpan = New TimeSpan(DateTimePicker1.Value.Hour, DateTimePicker1.Value.Minute, DateTimePicker1.Value.Second)
Storing dates and time in the database instead of string makes life a lot easier when you come to read them because you can just format the date or time in any way you wish.
You can then use the same code in your application to show that date or time in the users preferred culture. (Formatting Date and Time for a Specific Culture)
It also allows you to perform queries on the actual date or time which would not be possible (or at least very inefficient) on a string
.ToString("HH:mm") Solved all my problems for a while.
I have a database in MySQL. The values in column named Curr_BaL is updated by different operations performing on it. The application, which is written in Java, accesses that database. When it runs, by default it should retrieve the last updated value. However, I also want to be able to get the value at a specific DATE entered by the user.
I have tried to do my best, but have not successful yet, and my whole application depends on that data.
Your problem is not entirely clear. What I can understand is that you need a way to have your users aware of this "last updated" value.
You have several designs approach for this. I think that the simpler would be to fetch this value when you're authenticating your user, and set it to its session information, so it will be available at any time.
You can also have some kind of service caching this value (since I guess is the same for all users).
A very important thing you didn't mentioned is who updates this value, is an external application? is a process on the same application?.
What I can understand, users date more priority then automaticaly date. Simple way for it's using triggers. Below may be useful:
CREATE OR ALTER trigger on_table_ins for TABLE
active before insert position 0
AS
BEGIN
IF (NEW.DATEFIELD IS NULL) THEN NEW.DATEFIELDD='now';
END
It correct for firebird, so see manual for triggers and insert current date(time) for your RDBMS.
Is it possible to apply some function (user defined / system) to selected columns automatically, may be binding it at schema level.
My scenario is I am saving timestamps of record saving in each table automatically, for which I have used getdate() as default value of those columns, It was working fine till we had our own hosting. But since now we are moving to shared hosting and don't know in which timezone the servers shall be placed in future, I am using GETUTCDATE() to get GMT time.
Since a lot of procedures / functions are already in place, I am looking for something where I don't need to convert this UTC time to my local time explicitly.
So that my Select * from MyTable shall give me time in my fixed timezone using the function I've created.
Let me know if its possible by any way.
Thanks.
It's not exactly clear what you want to do, but there's no way to replace what the SELECT statement asks for with something else: what you ask for in a query is what you get. Unless you replace a table with a view with the same name, but that probably isn't the best approach.
Using a view or function would still mean you have to change your code anyway, so why not just UPDATE all data to UTC time and then do the conversion in your application code? SQL Server has no idea what time zone a client is in anyway, so it isn't possible to do the conversion reliably on the server side. Unless perhaps the client sends the local time zone to the server as a parameter or in CONTEXT_INFO, but there wouldn't be much point because doing it in the client would be simpler anyway.
And of course handling it all in the application will give you a much more flexible, robust solution.
I have a some tables that include a lastupdatedate fiel, the idea being that anytime the information in a row is altered, the lastupdatedate will be reset to the current date/time Setting lastupdatedate using the client side's current datetime is not a good idea. For starters, they may be in different time zones. Although I could solve this problem by storing UCT however a more serious issue is that the clocks of different users will not all be synchronized. What makes more sense is to just use GETDATE() for the lastupdate parameter in the SQL Update command. This way you are guaranteed that all lastupdatedate values will be relative to the same clock, the one on the SQL server.
In ADO.NET this was easy because you directly submitted a SQL statement to be executed but in LinqToSQL you would typically do SubmitChanges.
Is there any easy way to do this with linqtosql outside of creating a stored procedure ?
Another option you could consider would be to create a Scalar function on the database server which exposes that server's current time. You map that scalar function to a LINQ to SQL function and call that method to get your server's time to set the lastupdatedate on your object prior to SubmitChanges.
I also store last modified date and will grab the data from the web server. It has worked fine so far. If your code is running on only one web server, then the LinqToSQL is executed on that server, therefore you will a have a consistent time source. Will that work?
I have a data structure where an entity has times stored as an int (minutes into the day) for fast comparison. The entity also has a Foreign Key reference back to a TimeZone table which contains the .NET CLR ID Name and it's Standard Time/Daylight Time acronyms.
Since this information is stored as time-zone insensitive - I was wondering how in LINQ to SQL I could convert this into a UTC DateTime for comparison against other times that will be in UTC.
Just to be clear this conversion has to be done server-side so that I can execute filtering on the SQL Server and not the client. The reason for this is to ensure we take into account DST for time zones that support it.
I am using .NET 3.5 SP1 and SQL Server 2008.
Ideally, times should be stored in the database in UTC, and only converted to some local timezone (which would include a DST factor where appropriate) for display. This is especially true if "fast comparison" is your goal.
You might find it easiest to add an extra field which contains the UTC time, modify the clients to add this information, and run a script which one-time calculates it for existing entries.
I can store a CurrentOffset field that will need to be updated by a script that will be updated on the hour, every hour to determine the contextual offset.