如何在存储过程sql中添加秒和小时

时间:2013-11-18 09:25:26

标签: c# sql sql-server linq stored-procedures

我正在使用Microsoft SQL Server Managment Studio 2008.我正在尝试从数据库中获取信息。有linq查询的例子

    var fbPost = db.FacebookStatusUpdates
                .Where(f => f.Status == FacebookNotificationStatus.Active &&
                            f.Alarm.User.FbStatus == true)
                .AsEnumerable() 
                .Where(f => f.FacebookUpdateTime - f.ClientTime.Offset <=
                            DateTimeOffset.Now.UtcDateTime.AddSeconds(f.Offset))
                                .ToList();

我的存储过程是:

 declare @date2 datetimeoffset=getutcDate();
 select a.*
 from [Alllarm].[dbo].[FacebookStatusUpdates] a
 inner join [Alllarm].[dbo].[Alarms] b
 inner join [Alllarm].[dbo].[Users] o
  on o.[Id] = b.[User_id] on b.Id=a.[Alarm_id]
 where o.[FbStatus] = 1 and a.Status=2
 and   DATEADD(hour,datepart(tz,a.ClientTime),a.FacebookUpdateTime)<=DATEADD(second, a.Offset,    @date2);

Thare是我的模特:

    public class User
{
    public long Id { get; set; }
    public string FacebookID { get; set; }
    public string AccessToken { get; set; }
    public DateTime UpdateTime { get; set; }
    public bool? FbStatus { get; set; }

    public virtual Device Device { get; set; }
    public virtual List<Alarm> Alarms { get; set; }
    public virtual List<Sms> Sms { get; set; }
}

public class Alarm
{
    public long Id { get; set; }
    public DateTime StartDate { get; set; }
    public int Snoozes { get; set; }
    public bool Repeat { get; set; }
    public DateTime AlarmUpdateTime { get; set; }

    public virtual User User { get; set; }
    public virtual List<FacebookNotificationStatus> StatusUpdates { get; set; }
}

public class FacebookStatusUpdate
{
    public long Id { get; set; }
    public DateTime FacebookUpdateTime { get; set; }
    public string PostId { get; set; }
    public DateTime? FacebookPostTime { get; set; }
    public DateTimeOffset ClientTime { get; set; }
    public int Offset { get; set; }

    public virtual FacebookNotificationStatus Status { get; set; }
    public virtual Alarm Alarm { get; set; }
}

但是当我运行存储过程时,没有任何事情发生。我想我错过了那一行

and   DATEADD(hour,datepart(tz,a.ClientTime),a.FacebookUpdateTime)<=DATEADD(second, a.Offset,    @date2);

有人能帮助我吗?

a.ClientTime的类型为DateTimeOffset 2013-11-13 12:03:36.0000000 +02:00 a.Offset type int(seconds)

1 个答案:

答案 0 :(得分:0)

Probem在那里:

         DATEADD(hour,datepart(tz,a.ClientTime),a.FacebookUpdateTime)

datepart(tz,a.ClientTime)以分钟为单位返回偏移量,因此我更改为

         DATEADD(minute,datepart(tz,a.ClientTime),a.FacebookUpdateTime)

2013-11-13 12:03:36.0000000 +02:00 datepart return 120.