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!

Need first day of previous month for criteria in a SQL view.

Status
Not open for further replies.

ljevans

Technical User
Apr 4, 2001
53
US
When this view is run, all of the discrepancies written the previous month are displayed. I've got the last day of the previous month, but I can't get the first day. Any help would be appreciated.

A criteria example: Between 10/1/2006 and 10/31/2006

So far I have:

Between 10/1/2006 and DateAdd("d", -Day(NOW()), NOW())
 
This should get you in the right direction:

Code:
select convert(char(2),datepart(month, dateadd(m, -1,getdate()))) + '/01/' + convert(char(4),datepart(year, dateadd(m,-1,getdate())))

Hope it helps,

Alex

A wise man once said
"The only thing normal about database guys is their tables".
 
Is this a SQL Server database? The syntax you show appears to be for Access.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
If it is a SQL Server database, then this syntax should work.

Code:
Select DateAdd(Month, DateDiff(Month, 0, GetDate())-1, 0)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
It is a SQL server database, but the code that I used seems to work.
 
Uh... no. Now() is not a recognized function name.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
In that case, (because of integer math), the query would evaluate to...

[tt][blue]
Between 10/1/2006 and DateAdd("d", -Day(NOW()), NOW())

Between 0 and DateAdd("d", 0, 0)

Between 0 and 0
[/blue][/tt]

So... Only records for Jan 1, 1900 (without a time component) would be returned.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
But, I get the results I expect when I run the view.
 
ljevans,

Please do me a favor. Open Query Analyzer and run this...

Code:
Select @@Version

Then post the results back here.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Guys,

I've just tried creating a view with that code in, and the view creator replaces it with:

Code:
DATEADD(d, - DAY({ fn NOW() }), { fn NOW() })

Which runs just fine in query analyzer. I have absolutely no idea why though!


Geraint

The lights are on but nobody's home, my elevator doesn't go to the top. I'm not playing with a full deck, I've lost my marbles. Barenaked Ladies - Crazy
 
Yes, in going back, I am seeing the same thing.
 
Wow it is getting a little loopy in here today.

I think I will continue using getdate().

btw george, I really like your second post. It's much slicker than my clunker.

A wise man once said
"The only thing normal about database guys is their tables".
 
GETDATE
Returns the current system date and time in the Microsoft® SQL Server standard internal format for datetime values.

CURRENT_TIMESTAMP
Returns the current date and time. This function is the ANSI SQL equivalent to GETDATE.

Think about what Celko would tell you to use

Denis The SQL Menace
SQL blog:
Personal Blog:
 
[blush]

A wise man once said
"The only thing normal about database guys is their tables".
 
Yeah I may stick with it too. They both come in the same format for me. I'll keep it in mind next time I am using sybase or anything like that though.

A wise man once said
"The only thing normal about database guys is their tables".
 
I just talked to Celko this morning and he's as big an arrogant expletive as any rumors you may have heard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top