We help IT Professionals succeed at work.

SELECT by day, month etc

260 Views
Last Modified: 2010-03-20
How do I create a Select statement that will select all orders this month or all orders today or all orders last month etc.

I guess something like this:

WHERE        (EntryDate = DATEPART(month, GETDATE()))
Comment
Watch Question

Jai STech Arch

Commented:
WHERE        (DATEPART(month, EntryDate) = DATEPART(month, GETDATE()))
Jai STech Arch

Commented:
this month
WHERE        (DATEPART(month, EntryDate) = DATEPART(month, GETDATE()))
last month
WHERE        (DATEPART(month, EntryDate) = DATEPART(month, GETDATE()) -1)
today
WHERE        (DATEPART(day, EntryDate) = DATEPART(day, GETDATE()))
Sham HaqueSenior SAP CRM Consultant
CERTIFIED EXPERT

Commented:
all orders this month
WHERE  DATEPART(month, EntryDate) = DATEPART(month, GETDATE())

all orders today
WHERE CONVERT(varchar(10),EntryDate,101) = CONVERT(varchar(10),GETDATE(),101)

all orders last month
WHERE  DATEPART(month, EntryDate) = DATEPART(month, GETDATE())-1
Sham HaqueSenior SAP CRM Consultant
CERTIFIED EXPERT

Commented:
>>today
WHERE        (DATEPART(day, EntryDate) = DATEPART(day, GETDATE()))

i would not use this!! this will return all orders on the same day of month as current day

eg. today is 28th October 2007 - will SELECT recs with an EntryDate of 28th of any month and year....
Jai STech Arch
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Jai STech Arch

Commented:
the same applies for the months too gbshahaq
Sham HaqueSenior SAP CRM Consultant
CERTIFIED EXPERT

Commented:
good point jaiganesh...it's funny that when it comes to months, I forget that other years exist.... :-)
Database Consultant
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
@All,

In order to maximise the index usage, try avoinding converting the data as far as possible

Author

Commented:
Wow, thanks for all your double quick replies.

Can I just ask which is the more correct solution between aneeshattingal and jaiganeshsrinivasan?

They both seem to do the same thing although aneeshattingal 's is mor difficult to read.

Thanks.
Jai STech Arch

Commented:
aneesh is the master, his solution is much proper than mine .....if you think you can split the points tht will be great :-)
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
>They both seem to do the same thing although aneeshattingal 's is mor difficult to read.


eventhough it is difficult to read, performance will be higher, if you create an index on EntryDate

Author

Commented:
How do i get orders for yesterday using Aneeshattingal's method?
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
WHERE EntryDate >= CAST(CONVERT(VARCHAR(10),GETDATE()-1,120)  as SmallDatetime )  and EntryDate <  CAST(CONVERT(VARCHAR(10),GETDATE(),120)  as SmallDatetime )

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.