Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select records within 12 months of each other

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
Hello,

I am trying to select all the B types within 12 months of each other.

Here is the sql to create the table. I need to select all type B s that are within 12 months. In this case, 5,4, and 3.

How do I do this?

Code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_ads](
	[id] [int] NULL,
	[dateStamp] [date] NULL,
	[type] [char](1) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[tbl_ads] ([id], [dateStamp], [type]) VALUES (1, CAST(0x61370B00 AS Date), N'b')
INSERT [dbo].[tbl_ads] ([id], [dateStamp], [type]) VALUES (2, CAST(0x01380B00 AS Date), N'a')
INSERT [dbo].[tbl_ads] ([id], [dateStamp], [type]) VALUES (3, CAST(0x52380B00 AS Date), N'b')
INSERT [dbo].[tbl_ads] ([id], [dateStamp], [type]) VALUES (4, CAST(0x73380B00 AS Date), N'b')
INSERT [dbo].[tbl_ads] ([id], [dateStamp], [type]) VALUES (5, CAST(0xDA380B00 AS Date), N'b')

ColdFusion Ninja for hire.
 
 http://files.engineering.com/getfile.aspx?folder=5a819c2d-e58c-4ec9-8c77-01e85fb85c1f&file=tbl.JPG
SOMETHING LIKE



SQL:
declare @tbl_ads as table(
	[id] [int] NULL,
	[dateStamp] [date] NULL,
	[type] [char](1) NULL
) 

INSERT @tbl_ads ([id], [dateStamp], [type]) VALUES (1, CAST(0x61370B00 AS Date), N'b')
INSERT @tbl_ads ([id], [dateStamp], [type]) VALUES (2, CAST(0x01380B00 AS Date), N'a')
INSERT @tbl_ads ([id], [dateStamp], [type]) VALUES (3, CAST(0x52380B00 AS Date), N'b')
INSERT @tbl_ads ([id], [dateStamp], [type]) VALUES (4, CAST(0x73380B00 AS Date), N'b')
INSERT @tbl_ads ([id], [dateStamp], [type]) VALUES (5, CAST(0xDA380B00 AS Date), N'b')

select *
from @tbl_ads 
where DATEDIFF(d, dateStamp ,getdate())< 365
AND [TYPE]='b'
 
Why not 1?
It is in 12 months range from 3 and 4.

Borislav Borissov
VFP9 SP2, SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top