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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date definition

Status
Not open for further replies.

jabmod

MIS
Sep 12, 2004
68
US
What is the SQL code to define variables
@Begindt to generate 12/01/2004
@Enddt to generate 01/01/2005

Thanks.
 
Code:
declare @Begindt datetime
set @Begindt = '12/01/2004'
select @Begindt

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Code:
set dateformat MDY
declare @Begindt datetime
set @Begindt = '12/01/2004'
select @Begindt

--if 12/01/2004 is 12th Jan 2004 otherwise set dateformat MDY

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Thanks but I do not want to hard code the dates. The purpose is to pick up past month data at the beginning of each new month. Thanks you.

 
Here's what I run. I got this from this site sometime ago:
Code:
CREATE PROCEDURE usp_monthlyreport AS
SET NOCOUNT ON
/* set variables for the beginning and end of month */

DECLARE @start_dt SMALLDATETIME
        , @end_dt SMALLDATETIME

IF MONTH(GETDATE()) = 1
  BEGIN
    SELECT
      @start_dt = '12/1/' + CONVERT(CHAR(4), YEAR(GETDATE()) - 1)
  END
ELSE 
  BEGIN
    SELECT
      @start_dt = CONVERT(CHAR(2), MONTH(GETDATE()) - 1) + '/1/' + CONVERT(CHAR(4), YEAR(GETDATE()))
  END

SELECT
  @end_dt = DATEADD(MONTH, 1, @start_dt)

-SQLBill

Posting advice: FAQ481-4875
 
Code:
[Blue]SELECT[/Blue] [Fuchsia]DateAdd[/Fuchsia][Gray]([/Gray]mm[Gray],[/Gray][Fuchsia]DateDiff[/Fuchsia][Gray]([/Gray]mm[Gray],[/Gray]0[Gray],[/Gray][Fuchsia]GetDate[/Fuchsia][Gray]([/Gray][Gray])[/Gray][Gray])[/Gray][Gray]-[/Gray]1[Gray],[/Gray]0[Gray])[/Gray][Gray],[/Gray]
       [Fuchsia]DateAdd[/Fuchsia][Gray]([/Gray]mm[Gray],[/Gray][Fuchsia]DateDiff[/Fuchsia][Gray]([/Gray]mm[Gray],[/Gray]0[Gray],[/Gray][Fuchsia]GetDate[/Fuchsia][Gray]([/Gray][Gray])[/Gray][Gray])[/Gray][Gray],[/Gray]0[Gray])[/Gray]
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Code:
declare @Begindt datetime, @Enddt datetime
set @Begindt = (select dateadd(month,-1,
(dateadd(day, -(datepart(day,getdate()-1)),
 (dateadd(day,datediff(day,0,getdate()),0))))))
set @Enddt = (select dateadd(day,-1,
(dateadd(day, -(datepart(day,getdate()-1)),
 (dateadd(day,datediff(day,0,getdate()),0)))))+1)
select @Begindt
select @Enddt

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
DBomrrsm, the Select within the Set is superfluous.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top