sql date +14 days

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
websssCEOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
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
lcohanDatabase AnalystCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZberteocCommented:
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
David ToddSenior DBACommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.