Solved

Date Question

Posted on 2007-11-26
5
232 Views
Last Modified: 2010-03-20
I have a query looking at yesterdays date when run, as shown in the query below.

I want to add last full month as a datechoice, i.e.
f @datechoice = 'Last Full Month'
begin

I need help settin the last full month for @stat and @start1, I will need an @end and @end1 too as well.

Any help would be appreciated

Thanks







declare @datechoice varchar(20)

declare @start varchar(20)
declare @start1 datetime

if @datechoice = 'Yesterday'
begin
set @start = CONVERT(varchar(20), DATEADD(dd, - 1, GETDATE()), 103)
set @start1 = CONVERT(char(10), GETDATE() - 1, 101)
end

SELECT    
FROM
WHERE     (CONVERT(varchar(10), field1 112)) = @start

UNION ALL

SELECT    
FROM        
WHERE      CONVERT(char(10), field1, 101) = @start1
0
Comment
Question by:halifaxman
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
set @start = CONVERT(varchar(20), DATEADD(month, - 1, GETDATE()), 103)
set @start1 = CONVERT(char(10), GETDATE() - 1, 101)
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
if month is not working , try mm or m
0
 
LVL 5

Expert Comment

by:ursangel
Comment Utility
Both MM or month will work with DATEADD function.
0
 

Author Comment

by:halifaxman
Comment Utility
Hi,

Thanks for the answers.

By Last Full Month, I meant that if I ran it today @start and @start1 would equal 1st October and @end and @end1 would equal 31st October, and if I ran it in December the dates would be in November and so on

Cheers
0
 
LVL 5

Accepted Solution

by:
ursangel earned 250 total points
Comment Utility
declare @TempStart datetime
set @TempStart = ltrim(datepart(month, GETDATE())) + '/01/' + ltrim(datepart(Year, GETDATE()))
set @start = CONVERT(varchar(20), dateadd(month, -1, @TempStart))
set @start1 = CONVERT(char(10), @TempStart - 1, 101)

similarly do it for @end
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

771 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

14 Experts available now in Live!

Get 1:1 Help Now