DATE() Not working after BE converted to SQL

William Burke 1 Reputation point
2021-04-07T13:17:23.857+00:00

I converted my Access Backed to SQL yesterday everything went well except now the Date() function doesn't work. The query below returns no records. If I change the dates to "4/7/2021" it works fine. One thing I noticed is if I put in #4/7/2021# it no longer works. So is there some problem with the translator between Access and SQL server? One other thing all the converted dates were converted to DateTime(0). I created another database and changed all the dates to Date types and connected to it but that didn't fix the issue.

SELECT Package.*, Component.CompName, Component.CompType, Component.BDate, Component.Edate, Package.BDate, Package.EDate
FROM Package INNER JOIN Component ON Package.PkgId = Component.PkgId
WHERE (((Component.BDate)<=Date()) AND ((Component.Edate)>=Date()) AND ((Package.EDate)<=Date()));

SELECT Package.*, Component.CompName, Component.CompType, Component.BDate, Component.Edate, Package.BDate, Package.EDate
FROM Package INNER JOIN Component ON Package.PkgId = Component.PkgId
WHERE (((Component.BDate)<="4/7/2021") AND ((Package.BDate)<="4/7/2021") AND ((Package.EDate)>="4/7/2021"));
Developer technologies Transact-SQL
SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2021-04-07T13:37:36.557+00:00

    If you want to use today's date:

    SELECT Package.*, Component.CompName, Component.CompType, Component.BDate, Component.Edate, Package.BDate, Package.EDate
    FROM Package INNER JOIN Component ON Package.PkgId = Component.PkgId
    WHERE Component.BDate <= CAST(GETDATE() AS date) AND Package.BDate <= CAST(GETDATE() AS date) AND Package.EDate >= CAST(GETDATE() AS date);
    

    If you want to use a specific date:

    SELECT Package.*, Component.CompName, Component.CompType, Component.BDate, Component.Edate, Package.BDate, Package.EDate
    FROM Package INNER JOIN Component ON Package.PkgId = Component.PkgId
    WHERE Component.BDate <= '4/7/2021' AND Package.BDate <= '4/7/2021' AND Package.EDate >= '4/7/2021';
    
    0 comments No comments

  2. Tom Phillips 17,771 Reputation points
    2021-04-07T14:50:45.353+00:00
    0 comments No comments

  3. EchoLiu-MSFT 14,621 Reputation points
    2021-04-08T03:16:47.137+00:00

    Hi @William Burke ,

    Welcome to microsoft TSQL forum!

    Your question is related to tsql. My colleague helped you add the tsql tag.
    The sql-server-general forum mainly supports:
    85583-image.png

    Date() is not a built-in function of tsql, a function with similar functions in tsql is GETDATE (Transact-SQL)
    please refer to:

    SELECT Package.*, Component.CompName, Component.CompType,  
    CONVERT(DATETIME,Component.BDate,102) ,CONVERT(DATETIME,Component.Edate,102)   
    ,CONVERT(DATETIME,Package.BDate,102),CONVERT(DATETIME,Package.EDate,102)  
    FROM Package INNER JOIN Component ON Package.PkgId = Component.PkgId  
    WHERE (((Component.BDate)<=GETDate()) AND ((Component.Edate)>=GETDate()) AND ((Package.EDate)<=GETDate()));  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. SQLZealots 276 Reputation points
    2021-04-08T04:28:02.907+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.