Solved

SQL Query date / datediff

Posted on 2014-04-04
4
249 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
4 Comments
 
LVL 142

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 32

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:ScottPletcher
ID: 39978448
WHERE
    SMS_G_System_OPERATING_SYSTEM.InstallDate >= CONVERT(char(8), GETDATE(), 112)
0
 
LVL 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now