Solved

SQL Query date / datediff

Posted on 2014-04-04
4
252 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 33

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 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SYbase 4 32
How to construct an if else statement from existing code 6 22
T-SQL: New to using transactions 9 31
2 IIF's in Access query 25 32
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

831 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