To add offset with the UTC date in SQL - sql

Dates in database(SQL) are in UTC. I want to get the dates based on the Timezone.
Say for example if it's IST (UTC+05:30) then I want to get the IST date and time in database. If anyone knows, please guide me to do operation in database (I have date and offset of the particular timezone).

You've tagged as SQL Server 2008 so you will be able to use DATETIMEOFFSET.

No easy & generic method exists if you want to be able to display dates from the past or future. The problem is that you have to take DST at the date into account.
The easiest way is to add a .Net assembly to your database to manage this.

Related

DateTime values read wrongly from Database

I have data stored on a SQL Server in a country that is GMT + 2. So for instance, a DateTime value is something like 1/1/1800 12:00:00 AM.
Now, I have a C# console app, that is running on a server in UK. The C# is reading those values from the remote database. However, the date received are 2 hours less, for instance: 31/12/1799 22:00:00.
I changed culture to en-US, the only thing that changed was the printing of 10:00:00 PM instead of 22:00:00
Any idea why the date values are being "auto-converted" to locale timezone instead of keeping the values as is?
Thank you
Regards
The problem you're experiencing is not related to the culture on your computer, it is related to a conversion of timezones.
When initializing the connection - there's a negotiation happening between you and the server that tells the server the time zone of your location, and does the conversion for the communication automatically for you.
First, try to understand what is stored in the DBMS, and what you want it to store.
Maybe you need to change your DateTime type to one that considers timezones: for example: http://blogs.msdn.com/b/sqlprogrammability/archive/2008/03/18/using-time-zone-data-in-sql-server-2008.aspx
If you don't want to have timezone "adjustments" try to synchronize the DBMS session to be on the same timezone (between your client and the SQL Server) - so no conversions would occur.
There are two things you shouldn't confuse here: (1) how the data is stored in the database; (2) how it is displayed. The database should be storing the information in a timezone-agnostic way; it's up to you how you display it.
This means that there are two things that might be going wrong. Either it's being stored wrong, because the application putting things into the database is not correctly interpreting the date/time that it wants to store, or, more likely, you're converting to local time when you display it.
I'm not sure how you're doing the conversion, but lots of date/time functions will use the default locale if you don't specify one. They will allow you to specify a locale if you want to, though.
If I were you, I'd have a look at the raw data in the database to see whether that's correct.

How can I store date only in datetime field in WebMatrix with Sql Server CE?

I was wondering if there was a way to store a date (example: 01/01/2013) as datetime without SQL Server CE adding the time (example: 12:00:00 AM).
I could always store it as the string "01/01/2013" but I really want to be able to compare the dates on querying the database.
I realize that as long as I only stored the date part, all of the times in the datetime field would have equal values (i.e. 12:00:00 AM), so comparing them wouldn't be a problem and I could just always ignore the time part, however, it seems ridiculous to have this unnecessary data appended to every entry in the table.
Is there a way to store only the date part of the datetime as datetime so that the dates can still be compared in the SQL query or do I just need to live with this overhead and move on?
Side Note:
I just spent the last 30 minutes searching Google and SO for an answer I was sure was already out there, but to my surprise, I couldn't find anything on this issue.
Update:
The conclusion I have come to is that I will just accept the time in the datetime format and let it always default to 12:00:00 AM by only adding the date part during the INSERT statement (e.g. 01/01/2013). As long as the time part always remains the same throughout, the dates will still be easily comparable and I can just trim it up when I convert it to string for screen display. I believe this will be the easiest way to handle this scenario. After all, I decided to use SQL for the power of its queries, otherwise, I might have just used XML instead of a database, in the first place.
No you really can't get rid of the time component. It is part of the data type defined by sql server. I was very annoyed by it until I found that I could still display the dates without the time using JQuery to reformat them with the date formatter plugi:
https://github.com/phstc/jquery-dateFormat
Good Luck!
select CONVERT(date, GETDATE())

NHibernate support for TIMESTAMP with TIMEZONE type in Oracle

Is it possible to map a .Net DateTime to a TIMESTAMP with TIMEZONE type in Oracle using NHibernate? Looks like it's not readily available in "Hibernate"
If this is not available out of the box, can someone please direct to examples of how it may be achieved.
We were not able to find a proper mapping to this using NHb 3.1. However extended support for times (including timezone etc...) is available for SQL Server '07 onwards.
Our solution was to use a varchar field instead of timestamp and format the dates to include the timezone information. The consumer of this data was able to handle this change accordingly.
You could also convert them to UTC and store that.

Displaying Time Zone information in Reporting Service Reports (SQL 2005)

I have a Microsoft SQL 2005, reporting project. I want to display the Current date, time and timezone information on the page header. That is, I want to get a display as below
6/17/2009 12:25:11 PM +05:30
I added a text box to the page header. When set its expression to =Now, used the FormatDateTime function but they all either display only, date or time or date & time but not the timezone.
When I set the Format property of textbox to "o" and I got the following display 2009-06-17T12:37:36.2347500+05:30. This does have the timezone, but date and time info is not very friendly.
Is there anyway I can display the current date time and timezone info the the format (6/17/2009 12:25:11 PM +05:30) I require?
Thanks
Shreedhar
use
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt K")
Please use this:
DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt K")
Be careful with the format you define "friendly". Since you want to display the timezone I suppose your app has to be used by people living all over the world: for most of them the format "MM/dd/yyyy" is not friendly at all.
Think of a date like "06/10/2009": for English people it's clearly the 10th of June, but for Latin people it will likely be the 6th of October.
The ISO format is not very friendly, I agree, but the informations are displayed in a hierarchical order from the greatest to the smallest. Besides almost nobody uses natively it, and this can be a plus, since everybody will have to understand what it is looking at, without making false assumptions based on his/her locale.

How to convert a SqlServer DateTime to universal time using SQL

I have a database with DateTime fields that are currently stored in local time. An upcoming project will require all these dates to be converted to universal time. Rather than writing a c# app to convert these times to universal time, I'd rather use available sqlserver/sql features to accurately convert these dates to universal time so I only need an update script. To be accurate, the conversion would need to account for Daylight savings time fluctuations, etc.
A User Defined Function would allow you to write an SQL query that looks like this:
SELECT toUTC([MyDateColumn], [MyTimeZoneColumn]) FROM [MyTable]
Then you get universal times back from the server without a lot of ugly syntax in the query itself. Now you could build the UDF for this with regular SQL similar to what Chris posted, but SQL Server 2005 and later will let you build the UDF using CLR (.Net: C# optional) instead. It has much better support for dates and can do a better job taking timezones and daylight savings time into account.
check out the convert function and the getutcdate function?
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Check out this link from CodeProject.com - it does exactly what you want: take a date and a time zone, pass them to a UDF, and get the date in UTC or any other time zone.
IMPORTANT: Check the comments of that article - the author wasn't allowed to revise the article after a certain point, and there is an updated version of the code used for the UDFs in the comments that addresses some issues not found in the original article code.
ALSO IMPORTANT: Don't use this for querying large data sets. It's perfectly fine for a one-time load into a database, or for returning a UTC date for a single row (like a user login table or what have you.)
If you want performance, the only really acceptable method for time zone conversion is to have a lookup table that handles every possible time zone conversion for every single hour in a year, with a case statement to handle rollovers between years (ie December 31 - January 1 or vice versa.) Yes, the table is huge, but the query performance is nil.
SQL Doesn't have anything built in for this.
Two ways would be the C# application (you mentioned you don't want) or writing a really complicated update statement with something like:
UtcDate = DATEADD(hour, CASE WHEN OriginalDate BETWEEN x AND y THEN 4
WHEN OriginalDate BETWEEN x2 AND y2 THEN 5 ... END, OriginalDate)
Note - I'd recommend the C# app plus something like TZ4Net to handle the conversion.