How to get date and time difference between 10/12/2010 07:35:02 PM and 2010-11-19 21:51:01.713. Where first date is in MM-DD-YYYY format and Second date is in YYYY-MM-DD Format Rest is time it is also in different format as first format has "pm" in it. Please let me know how to write a query in sql 08 to calculate date and time difference?
The datetime data type in SQL Server is actually a 8-byte number. It may be represented in different formats to please humans but the format has no meaning to SQL Server itself.
To calculate the time difference between to datetime values you can use the built-in DATEDIFF function, which you can find details about here: http://technet.microsoft.com/en-us/library/ms189794.aspx
This will work thanks to the SQL Server ability to parse formatted dates for us:
select datediff(day, '10/12/2010 07:35:02 PM', '2010-11-19 21:51:01.713')
-----------
38
Related
We have data in SQL Server 2008, with multiple date time columns. As date format in SQL Server is a big integer value i.e. number of seconds since 1970, we are using DATEADD() function to convert it into actual date time format. But the issue we are facing is that when we use DATEADD(), we get data in UTC timezone whereas we want data in EST. I tried simply using -4 HR from the time but during NOV - MAR we are 1 HR ahead because the difference is 5 HR when daylight time is off. Is there any function that can be used which considers daylight saving time. Any help is appreciated.
Current Function -> DATEADD(s, columnName, '01/01/1970 00:00:00')
Regards, Sahil
try this:
DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), columnName)
You can use my SQL Server Time Zone Support project for this. It uses IANA standard time zone names, as listed here.
Building on your example, the following first converts from a UNIX Timestamp to a UTC based datetime, then converts to a datetime in the US Eastern time zone.
SELECT Tzdb.UtcToLocal(DATEADD(s, columnName, '1970-01-01 00:00:00'), 'America/New_York')
I would write a UDF that interacts with a time database such as the one from worldtimeserver.com -- I'm not affiliated with them and have found it with a quick internet search.
Further research shows timezonedb.com which has a free (if non-commercial) membership, allows to download the database and has examples on how to use with SQL Server and MySQL.
I am trying to use Oracle SQL Developer to import a CSV file to a table. One of the fields is in day/time format. An example of such a date is '9/15/1993 12:00:00.000 AM'. IN SQL Developer when it asks me what date format to use I enter MM/DD/YYYY HH:MI:SS AM but this creates a ORA-01855 error complaining about AM so I imagine something is wrong. Any ideas?
It's probably more likely to be the milliseconds rather than the AM/PM indicator. Convert to a timestamp first then cast to date: String to date in Oracle with milliseconds
I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.
I am new to Oracle, and I need to save date and time in an Oracle database.
I am using time stamp as datatype for row. But now my problem is it saves date and time in 12 hours format like this 17/11/2011 10:10:10 PM.
But I need it in 24 hours format like 17/11/2011 22:10:10. I didn't understand the results that Google search result provided. Can any one please help me by posting some code.
Oracle always stores timestamps (and dates) in a packed binary format that is not human readable. Formatting is done only when a timestamp (or a date) is converted to a string.
You can control the formatting of your output by coding an explicit to_char. For example
SELECT to_char( your_timestamp_column, 'DD/MM/YYYY HH24:MI:SS' )
FROM your_table
Oracle stores timestamps in an internal format (with a default representation).
You can customize this representation on output like with the to_char() function.
For input (into the database) you can use to_date().
I have 2 questions:
I want to compare a field whose data type is "Date" against a given date. The DB is oracle and being a mysql guy I'm finding it difficult to come up with simple queries.
The field("date_closed") stores date in UTC format (24-Aug-2011 18:55:11 for example) and I want to convert it to PST for comparison.
I tried this query but it returns some extra rows in the data set(obviously):
select * from table1 where trunc(date_closed)=to_date('2011-08-24','yyyy-mm-dd')
How do I covert to PST format before comparison?
In the same query how do I compare "date_closed" against the current date?
You need the NEW_TIME function
Dates don't include timezone in Oracle, and are assumed to be in the database timezone (which may by UTC but probably isn't). You should look at the TIMESTAMP WITH TIMEZONE data types.
Also, bear in mind that if you are comparing to the current date - I assume that you want to strip off the timestamp and compare only the day.
So, if new_time(date_closed,'GMT','PST') translates the date , your where clause will be comparing something like
trunc(new_Time(date_closed,'GMT','PST')) = trunc(sysdate)
to get all records with date_closed on the current day in PST.