How to convert minutes into HH:MM:SS formate in sql server - datetime

I am having a column in my sql server database table with datatype bigint.
Now i need to convert this column into HH:MM:SS formate.
For this i use this command
SELECT cast(CONVERT(VARCHAR,DATEADD(MS,SUM(mFld),0),8) as Time) FROM tblm
But this command doesn't show right time duration.
For right time duration i use this command
SELECT rtrim(LTRIM(cast((mFld/(60*60)) as char)))+':' +rtrim(LTRIM(cast((mFld%(60*60))/(60)as char)))+':'+rtrim(LTRIM(cast(((mFld%(60*60))%(60)) as char))) FROM tblm
This command shows right result.But resulted data type is varchar.How can i convert this column in to time column with same result.
Example
DECLARE #minites bigint;
SET #minites = 5200020;
SELECT rtrim(LTRIM(cast((#minites/(60*60)) as char)))+':' +rtrim(LTRIM(cast((#minites%(60*60))/(60)as char)))+':'+rtrim(LTRIM(cast(((#minites%(60*60))%(60)) as char)))
SELECT cast(CONVERT(VARCHAR,DATEADD(MS,SUM(#minites),0),8) as Time)

How can i convert this column in to time column with same result.
You can't. Time has the range 00:00:00.0000000 to 23:59:59.9999999.
time (Transact-SQL)

Related

How do i subtract time from datetime in snowflake?

pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)

In Teradata, trying to convert Timestamp(6) to timestamp(0)

A is in the format of timestamp(6). I need it in timestamp(0). The code I am using is the following:
cast(cast(A AS date) as timestamp(0))
FROM 'table'
where A >= '?StartDT'
After inputing the date I want for the parameter I get the 'Invalid timestamp' error.
If A is truly a Timestamp(6) then casting it first as a DATE will affectively trim off the time elements, so when you cast the result to a TIMESTAMP(0) you are going to end up with a time of 00:00:00.
You'll need to also cast the TIMESTAMP(6) field as a time and then add the results together like:
CAST(CAST(A AS DATE) AS TIMESTAMP(0)) + (CAST(A AS TIME(6)) - TIME '00:00:00' HOUR TO SECOND)
You can also use SUBSTRING() to snip off the last 6 characters of the TIMESTAMP(6) field and cast that resulting string to a TIMESTAMP(0):
CAST(SUBSTRING(CAST(A AS CHAR(26)) FROM 1 FOR 19) AS TIMESTAMP(0))
This doesn't address the INVALID TIMESTAMP error you are getting though. Are you certain that field A is a TIMESTAMP(6) and not a VARCHAR() that looks like a Timestamp? What happens when you remove the outer cast, are their any dates in the result that look like they wouldn't convert nicely to a timestamp? Something is not quite right here, and I suspect that it's in your data.

Checking if a Date falls within a Range of Dates in SQLITE

I have an sqlite table with with 2 date columns (beginning & end). I am looking for a query that returns the date range a date falls in. e.g Checking range where 10-02-2016 falls given 2 records as follows
(01-02-2016 - 12-02-2016) & (13-02-2016 - 20-02-2016)
Clearly it falls in the first range but how do I do it in SQLITE. Please help?
I think the problem is next SQLite requires dates to be in format YYYY-MM-DD. With that format you could write a simple query:
select * from table where yourdate between begin and end
If you can't change your format you should look at next sqlite functions https://sqlite.org/lang_datefunc.html
Also you can try substr function to cotact date in needed format.

What format is the Safari History.db history_visits.visit_time in?

When looking at the History.db from Safari, there's a table named history_visits which has a column named visit_time, which is a REAL value. It has values such as 470799793.096987. What format is that in? I'd like to see it in a format such as 12/08/2015 05:12:05.
It's the number in seconds since 00:00:00 UTC on 1 January 2001. It must be coming from an NSDate.
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
— NSDate Class Reference
To get a decent human value out of it, you must add 978307200 (the epoch for 2001-01-01 00:00:00).
This query should give you what you want:
.headers on
select datetime(v.visit_time + 978307200, 'unixepoch', 'localtime') as date, v.visit_time + 978307200 as epoch, v.visit_time, i.domain_expansion, i.url
from history_items i left join history_visits v on i.id = v.history_item
order by i.id desc
limit 100;
Example output:
date|epoch|visit_time|domain_expansion|url
2015-12-31 11:51:27|1451562687.28465|473255487.284646|duckduckgo|https://duckduckgo.com/?q=current+timestamp+2015-12-31+11:51&t=osx
PS: Just for future reference, the Safari db file is located at ~/Library/Safari/History.db
To convert the visit_time value in the history.db in an excel spread sheet, open the history.db file in a tool such as DB browser for SQLLite (Windows) and export the history_visits values to a CSV file.
Open the CSV file and create a column where you will populate your values in human readable time adjusted to your time zone, and use the following formula convert your NSDate:
=((((C2+978307200)/60)/60)/24)+DATE(1970,1,1)+(-5/24)
In the above formula, the time value is in cell C2, and my time zone GMT-5. To adjust to your own time zone adjust the statement in the last set of parenthesis. Presently I have (-5/24) to represent GMT-5.
When I first approached this conversion, I mistakenly assumed the time in the history.db to be epoch time, which starts at 1/1/1970, and did not understand why there was such a skew in time. Adding the required conversion factor +978307200 solved the problem.
I found the domain_expansion field to be null in some cases, here's a modified query:
SELECT SUBSTR(
SUBSTR(url, INSTR(url, '/')+2),
1,
INSTR(SUBSTR(url, INSTR(url, '/')+2),'/') - 1
) domain,
url,
datetime(hv.visit_time + 978307200, 'unixepoch', 'localtime') visit_time
FROM history_items hi
JOIN history_visits hv on hi.id = hv.history_item;

SQLite3 on windows: Convert epoch to normal time

I am trying to convert the following timestamp(in milliseconds since epoch) to normal date-time. Am using sqlite3 on windows xp.
I am using this query:
select datetime((timestamp/86400000)+25569) from table;
(timestamp is the column name which contains the values like 1289325613669,1289325823860,
1289327180545).
I dont seem to be getting the right values. Am i doing something wrong?
Do this:
select datetime('1289325613', 'unixepoch');
The unixepoch modifier expects a value in seconds.
Currently, what you provide to datetime is interpreted as a Julian Day number.
The reference for date and time functions is here

Resources