Solved

sql date +14 days

Posted on 2014-02-26
4
333 Views
Last Modified: 2014-03-05
i need to filter data on the last 14 days

the code would be something like
Select 06-07, 07-08, 08-09 from UsageLogs where BranchId = 54 AND reportDate between now + 14 days

Open in new window


what code would be correct for this
the data type is date2
0
Comment
Question by:websss
4 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39889891
You want the dateadd function:

http://technet.microsoft.com/en-us/library/ms186819.aspx

Where reportdate between getdate() and dateadd(dd, getdate(), -14)

Open in new window


Should do the trick - between "now" and "14 days ago"
0
 
LVL 39

Accepted Solution

by:
lcohan earned 250 total points
ID: 39889892
Select 06-07, 07-08, 08-09
from UsageLogs
where BranchId = 54
      AND reportDate between cast(getdate() - 14 as date) and cast(getdate() as date)
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39890047
Basically you wan all the records from the table that have a ReportDate in the last 14 days from right now, which means the ReportDate is greater than right now minus 14 days:
Select 
	06-07, 07-08, 08-09 
from 
	UsageLogs 
where 
	BranchId = 54 
	AND reportDate > DATEADD(dd,-14, getdate())

Open in new window

0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 250 total points
ID: 39891136
Hi Folks

I fear that the results will be something like
-1, -1, -1.

I suggest that if those really are columns names that you'll need to quote them, say with square brackets.

select
    [06-07]
    , [07-08]
    , [08-09]

etc.

Here is a pivot table that sort of does something similar
select 
	pvt.SystemDrive
	, pvt.Environment
	, pvt.BusinessDescription
	, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]
	, [10], [11], [12], [13], [14], [15], [16], [17], [18], [19]
	, [20], [21], [22], [23], [24], [25], [26], [27], [28], [29]
	, [30], [31], [32], [33], [34], [35], [36], [37], [38], [39]
	, [40], [41], [42], [43], [44], [45], [46], [47], [48], [49]
	, [50], [51], [52], [53], [54], [55], [56], [57], [58], [59]
	, [60], [61], [62], [63], [64], [65], [66], [67], [68], [69]
	, [70], [71], [72], [73], [74], [75], [76], [77], [78], [79]
	, [80], [81], [82], [83], [84], [85], [86], [87], [88], [89]
	, [90], [91], [92], [93], [94], [95], [96], [97], [98], [99]
	, [100], [101], [102], [103], [104], [105], [106], [107], [108], [109]
	, [110], [111], [112], [113], [114], [115], [116], [117], [118], [119]
	, [120], [121], [122], [123], [124], [125], [126], [127], [128], [129]
	, [130], [131], [132], [133], [134], [135], [136], [137], [138], [139]
	, [140], [141], [142], [143], [144], [145], [146], [147], [148], [149]
	, [150], [151], [152], [153], [154], [155], [156], [157], [158], [159]
	, [160], [161], [162], [163], [164], [165], [166], [167], [168], [169]
	, [170], [171], [172], [173], [174], [175], [176], [177], [178], [179]
	, [180], [181], [182], [183], [184], [185], [186], [187], [188], [189]
	, [190], [191], [192], [193], [194], [195], [196], [197], [198], [199]
from 
	(
	select 
		u.SystemName + ' ' + u.Caption as SystemDrive
		, ss.Environment
		, ss.BusinessDescription
		, datediff( day, u.UsageDT, getdate()) as DaysAgo
		, u.PercentUsed
	from dbo.DiskUsage u
	inner join ( 
		select
			ssi.MachineName
			, max( ssi.Environment ) as Environment
			, dbo.concatBusinessDescription( ssi.MachineName ) as BusinessDescription
		from dbo.SQLServer ssi
		group by
			ssi.MachineName
		) ss on ss.MachineName = u.SystemName
	where
		u.SystemName in (
			select ts.SystemName
			from #server ts
			)
	) s
pivot( 
	max( s.PercentUsed ) 
	for s.DaysAgo in ( [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]
		, [10], [11], [12], [13], [14], [15], [16], [17], [18], [19]
		, [20], [21], [22], [23], [24], [25], [26], [27], [28], [29]
		, [30], [31], [32], [33], [34], [35], [36], [37], [38], [39]
		, [40], [41], [42], [43], [44], [45], [46], [47], [48], [49]
		, [50], [51], [52], [53], [54], [55], [56], [57], [58], [59]
		, [60], [61], [62], [63], [64], [65], [66], [67], [68], [69]
		, [70], [71], [72], [73], [74], [75], [76], [77], [78], [79]
		, [80], [81], [82], [83], [84], [85], [86], [87], [88], [89]
		, [90], [91], [92], [93], [94], [95], [96], [97], [98], [99]
		, [100], [101], [102], [103], [104], [105], [106], [107], [108], [109]
		, [110], [111], [112], [113], [114], [115], [116], [117], [118], [119]
		, [120], [121], [122], [123], [124], [125], [126], [127], [128], [129]
		, [130], [131], [132], [133], [134], [135], [136], [137], [138], [139]
		, [140], [141], [142], [143], [144], [145], [146], [147], [148], [149]
		, [150], [151], [152], [153], [154], [155], [156], [157], [158], [159]
		, [160], [161], [162], [163], [164], [165], [166], [167], [168], [169]
		, [170], [171], [172], [173], [174], [175], [176], [177], [178], [179]
		, [180], [181], [182], [183], [184], [185], [186], [187], [188], [189]
		, [190], [191], [192], [193], [194], [195], [196], [197], [198], [199]
		 )
	) pvt
order by
	pvt.SystemDrive
;

Open in new window

Points of interest:
I'm calculating days ago. Today is 0 days ago, yesterday is 1 day ago, etc. That means that the column headings 0, 1 ...199 _always_ work for me, and I don't have to do any dynamic stuff to label the columns.

That is, next week, your column headings will all be different, and so you can't use this weeks query ...

I'm pre-selecting servers of interest into #t

There could be one or more rows in servers which have the same machine name/system name. That is, I'm allowing for multiple instances of SQL on servers.

HTH
  David
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

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