SQL Datetime as time query - sql

I would like the below select statement to be show as time rather than datetime. I'm aware that a cast as solution is likely however given I am trying to Cast as a "grouped by time", I am not finding a solution.
SELECT
DATEADD(hour, DATEDIFF(hour, 0,dbo.tbReceiptLine.Time), 0) AS Hourly,
DATEADD(minute, DATEDIFF(minute, 0, dbo.tbReceiptLine.Time) / 30 * 30, 0) AS [30 Mins]
I would like to show this as time only.

In SQL Server 2008 onwards you can cast to time datatype:
SELECT CAST(dbo.tbReceiptLine.Time as time)
See The ultimate guide to the datetime datatypes

Related

Select DATE part from TIMESTAMP in SQL Server?

I have this part of a query made in POSTGRE and I want to change it for using it in SQL Server, but there is an error in DATE() function.
AND DATE(A.SERVERTIME) = DATE(B.SERVERTIME) + 1
SERVERTIME is TIMESTAMP datatype.
I want to know how to get the date part from SERVERTIME.
Assuming you want to add days;
AND cast(A.SERVERTIME as date)= cast(DATEADD(day, 1, B.SERVERTIME) as date)
Edit: As Larnu warned, timestamp can not be casted to date. However it can be casted to date if it goes through DATEADD function.
Hence it should be;
AND cast(DATEADD(day, 0, A.SERVERTIME) as date)= cast(DATEADD(day, 1, B.SERVERTIME) as date)

Converting Unix time to DateTime 103

I have a stored procedure that joins two tables of hotel booking data, however, the API that I pull my data from uses Unix time. I need to convert this to DateTime to match with my companies fields.
Currently, my conversion looks like this.
IIF([start] IS NOT NULL,
CONVERT(varchar(10), [start], 103),'') as 'ArrivalDate'
This just returns the value 1547310796 so no conversion has been done.
How do I convert the value to match 103 Date Time?
This should do it:
SELECT DATEADD(second, 1547310796 - DATEDIFF(second, GETDATE(), GETUTCDATE()), '1970-01-01')
The DATEDIFF(second, GETDATE(), GETUTCDATE()) part will give you how far behind you are from UTC time. You need to subtract that many seconds from the UTC timestamp to get the local time.
I just wanted to come back to this and add another answer in that works if you just want the date and not time.
SELECT cast(DATEADD(S,[start], '1970-01-01') as date) as 'ArrivalDate'
works on Oracle/Sql
select TO_date ('19700101000000','YYYYMMDDHH24MISS') + NUMTODSINTERVAL(<YOUR_COLUMN>+ decode (sessiontimezone, '+01:00', 3600, '+02:00', 7200, 0) , 'SECOND') as ArrivalDate

add time greater than 24 hours to datetime in sql

I have one datetime field, '2017-05-03 10:00:00' and I want to add the time with field greater than 24 hours, like '30:05' which means 30 hours and 5 minutes in the datetime field. how could I do that? thanks. I'm using sql server 2014.
To get the hours:minutes to minutes:
DATEDIFF(MINUTE, 0, timestr)
Where timestr is your 'h:m' string for input.
The result will be minutes. You should be able to then use this result with the DATEADD function.
DATEADD(minute, DATEDIFF(MINUTE, 0, timestr), dateTimeColumn)
So in an UPDATE I'd expect something like:
SET dateTimeColumn = DATEADD(minute, DATEDIFF(MINUTE, 0, timestr), dateTimeColumn)

Get tomorrows date

I am trying to get tomorrows date in a sql statement for a date comparison but it is not working.
Below is my code:
select *
from tblcalendarentries
where convert(varchar,tblcalendarentries.[Start Time],101)
= convert(varchar, GETDATE() +1, 101)
To get tomorrows date you can use the below code that will add 1 day to the current system date:
SELECT DATEADD(day, 1, GETDATE())
GETDATE()
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
DATEADD(datepart , number , date)
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
So adding this to your code in the WHERE clause:
WHERE CONVERT(VARCHAR, tblcalendarentries.[Start Time], 101) =
CONVERT(VARCHAR, DATEADD(DAY, 1, GETDATE()), 101);
First off, GETDATE() will get you today's date in the following format:
2013-04-16 10:10:02.047
Then using DATEADD(), allows you to add (or subtract if required) a date or time interval from a specified date. So the interval could be: year, month, day, hour, minute etc.
Working with Timezones?
If you are working with systems that cross timezones, you may also want to consider using GETUTCDATE():
GETUTCDATE()
Returns the current database system timestamp as a datetime value. The database time zone offset is not included. This value represents the current UTC time (Coordinated Universal Time). This value is derived from the operating system of the computer on which the instance of SQL Server is running.
Try the below:
SELECT GETDATE() + 1
This adds one day to current date
Specify size of varchar in convert()
where convert(varchar(11),tblcalendarentries.[Start Time],101) = convert(varchar(11), GETDATE() +1, 101)
I would write:
where
DATEADD(day,DATEDIFF(day,0,tblcalendarentries.[Start Time]),0) =
DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
This avoids converting dates to strings entirely, whilst also removing the time portion from both values.

Convert this SQL Server query to Oracle

I have the following in a SQL Server query which I have to convert to Oracle sp
DATEADD(dd, 0, DATEDIFF(dd, 0, DATEADD(ss, -L_LAST_TIME, TR.TR_DATETIME))) AS TRDATE,
Essentially you subtract L_LAST_TIME seconds from TR_DATETIME and then truncate the time part and keep only the date part.
You can divide intervals:
select trunc(TR.TR_DATETIME - interval L_LAST_TIME SECOND) AS TRDATE
or
select trunc(TR.TR_DATETIME - NUMTODSINTERVAL(L_LAST_TIME, 'SECOND')) AS TRDATE
We can do arithmetic with dates in Oracle.
select trunc(tr.tr_time - (l_last_time/86400)) as trdate
from tr
/
Dividing l_last_time by 86400 turns a number of seconds into a fraction of day. Subtracing it from the tr_time column gives you a new, earlier date. Truncating a date removes the time component.