Solved

sql date +14 days

Posted on 2014-02-26
4
336 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to reinstall SCVMM 2012 R2 - SQL errors. 5 36
disk usage reporting tools 27 51
SQL Add Parameter in Variable 4 21
SQL Syntax 6 38
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

740 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