Solved

sql date +14 days

Posted on 2014-02-26
4
354 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
[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 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 40

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 27

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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
Viewers will learn how the fundamental information of how to create a table.

626 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