Solved

SQL Query date / datediff

Posted on 2014-04-04
4
259 Views
Last Modified: 2014-04-30
Hey

I would like to have the following only find the machines installed from today (4/4/2014) and forward

Howto?

select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceId = SMS_R_System.ResourceId    

WHERE    dateDiff(dd, SMS_G_System_OPERATING_SYSTEM.InstallDate, GetDate()) < 30

Open in new window

0
Comment
Question by:mikeydk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39977547
you may want to read up this article about date/time stuff:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html

hence, syntax like this:
WHERE    dateDiff(dd, SMS_G_System_OPERATING_SYSTEM.InstallDate, GetDate()) < 30
should be banned ...
the correct way would be for example:
WHERE    SMS_G_System_OPERATING_SYSTEM.InstallDate > dateadd(day, 30, GetDate())

now, getdate() contains also a time portion (data type is datetime), so you may want to use the date data type instead: CONVERT(date, GETDATE())
http://technet.microsoft.com/en-us/library/ms188383.aspx

for your query, it would look like this:
WHERE    SMS_G_System_OPERATING_SYSTEM.InstallDate > convert(date, getdate())
0
 
LVL 34

Expert Comment

by:ste5an
ID: 39977548
You should use table alias names to improve readability. E.g.

SELECT  S.ResourceID ,
        S.ResourceType ,
        S.Name ,
        S.SMSUniqueIdentifier ,
        S.ResourceDomainORWorkgroup ,
        S.Client
FROM    SMS_R_System S
        INNER JOIN SMS_G_System_OPERATING_SYSTEM OS ON OS.ResourceId = S.ResourceId
WHERE   DATEDIFF(DAY,OS.InstallDate, GETDATE()) = 1;

Open in new window


btw, there is no "forward"..
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39978448
WHERE
    SMS_G_System_OPERATING_SYSTEM.InstallDate >= CONVERT(char(8), GETDATE(), 112)
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39979415
SELECT SMS_R_SYSTEM.ResourceID, 
       SMS_R_SYSTEM.ResourceType, 
       SMS_R_SYSTEM.Name, 
       SMS_R_SYSTEM.SMSUniqueIdentifier, 
       SMS_R_SYSTEM.ResourceDomainORWorkgroup, 
       SMS_R_SYSTEM.Client 
  FROM SMS_R_System 
       INNER JOIN SMS_G_System_OPERATING_SYSTEM 
               ON SMS_G_System_OPERATING_SYSTEM.ResourceId = SMS_R_System.ResourceId 
 WHERE SMS_G_System_OPERATING_SYSTEM.InstallDate >= DATEADD(DD, 0, DATEDIFF(DD, 0, GETDATE())) 

Open in new window

0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question