I have the following columns in a table
Year Julian_Day Time_HHMM Seconds Decimal_Day
2015 271 2000 0 0.7415013
The ouput datetime is "2015-09-28 20:00:00"
I tried to figure out how to convert those parameters to datetime but I havent had luck.
Any help is welcome
You should be able to do this by nesting calls to the DATEADD function for the various components:
SELECT DATEADD(SECOND, [Seconds], DATEADD(MINUTE, CAST(RIGHT([Time_HHMM], 2) AS INT), DATEADD(HOUR, CAST(LEFT([Time_HHMM], 2) AS INT), DATEADD(DAY, [Julian_Day] - 1, DATEADD(YEAR, [Year] - 1900, 0)))))
FROM YourTable
Note I have not used the Decimal_Day column.
DATEADD takes integer parameters for the components, so you may need some additional casting on the columns to INT, if they are currently of type VARCHAR.
[https://social.msdn.microsoft.com/Forums/sqlserver/en-US/47d773c3-dae2-41cc-8770-5f2f24a1c665/julian-date-to-datetime-conversion][1]
SELECT DATEADD(YEAR, #JulianDate / 1000 - 1900, #JulianDate % 1000 - 1)
Related
I am trying to get a smalldatetime value of "9pm today" in a query. I thought I could use
DATEADD(HOUR, 21, CONVERT(date, GETDATE()))
but SQL Server doesn't like that - I get the error
The datepart hour is not supported by date function dateadd for data
type date.
Suggestions for a workaround?
Pretty simple, just cast date back to datetime after casting to date.
Thus you'll get current_date 00:00:00 and then add 21 hours:
select dateadd(hh, 21, cast(cast(getdate() as date) as datetime))
it is because dateadd's 3rd parameter should be datetime type, not date.
SELECT DATEADD(HOUR, 21, CONVERT(datetime,CONVERT(date, GETDATE())))
just add 21 / 24.0 to todays date
Select dateadd(day, datediff(day, 1, getDate()), 1) + (21 / 24.0)
First part, dateadd(day, datediff(day, 1, getDate()), 1), strips time from getdate(),
second part, + (21 / 24.0), adds fractional part of day equal to 9 am
This works because internally, SQL Server represents datetimes as two integers, one for the date, (number of days since 1 Jan 1900), and a second integer for the time, (number of ticks since midnight), which it combines into a decimal value where the integer part is the date integer, and the decimal part is the fraction of one day, so if you add 0.5 to a date, you get noon on that day, etc.
or, for comparison, using dateadd for hours,
Select dateadd(hour, 21, dateadd(day, datediff(day, 1, getDate()), 1))
I want to display records of last 4 months from current date.
I don't want to consider time
How can I get just date part from the below query?
where OrderDate >= DATEADD(month, -4, GETDATE())
If you're using SQL Server 2008, try converting GETDATE() to a DATE directly.
WHERE OrderDate >= DATEADD(month, -4, CONVERT(date, GETDATE()))
http://sqlfiddle.com/#!3/df444/2
Why not use the simple DATEDIFF function
where DATEDIFF(MM, OrderDate, GETDATE()) < 4
If you can't use the DATE type, there's the old way: convert the DATETIME value to CHAR, trim the hour components and then convert it back to DATETIME, so the hour components will be zeroed:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), GETDATE(), 112), 112)
-- -----------------------
-- 2014-02-25 00:00:00.000
The important thing is to use the function over the scalar parameter (and not on the column) to allow the usage of existing indexes.
I wrote a query to obtain First of month,
SELECT DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as First_Of_Month;
for which i do get the appropriate output, but my time stamp shows the current time.
Here's what I am doing in the query, hope i make sense.
using datepart i calculated the no. of days (int) between the 1st and today (27-1 =26)
Then using dateadd function, i added "-datepart" to get the first of the month.
This is just changing the date, what should i look at or read about in order to change the time. I am assuming that it would have something to do with 19000101
For SQL Server 2012 (thanks adrift and i-one)
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
SELECT DATEADD(DAY, 1, EOMONTH(#now, -1));
-- or
SELECT DATEFROMPARTS(YEAR(#now), MONTH(#now), 1);
For SQL Server 2008+
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
-- This shorthand works:
SELECT CONVERT(DATE, #now+1-DAY(#now));
-- But I prefer to be more explicit, instead of relying on
-- shorthand date math (which doesn't work in all scenarios):
SELECT CONVERT(DATE, DATEADD(DAY, 1-DAY(#now), #now));
For SQL Server 2005
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0);
A caveat if you're using this SQL Server 2005 method: you need to be careful about using expressions involving DATEDIFF in queries. SQL Server can transpose the arguments and lead to horrible estimates - as seen here. It might actually be safer to take the slightly less efficient string approach:
SELECT CONVERT(DATETIME, CONVERT(CHAR(6), GETDATE(), 112) + '01');
SELECT convert(varchar(10),DATEADD(DAY, - (DATEPART(DAY,GETDATE())-1), GETDATE()),120)+' 00:00:00' as First_Of_Month;
Just the date
DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
So for a month it is:
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS FirstDatetimeOfMonthmm,
I think the easiest way is to cast the result to date:
SELECT cast(DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as date) as First_Of_Month
One alternative:
SELECT cast(
cast(datepart(yyyy, getdate()) as varchar)
+ '-'
+ cast(datepart(mm, getdate()) as varchar) + '-01 00:00:00'
as datetime)
Build up the date from year/month components, then tack on the 1st and midnight.
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as First_Of_Month;
Try following code:
SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME) AS StartDateTime,
DATEADD(ms, -3, CAST(CONVERT(date, DATEADD (DAY,1,GETDATE())) AS varchar(10))) AS EndDateTime
Result:
StartDateTime
EndDateTime
2022-05-23 00:00:00.000
2022-05-23 23:59:59.997
I want to convert a value from bigint to datetime.
For example, I'm reading the HISTORY table of teamcity server. On the field build_start_time_server, I have this value on one record 1283174502729.
How can I convert it to a datetime value?
Does this work for you? It returns 30-8-2010 13:21:42 at the moment on SQL Server 2005:
select dateadd(s, convert(bigint, 1283174502729) / 1000, convert(datetime, '1-1-1970 00:00:00'))
I've divided by 1000 because the dateadd function won't work with a number that large. So you do lose a little precision, but it is much simpler to use.
Slightly different approach:
Your scenario:
SELECT dateadd(ms, 1283174502729 / 86400000, (1283174502729 / 86400000) + 25567)
FROM yourtable
Generic code:
SELECT dateadd(ms, yourfield / 86400000, (yourfield / 86400000) + 25567)
FROM yourtable
Output:
August, 30 2010 00:00:14
SQL Fiddle: http://sqlfiddle.com/#!3/c9eb5a/2/0
CAST(SWITCHOFFSET(CAST(dateadd(s, convert(bigint, [t_stamp]) / 1000, convert(datetime, '1-1-1970 00:00:00')) AS DATETIMEOFFSET), DATENAME (TZoffset, SYSDATETIMEOFFSET())) AS DATETIME)
The following takes new SQL terminology into account and will return the milliseconds (can also be modified for use in a calculated field.) [SQL Server 2012 or later]
declare #StartDate datetime2(3) = '1970-01-01 00:00:00.000'
, #milliseconds bigint = 1283174502729
, #MillisecondsPerDay int = 60 * 60 * 24 * 1000 -- = 86400000
SELECT DATEADD(MILLISECOND, TRY_CAST(#milliseconds % #millisecondsPerDay AS
INT), DATEADD(DAY, TRY_CAST(#milliseconds / #millisecondsPerDay AS INT),
#StartDate));
select Cast(Cast(19980324 as nvarchar) as Datetime)
If you want precision in milliseconds to be maintained then you could do as follows. Works on SQL server 2016
SELECT dateadd(ms, ((CONVERT(bigint, build_start_time_server)%1000)),
dateadd(ss, ((CONVERT(bigint, build_start_time_server)/1000)%60),
dateadd(mi, ((CONVERT(bigint, build_start_time_server)/1000)/60), '1970-01-01'))) FROM yourtable
The answer I got was
Monday, August 30, 2010 1:21 PM
To convert bigint to datetime/unixtime, you must divide these values by 1000000 (10e6) before casting to a timestamp.
SELECT
CAST( bigIntTime_column / 1000000 AS timestamp) example_date
FROM example_table
Simple and easy solution which won't require any added library or function to be imported
DATEADD(second,YourValue, CAST('1970-01-01 00:00:00' AS datetime))
Did you try FROM_UNIXTIME?
select from_unixtime('your_field') from 'your_table'
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_from-unixtime
Works for me.
If you are familiar with Microsoft Log Parser you probably recognize the Quantize function which will truncate a value to the nearest multiple of another value. It is quite handy for grouping date-time fields into increments.
Date-Time Count
1/1/2010 00:00 100
1/1/2010 00:15 134
1/1/2010 00:30 56
....
I'm trying to find a similar function in Transaction-SQL (specifically SQL Server 2005 or 2008) that will allow me to do a similar grouping on date-time.
You can round to any given number of minutes like so:
DateAdd(Minute, (DateDiff(minute, 0, getutcdate() )/15) * 15, 0)
Instead of using getutcdate() you can use your date column, variable or expression. In addition the number of minutes can be a variable.
declare #minutesQuantize int set #minutesQuantize = 15
DateAdd(Minute, (DateDiff(minute, 0, getutcdate() )/#minutesQuantize) * #minutesQuantize, 0)
The only rule is that the date difference must fit into an integer, I.e. be less than 2 billion. That means you can't do seconds or milliseconds without a more complicated expression.
If you need seconds or milliseconds do this:
dateadd(ms, (datediff(ms, dateadd(day, datediff(day, 0, #date), 0), #date)/#msInterval)*#msInterval, dateadd(day, datediff(day, 0, #date), 0))
Or, if you want to wrap this into a function:
create function dbo.DateRoundMinutes(#dt datetime, #minutes int)
returns datetime
as begin
return DateAdd(Minute, (DateDiff(minute, 0, #dt )/#minutes) * #minutes, 0)
end
go
create function dbo.DateRoundMilliseconds(#dt datetime, #ms int)
returns datetime
as begin
return dateadd(ms, (datediff(ms, dateadd(day, datediff(day, 0, #dt), 0), #dt)/#ms)*#ms, dateadd(day, datediff(day, 0, #dt), 0))
end
Which you can use like this:
select t.dt,
dbo.DateRoundMilliseconds(dt, 500) dt0_5Second, -- Half second
dbo.DateRoundMilliseconds(dt, 5000) dt5second, -- 5 second
dbo.DateRoundMilliseconds(dt, 15000) dt15Second,
dbo.DateRoundMilliseconds(dt, 90000) dt90Second,
dbo.DateRoundMinutes(dt, 2) dt2Minute,
dbo.DateRoundMinutes(dt, 5) dt5Minute,
dbo.DateRoundMinutes(dt, 15) dt15Minute,
dbo.DateRoundMinutes(dt, 90) dt90Minute
from
/* some table having a column dt */
Not directly, it doesn't. But you can group by a function (that you write) that rounds the datetime column to its nearest quarter-hour (or whatever Quantize does).
SELECT
dbo.QuarterHour(DateColumn) AS Date-Time
, COUNT(*) AS Count
FROM MyTable
GROUP BY dbo.QuarterHour(DateColumn)