Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date Question

Posted on 2007-11-26
5
Medium Priority
?
240 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
[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
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20349081
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
ID: 20349094
if month is not working , try mm or m
0
 
LVL 5

Expert Comment

by:ursangel
ID: 20349123
Both MM or month will work with DATEADD function.
0
 

Author Comment

by:halifaxman
ID: 20356599
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 1000 total points
ID: 20381154
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

597 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