Solved

sql date +14 days

Posted on 2014-02-26
4
322 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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