1
DECLARE @temp TABLE
(
      iLeadID INT ,
      Title VARCHAR(MAX) ,
      AlertDate DATETIME
)

DECLARE @iLeadID INT
DECLARE @getiLeadID CURSOR

SET 
@getiLeadID = CURSOR FOR
   SELECT iLeadID FROM LeadsContracts 

OPEN @getiLeadID

FETCH NEXT FROM @getiLeadID INTO @iLeadID

WHILE @@FETCH_STATUS = 0 
BEGIN
        INSERT  INTO @temp
                SELECT  @iLeadID ,
                        'Disclosure' ,
                        CONVERT(VARCHAR, dtDisclosure, 101) 'Date'
                FROM    LeadsContracts
                WHERE   iLeadID = @iLeadID
                        AND dtDisclosure IS NOT NULL
        INSERT  INTO @temp
                SELECT  @iLeadID ,
                        'Due Diligence' ,
                        CONVERT(VARCHAR, dtDueDiligence, 101) 'Date'
                FROM    LeadsContracts
                WHERE   iLeadID = @iLeadID
                        AND dtDueDiligence IS NOT NULL
        INSERT  INTO @temp
                SELECT  @iLeadID ,
                        'Finance Appraisals' ,
                        CONVERT(VARCHAR, dtFinanceAppraisals, 101) 'Date'
                FROM    LeadsContracts
                WHERE   iLeadID = @iLeadID
                        AND dtFinanceAppraisals IS NOT NULL
        INSERT  INTO @temp
                SELECT  @iLeadID ,
                        sFreeTextCustom1 ,
                        CONVERT(VARCHAR, dtFreeTextDate1, 101) 'Date'
                FROM    LeadsContracts
                WHERE   iLeadID = @iLeadID
                        AND dtFreeTextDate1 IS NOT NULL
        INSERT  INTO @temp
                SELECT  @iLeadID ,
                        sFreeTextCustom2 ,
                        CONVERT(VARCHAR, dtFreeTextDate2, 101) 'Date'
                FROM    LeadsContracts
                WHERE   iLeadID = @iLeadID
                        AND dtFreeTextDate2 IS NOT NULL

        FETCH NEXT FROM @getiLeadID INTO @iLeadID
    END
  CLOSE @getiLeadID
  DEALLOCATE @getiLeadID

  SELECT    * ,
            ( CASE WHEN 1 = 1
                   THEN ( SELECT TOP 1
                                    sEmail
                          FROM      UserAccount objUA
                          WHERE     EXISTS ( SELECT iUserID
                                             FROM   GroupAgent
                                             WHERE  iUserID = objUA.iUserID
                                                    AND iGroupID = t1.GroupID
                                                    AND btAdminFlg = ( (1) ) )
                        )
                   ELSE ''
              END ) AS AdminEmail
  FROM      ( SELECT    * ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            sFirstName + ' ' + sLastName
                                    FROM    Lead
                                    WHERE   iLeadID = objTemp.iLeadID
                                  )
                             ELSE ''
                        END AS LeadName ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            sEmail
                                    FROM    Lead
                                    WHERE   iLeadID = objTemp.iLeadID
                                  )
                             ELSE ''
                        END AS LeadEmail ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            iUserID
                                    FROM    AssignLeadUser
                                    WHERE   iLeadID = objTemp.iLeadID
                                  )
                             ELSE ''
                        END AS AgentID ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            sFirstName + ' ' + sLastName
                                    FROM    UserAccount
                                    WHERE   iUserID = ( SELECT
                                                              iUserID
                                                        FROM  AssignLeadUser
                                                        WHERE iLeadID = objTemp.iLeadID
                                                      )
                                  )
                             ELSE ''
                        END AS AgentName ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            sEmail
                                    FROM    UserAccount
                                    WHERE   iUserID = ( SELECT
                                                              iUserID
                                                        FROM  AssignLeadUser
                                                        WHERE iLeadID = objTemp.iLeadID
                                                      )
                                  )
                             ELSE ''
                        END AS AgentEmail ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            iGroupID
                                    FROM    AssignLeadUser
                                    WHERE   iLeadID = objTemp.iLeadID
                                  )
                             ELSE ''
                        END AS GroupID ,
                        CASE WHEN 1 = 1
                             THEN ( SELECT TOP 1
                                            sName
                                    FROM    [Group]
                                    WHERE   iGroupID = ( SELECT
                                                              iGroupID
                                                         FROM AssignLeadUser
                                                         WHERE
                                                              iLeadID = objTemp.iLeadID
                                                       )
                                  )
                             ELSE ''
                        END AS GroupName
              FROM      @temp objTemp
            ) AS t1
  ORDER BY  AlertDate ASC 

它给了我列2013-04-04 00:00:00.000中的输出AlertDate

我只想得到这样的日期(没有时间):2013-04-04“ 在AlterDate列中。

有人可以帮我提出一些查询或更新我的代码吗?

提前致谢

4

2 回答 2

4

您可以使用date类型而不是datetime

 declare @temp Table
(
iLeadID int,
Title varchar(Max),
AlertDate date
)
于 2013-05-29T14:04:01.243 回答
1

AlterDate输入了日期时间。如果您只想存储日期,请使用 DATE 数据类型。这样做的另一个好处是 DATE 数据类型只占用 3 个字节,而不是 DATETIME 的 8 个字节。

如果您随后需要将 AlterDate 转回 DATETIME,它将使用您当前看到的午夜值。

于 2013-05-29T14:03:51.037 回答