Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 1st, 2007, 04:50 AM
Authorized User
 
Join Date: Sep 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Urgent Help with datetime

I am writing a code that will generate the last months data on every first day of a new month. The code will be run as a job. The code works very well. The code snippet is below.

select *
from emeka
where startdate >= Convert(varchar,dateadd(mm, -1,getdate()), 103)
AND startdate <= convert(datetime,dateadd(dd, -1, getdate()),103)

My problem is, how can i re-write code so that i will generate last months data regardless of the day of the new month. I have tried this code below but it is not working. I keep geting an error that:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


Declare @month int
Declare @day int
Declare @startdate datetime
select @startdate = (select convert(varchar,startdate,103) from emeka)

select @startdate
select @month = datediff(month, @startdate, getdate())
select @day = datediff(day, @startdate, getdate())
--select @month
--select @day

select *
from emeka
where startdate >= Convert(varchar,dateadd(mm, -@month,getdate()), 103)
AND startdate <= convert(datetime,dateadd(dd, -@day, getdate()),103)

 
Old March 1st, 2007, 01:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

Try this, this should be what you are looking.
Code:
DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = Convert(DateTime, Cast(Year(DateAdd(m, -1, GetDate())) as Varchar(4)) + '-' + Cast(Month(DateAdd(m, -1, GetDate())) as Varchar(2)) + '-01')
SET @EndDate = DateAdd(m, 1, @StartDate)
SET @EndDate = DateAdd(ms, -3, @EndDate)
Select @StartDate, @EndDate
Cheers.

_________________________
- Vijay G
Strive for Perfection
 
Old March 1st, 2007, 04:39 PM
Authorized User
 
Join Date: Sep 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Vijay. The code works perfectly well.






Similar Threads
Thread Thread Starter Forum Replies Last Post
datetime MathLearner Beginning VB 6 0 November 15th, 2007 07:12 AM
DateTime pzmrcd C# 3 July 26th, 2007 06:35 AM
DateTime RickP SQL Server 2000 7 December 14th, 2005 07:08 PM
UTC DateTime to Local DateTime r_ganesh76 SQL Server 2000 1 April 4th, 2005 08:21 AM
DateTime DARSIN General .NET 1 December 1st, 2004 06:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.