TSQL datetimeofset without seconds - tsql

Hi is it possible to use datetimeoffset but with seconds and its fraction set to 0? I came up with this solution, but it's looks bit complex. I need to get format like in dt2 with seconds and after set to 0.
I feel should be something better. Tx all.
declare #dt datetimeoffset = '2018-12-18 23:49:18.8866667 +00:00' --getdate();
-- select #dt --2018-12-18 23:49:18.8866667 +00:00
select #dt,
datepart(ss,#dt) Sec,
datepart(ns,#dt) nSec,
dateadd(ns,-(datepart (ns,#dt)),#dt) ns0,
dateadd(ss,-(datepart (ss,#dt)),#dt) ss0,
dateadd(ss,-(datepart (ss,dateadd(ns,-(datepart (ns,#dt)),#dt))),dateadd(ns,-(datepart (ns,#dt)),#dt)) dt0
-- dt0
-- 2018-12-18 23:49:00.0000000 +00:00

Not sure which version of SQL you're using, but here are a couple of options (not sure about performance):
declare #dt datetimeoffset = '2018-12-18 23:49:18.8866667 +01:00'
select
#dt as dt
,cast(convert(varchar, #dt, 100) as datetimeoffset) as dt0 -- pre-sql2012: if you do not mind casting to varchar first (performance ?)
,switchoffset(cast(convert(smalldatetime, #dt) as datetimeoffset), datepart(tzoffset, #dt)) as dt1 -- pre-sql2012: if the TZ matters
,cast(cast(#dt as smalldatetime) as datetimeoffset) as dt2 -- pre-sql2012: if the TZ offset does not matter
,cast(format(#dt, 'yyyy-MM-dd HH:mm zzz') as datetimeoffset) as dt3 -- sql2012 onwards (performance ?)

Related

I got one problem in SQL,i need to get start of next minute value

I need to get the start of next Minute value, that means suppose I got the output for GETDATE() is 19.11.2019 12:52:51 but I need to get 19.11.2019 12:53:00
This is my code:
DECLARE #date DATETIME
SET #date = GETDATE()
DECLARE #increase int = 1;
SELECT DATEADD(mi, #increase,#date) as nextminutedate;
You want to round the date down. One method in SQL Server is:
select dateadd(minute, 1+datediff(minute, 0, getdate()), 0)
This is a little inscrutable. The datediff() calculates the number of minutes from a time of 0 for the current date/time. The dateadd() adds this back in.
Note: This works for minutes. You might have overflow problems with seconds or milliseconds.
For this reason, I rather prefer:
select dateadd(minute, 1+datediff(minute, '2000-01-01', getdate()), '2000-01-01')
I find this is clearer in the intention.

t-sql convert datetimeoffset with utc+01:00 to local amsterdam datetime

I want to run a simple T-SQL SELECT query such that this input (datetimeoffset)...
2015-01-01 00:15:00.0000000 +01:00
OR
2015-05-04 14:15:00.0000000 +02:00
...comes out as this output (datetime):
2015-01-01 01:15:00
OR
2015-05-04 16:15:00
Input is one column and output should also be one column.
Any ideas?
UPDATE 20170126
Ok as always it is never as easy as I think. The query is part of a bigger query, which I have now formulated as follows (see below), the input column being named 'TimeStamp' and the output column being named 'Tijd'. This doesn't work though as it doesn't seem to recognize [TimeStamp] in the declaration of the variable. What am I missing?
DECLARE #dt datetimeoffset = (SELECT CONVERT(datetimeoffset, [TimeStamp]))
SELECT #dt as Original
,CONVERT(datetime2,#dt,1) AS Tijd
,[Id]
,[EanCode]
,[DataAccessPointId]
,[DataSource]
,[ElectricityUsageNormalkWh] AS Piek
,[ElectricityUsageLowkWh] AS Dal
,[DateAltKey] = CONVERT(int, CONVERT(varchar(8), [TimeStamp], 112))
,[TimeAltKey] = DATEPART(hh,[TimeStamp]) * 10000 + DATEPART(mi,[TimeStamp]) * 100 + DATEPART(ss,[TimeStamp])
FROM [dbo].[ElectricityTelemetryData]
I think you have your time offset logic the wrong way around in your question regarding your +/- values, but you have a couple options depending on the data type you want as output:
declare #dt datetimeoffset = (select convert(datetimeoffset,'2015-01-01 00:15:00.0000000 +01:00'))
select #dt as Original
,convert(datetime2,#dt,1) as Converted
,switchoffset(#dt,'+00:00') as Switched
Output:
Original | Converted | Switched
2015-01-01 00:15:00.0000000 +01:00 | 2014-12-31 23:15:00.0000000 | 2014-12-31 23:15:00.0000000 +00:00

T-sql IF Condition date evaluation

I have a simple question regarding T-SQL. I have a stored procedure which calls a Function which returns a date. I want to use an IF condition to compare todays date with the Functions returned date. IF true to return data.
Any ideas on the best way to handle this. I am learning t-sql at the moment and I am more familar with logical conditions from using C#.
ALTER FUNCTION [dbo].[monday_new_period](#p_date as datetime) -- Parameter to find current date
RETURNS datetime
BEGIN
-- 1 find the year and period given the current date
-- create parameters to store period and year of given date
declare #p_date_period int, #p_date_period_year int
-- assign the values to the period and year parameters
select
#p_date_period=period,
#p_date_period_year = [year]
from client_week_uk where #p_date between start_dt and end_dt
-- 2 determine the first monday given the period and year, by adding days to the first day of the period
-- this only works on the assumption a period lasts a least one week
-- create parameter to store the first day of the period
declare #p_start_date_for_period_x datetime
select #p_start_date_for_period_x = min(start_dt)
from client_week_uk where period = #p_date_period and [year] = #p_date_period_year
-- create parameter to store result
declare #p_result datetime
-- add x days to the first day to get a monday
select #p_result = dateadd(d,
case datename(dw, #p_start_date_for_period_x)
when 'Monday' then 0
when 'Tuesday' then 6
when 'Wednesday' then 5
when 'Thursday' then 4
when 'Friday' then 3
when 'Saturday' then 2
when 'Sunday' then 1 end,
#p_start_date_for_period_x)
Return #p_result
END
ALTER PROCEDURE [dbo].[usp_data_to_retrieve]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF monday_new_period(dbo.trimdate(getutcdate()) = getutcdate()
BEGIN
-- SQL GOES HERE --
END
Thanks!!
I assume you are working on Sql2008. See documentation of IF and CASE keywords for more details.
CREATE FUNCTION dbo.GetSomeDate()
RETURNS datetime
AS
BEGIN
RETURN '2012-03-05 13:12:14'
END
GO
IF CAST(GETDATE() AS DATE) = CAST(dbo.GetSomeDate() AS DATE)
BEGIN
PRINT 'The same date'
END
ELSE
BEGIN
PRINT 'Different dates'
END
-- in the select query
SELECT CASE WHEN CAST(GETDATE() AS DATE) = CAST(dbo.GetSomeDate() AS DATE) THEN 1 ELSE 0 END AS IsTheSame
This is the basic syntax for a T-SQL IF and a date compare.
If you are comparing just the date portion for equality you will need to use:
select dateadd(dd,0, datediff(dd,0, getDate()))
This snippet will effectively set the time portion to 00:00:00 so you can compare just dates. So in use it will look something like this.
IF dateadd(dd,0, datediff(dd,0, fn_yourFunction())) = dateadd(dd,0, datediff(dd,0, GETDATE()))
BEGIN
RETURN SELECT * FROM SOMEDATA
END
Hope that helps!

TSQL DateTime Comparison

What I am trying to do is get a result from sql where the dates are in a certain range but its not working correctly, here is my query.
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID
,dbo.ProductDetails.ProductID
,dbo.Products.ProductName
,StartDate
,EndDate
FROM dbo.ProductDetails
INNER JOIN dbo.Products
ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
WHERE CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
AND CONVERT(VARCHAR(10),EndDate, 111) >= #CurrDate
but when the Enddate = #CurrDate the row does not show, but if i make that date just one day higher it gets displayed. Am i doing anything wrong? Any advice will do, thanks.
GetDate() returns date and time, while your conversion to varchar strips away the time part (I'm suspecting that's all it's actually supposed to do). So you would need to do the same conversion for #CurrDate.
If what you want is to simply consider the date only (ignoring the time part), you could use DATEDIFF instead of converting to varchar (see here); example:
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID, dbo.ProductDetails.ProductID,
dbo.Products.ProductName , StartDate, EndDate
FROM dbo.ProductDetails INNER JOIN
dbo.Products ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
-- where StartDate is on the same day or before CurrDate:
WHERE DATEDIFF(day, StartDate, #CurrDate) >= 0 AND
-- and where EndDate is on the same day or after CurrDate:
DATEDIFF(day, EndDate, #CurrDate) <= 0
If you want only DATE comparison, without time use the
cast(CONVERT(varchar, StartDate, 112) as datetime)
I am quite sure that the comparison takes into account the time as well as the date, in which case if the dates are the same but the current time is greater than the time being compared to you won't get that row as a result.
So, what you need to do is just extract the date part and compare those.
GETDATE() gives you date and time
if yours column have only date
then
CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
can give you unexpected result
remember
19.12.2011 14:41 > 19.12.2011 00:00
If you are using SQL 2008 or later, and wanting to compare only the date, not the time, you can also do:
Cast(StartDate as Date)
(This avoids having to convert to a string.)

in sql between #startdate and #enddate does not compare time part ? How to compare time parts of date

I have query like this
SELECT AVAILABILITY_DATE_TIME FROM APPT_PROVIDER_AVAILABILITY
WHERE AVAILABILITY_DATE_TIME between #START_DATE AND #END_DATE
suppose i have
if #startdate = '2/11/2010 11:31:00 AM' and #enddate = '2/11/2010 11:56:00 AM'
then difference is zero its ignoring time part .
If you can provide query plz use
table name : APPT_PROVIDER_AVAILABILITY
, column name : AVAILABILITY_DATE_TIME
and #start_date and #enddate as params
Thanks in advance
It does not ignore the time portion.
Check your data- that you really do have a record in that range
Check your culture settings- that it's not treating the string '2/11/2010' as '11/2/2010' when converting it to a datetime
Change your parameter assignment. Even if your culture setting is right you should still use an unambiguous value for the assignment. Something more like this: 2010-02-11 11:31:00 AM
Check your parameter definitions, that you're not using the newer 'date' type.
If all else fails, write it out in long hand: WHERE ([column] >= #startdate AND [column] <= #enddate)