如何在SQL Server中使用系统日期或当前日期

时间:2015-02-26 03:48:53

标签: sql-server sql-server-2014

例如,在数据库"图书馆"中,我希望在名为" Sharpstown&#的分支中获取图书标题,借款人的姓名和地址。 34;截止日期是今天?

这就是我做select语句的方法 -

SELECT BOOK.Title,BORROWER.Name, BORROWER.Address
FROM BORROWER,BOOK,LIBRARY_BRANCH,BOOK_LOANS
WHERE BOOK.BookID = BOOK_LOANS.BookID AND
      LIBRARY_BRANCH.BranchID = BOOK_LOANS.BranchID AND 
      BOOK_LOANS.CardNo = BORROWER.CardNo AND
      LIBRARY_BRANCH.BranchName = 'Sharpstown' AND 
      BOOK_LOANS.DueDate = now() -- This is where I want to compare the dates  

错误是:

  

'现在'不是公认的内置函数名称。

2 个答案:

答案 0 :(得分:2)

对于SQL Server,请使用getDate()而不是now()。根据{{​​1}}的数据类型,您可能还有其他问题,因此您可能需要BOOK_LOANS.DueDate

CAST

" ... and CAST(BOOK_LOANS.DueDate as date) = CAST(getDate() as date) " SQL Server中的类型具有一天的粒度/精度。默认情况下,date会返回值" getDate()" type,其粒度/精度为毫秒。

答案 1 :(得分:1)

您无法在SQL Server中使用NOW。在SQL Server中,我们有GETDATE()

SELECT BOOK.Title,BORROWER.Name, BORROWER.Address
FROM BORROWER,BOOK,LIBRARY_BRANCH,BOOK_LOANS
WHERE BOOK.BookID = BOOK_LOANS.BookID 
  AND LIBRARY_BRANCH.BranchID = BOOK_LOANS.BranchID 
  AND BOOK_LOANS.CardNo = BORROWER.CardNo 
  AND LIBRARY_BRANCH.BranchName = 'Sharpstown' 
  AND BOOK_LOANS.DueDate = CONVERT(DATE, CAST(GETDATE() AS VARCHAR(12)), 105)