Date in IST in SQL Server - sql-server-2008

My server is hosted in US or so with some other time zone. Is there an option to get the current date in SQL Server in Indian Standard Time.
select getdate()
What should I write to get the current time in India(or some other country as such).

You should use the DATETIMEOFFSET datatype which includes the timezone, and the SWITCHOFFSET method to switch between timezones. Also: to get the current time, use SYSDATETIMEOFFSET() instead of GETDATE()
-- gets current date/time in the current timezone
SELECT
SYSDATETIMEOFFSET()
-- get the current date/time in your preferred timezone +05:30 UTC being Indian Std. Time
SELECT
SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')

According to this link India's time is 9:30 hours ahead from US. So in-order to get the Indian time, you need to add 9.30 hours to US time.
SELECT DATEADD(hh,9.30,getdate())

Related

SQL dates are returning -1 day off

Trying to capture some information when a user inputs two dates. The information I'm getting back is correct apart from the dates are off by one. So if I ask for 13th 14th 15th I get the correct information back but the dates are 12th 13th 14th.
Found the issue it's to do with BST time being 1 hour ahead. Just not really sure how to rectify it apart from sticking my PC an hour behind.
Check the date stored in the database it might be stored in UTC Format. If it is in UTC Format, you must handle it in your code to match on your time zone.

Convert date to a month in one time zone when time saved is in GMT - SQL

This is related to an earlier question that I had here: Convert datetime to a fixed date of the month including the time - mysql
I am raising a another question because this is regarding the timezone component in the datetime.
I have a list of date time values that are stored in GMT. For the purpose of a report, I want to convert the dates to a single date of a month. (which was the subject of the previous thread I mentioned above). What I also would like to do is the take the time zone into consideration. I will explain.
The date field is always stored in GMT. But the report is generated for the HQ that is in EST. So when there the dates are converted to a single date time (again, part of the previous thread) only the dates that fall into that month in EST (not GMT) should be changed to that month.
For example, let us say I have this date stored in the table:
2016-04-01 00:03:07 (GMT). But for a person sitting in Eastern time zone this is created in the month of March. So, when I do the conversion of the dates to a specific date of a month, this date should be converted to 2016-03-15 00:00:00 and not 2016-04-15 00:00:00
Pretty challenging to me!
I need this in this format as I am integrating with a third party application
CONVERT_TZ should be able to convert to your required timezone. Then, do the rest of the operations on the result instead of doing them on th original date.

MySQL time zone

I am new to database development and am currently working in MySQL.
I have a column that contains all the time zones for USA. I want to be able to get the current time (only time, no date) as of right now for each time zone in each row.
The TimeZone data looks like: +05:00, -03:00 etc etc etc
This is how I have attempted it. I have 2 tables, one country, one city. TimeZone is found within "city" table. I have tried to use the function sys date() but it returns the complete date and the current time on my system, not based on the time zone. Can anyone help me out? Thanks a lot
select TimeZone, Country, sysdate() as "Current Time"
from city, country
where Country='USA' and city.CountryId= country.CountryId;
You should be able to use the convert_tz function:
SELECT CONVERT_TZ(NOW(),'-07:00', city.TimeZone) AS Time, city.Name
FROM city, country
WHERE country.Name='USA' and city.CountryId= country.CountryId;
Assuming your server is in -07:00 timezone.

created_at in where statement and timezone

I'm trying to retrieve all the entries of the day in my model:
where('date(created_at) = date(?)', Date.today)
But, because of the timezone (My local time is UTC+2), I don't have the expected result. For example, if an entry has been created one minute after midnight, in the database, it will be stored at 22:01 (so not "Date.today").
Is there a way to do that ?
EDIT:
As the admin of the website, I want to display the number of the entries on my local time "today" (It's only for statistics purpose!). So I want to keep timestamps store in utc, but convert them to my local timezone during this request!
You can set the correct time zone in several ways. One is described in this answer:
https://stackoverflow.com/a/6118837/567126
adding following to application.rb works
config.time_zone = 'Eastern Time (US & Canada)'
where('date(created_at) = ?', Time.zone.now.to_date)
Time in the database should be stored as UTC. This way, when you do a worldwide website, you can serve each user with their own timezone.
It's the UI (views) responsibility to show the time in the user's local timezone.
For your where you should use Time.now.utc
This way, you are searching the DB with the common UTC.

MySql timestamp timezone weirdness

I want to grab records from a table based on the day of the month the record was created.
This information is stored in a Unix timestamp. During testing I created new test records and threw in some timestamps for specific times that I had converted to timestamps using an online converter. I used...
01/29/2010-02:00:00
Right now I'm using...
FROM_UNIXTIME(timestamp, '%d') == 29
This should work for all times on the 29th day of every month. But it is calculating the timestamp to be 5 hours behind the actual value of the timestamp. When I just run a FROM_UNIXTIME on the timestamp it returns 01/28/2010-21:00:00. I was hoping someone could give an explanation for this, if there is an easy fix or should I just code the program to expect the timezone to be a factor.
The FROM_UNIXTIME function automatically converts the datetime to the current timezone.