Question

Getting last month end date in a sybase query

Asked by: llccoo

how would you go about getting the last_month_end_date in a sybase query?
I know oracle has something like last_day that i use to use. I also wanted to kno if there was a way of doing something to get a query back between 2 dates. Lets say i did getdate()-1 and getdate()-3.
and getdate was dec... i would get sept oct and nov data.
But it should work also no matter what month. lets say i did this select next year feb ... i should get jan04 dec03 nov03

I have tried a few i keep getting data that isnt right ... hope somebody can help with something.

Thanks in Advance for the help.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-12-21 at 12:23:56ID20831839
Tags

sybase

,

month

,

date

,

last

Topic

Sybase Database

Participating Experts
3
Points
50
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. GETDATE() in DTS Package
    What is the syntax for using GETDATE() in a DTS package.Whenever I try and use it I get an unrecognised column error ?
  2. using a getdate in a query
    Is it possible, i am sure it is. that i can us a combination of a getdate and datename within a query to be able to achaive a where command? say that i have a query like this Select col1, col2, col3 from table1 Where col1 = datename(MONTH,(getDate())) I want to be able t...
  3. SYBA sil680 ata 133 controller card installation
    I am trying to add two new IDE channels to my desktop as my motherboard's channels are filled up. I am trying to attach a CD-ROM drive to this new card but the system does not boot after I power it on. It gives me the F3 prompt (like the one you get for BIOS config) when I tr...
  4. GetDate??
    I have a query in SQL server 2005 that I run every day. Can I change the values below to pull the date from the system rather than having to change them each day? set @rundate = '2006-04-21' -- Today set @startdate = '2006-04-20 12:00:00' --Yesterday 12 PM set @enddate ...
  5. getdate() in oracle
    what is the equivalent of getdate() from sql server in oracle\I am tryingto get the current date

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: namasi_navaretnamPosted on 2003-12-21 at 16:09:47ID: 9982632

Q1) Isn't last date 12/31/YYYY always?
Ex: select * from MyTable where MyDate <= '12/31/2003'

Q2) Use DateAdd function with appropriate datepart?

DATEADD ( datepart , number, date )

number can be negative

Ex: select (mm, -1, GetDate())

HTH

 

by: llccooPosted on 2003-12-21 at 19:21:29ID: 9983113

Thanks so much for replying so quickly!

A1) I didn't know Sybase had a last_day function? I know Oracle has one where i would do
Ex: select * from MyTable where MyDate <= last_day(add_months(sysdate, -1));
      --Would give me the last day of the previous month which is what i want to do the same in a sybase query.

A2) I never thought about using dateadd. Thanks! I will give this a try. Just a follow up question will this work also if I had to run this query next year feb. ... would i get jan04, dec03, nov03 ?

 

by: namasi_navaretnamPosted on 2003-12-21 at 19:45:31ID: 9983181

See this link for Sybase date functions:

http://sybooks.sybase.com/onlinebooks/group-as/asg1251e/sqlug/@Generic__BookView?DwebQuery=date+functions

Q1) You can do this

select * from MyTable
where Datepart(yy, MyDate)  =  DatePart(yy, DateAdd(mm, -1, getdate()) and
         Datepart(mm, MyDate)  <= DatePart(mm, DateAdd(mm, -1, getdate())

Q2) dateadd will work for next year too.

 

by: namasi_navaretnamPosted on 2003-12-22 at 05:03:00ID: 9984514

Would you need further help?

 

by: llccooPosted on 2003-12-22 at 05:11:01ID: 9984538

A1)Anyway of limiting the months return to just the last 3 months? Right now i get all of 2003, basically year to date. I'm more looking for last 3 months. Was thinking maybe adding a having clause in it. That is why i wanted to find out how to find a month end date.


 

by: namasi_navaretnamPosted on 2003-12-22 at 14:45:53ID: 9988090

Try this.

select * from MyTable
where Datepart(yy, MyDate)  =  DatePart(yy, DateAdd(mm, -1, getdate()) and
         Datepart(mm, MyDate)  <= DatePart(mm, DateAdd(mm, -1, getdate())  and
         Datepart(mm, MyDate)  >= DatePart(mm, DateAdd(mm, -4, getdate())  


HTH

 

by: ChrisKingPosted on 2003-12-23 at 02:10:08ID: 9990417

this will use the index on MyDate (if you have one) and will work across year boundaries

select * from MyTable
where MyDate >=  dateadd(mm, -2, dateadd(dd, 1 - datepart(dd,getdate()), getdate()))

ie when run on 23-Dec-2003, it will show all dates from 1-Oct-2003 onwards

OR

select * from MyTable
where MyDate >=  dateadd(mm, -3, getdate())

if you want 23-Sep-2003 onwards

 

by: ChrisKingPosted on 2003-12-23 at 02:13:00ID: 9990427

oops, to strip the time portion from the starting date, wrap a
          convert( varchar,  ...  , 106 ) around the value

eg
select * from MyTable
where MyDate >=  convert( varchar, dateadd(mm, -2, dateadd(dd, 1 - datepart(dd,getdate()), getdate())), 106 )

OR

select * from MyTable
where MyDate >=  convert( varchar, dateadd(mm, -3, getdate()), 106 )

 

by: namasi_navaretnamPosted on 2003-12-23 at 04:38:01ID: 9990766

This may not work in the beginning of the year.

select * from MyTable
where Datepart(yy, MyDate)  =  DatePart(yy, DateAdd(mm, -1, getdate()) and
         Datepart(mm, MyDate)  <= DatePart(mm, DateAdd(mm, -1, getdate())  and
         Datepart(mm, MyDate)  >= DatePart(mm, DateAdd(mm, -4, getdate())  

But try this

select * from MyTablle
Where datediff(m , MyDateColumn, GetDate()) <= 3

HTH

 

by: ChrisKingPosted on 2003-12-23 at 05:50:45ID: 9991046

namasi_navaretnam
that still won't use an index, it will table scan then entire table

 

by: namasi_navaretnamPosted on 2003-12-23 at 14:48:03ID: 9994175

Chris,

I thought any built in function used on column forced table scan in older versions of sybase , but not on version12. Is that not true?

 

by: ChrisKingPosted on 2003-12-23 at 15:08:52ID: 9994254

namasi_navaretnam
it will if the function is on the column, but in my example the functions are not on the column, they are on getdate().

so, code like:
select * from MyTable
where MyDate >=  convert( varchar, dateadd(mm, -2, dateadd(dd, 1 - datepart(dd,getdate()), getdate())), 106 )

can be fully rationised to:
select * from MyTable
where MyDate >=  '1-Oct-2003'

and this will be done before the optimiser jumps in, as you can see, this will use an index  :)   .... (if one exists on MyDate)

the effect is the same as:
declare @mydate datetime
select @mydate = convert( varchar, dateadd(mm, -2, dateadd(dd, 1 - datepart(dd,getdate()), getdate())), 106 )
select * from MyTable where MyDate >=  @mydate

 

by: namasi_navaretnamPosted on 2003-12-23 at 15:11:45ID: 9994263

Yep. You are right!!

 

by: llccooPosted on 2003-12-25 at 08:24:36ID: 10000165

Thanks for all your help guys. I actually started to look into this datediff() function as HTH had recommended. So far in testing it has given me the dates i look for, even if i ran it on feb next year and so on. In my code i'm getting the diff number return in negatives, but thats ok its just the same as if it was positive. so in my where clause i say return all where datediff between -3 and -1. I will keep you posted.

 

by: ChrisKingPosted on 2003-12-25 at 17:34:18ID: 10000848

HTH is not his name, it stands for "Hope that helps".

glad to see you are makeing some progress

 

by: llccooPosted on 2003-12-25 at 18:49:23ID: 10000914

Im so sorry. Should have read that better. I apologize. I meant to say namasi_navaretnam. =)

 

by: namasi_navaretnamPosted on 2003-12-26 at 14:01:20ID: 10003505

That is OK. No need to applogize. For datediff function first parameter is the datepart. Second parameter is the start date and third parameter is the end date. If Start Date is greater than End Date then you will get negative numbers.

Look into Chris's solution as well.

To measure performance try,

Select GetDate()
go
Select Statement
go
Select GetDate()

Measure the difference in time.

HTH

Namasi

 

by: sandhiyaaPosted on 2003-12-29 at 01:30:10ID: 10009716

hi,
try this logic:

declare @day,@mon,@yy
@day = datepart(dd,getdate())
@mon = datepart(mm,getdate())
@yy = datepart(yy,getdate())

string @temp_date
declare @date datetime
select @mon = @mon + 1
if @mon > 12 then
      select @mon = 1
      select @yy = @yy + 1
end if
select @day = 1
select @temp_date = String(@day) + "/" + String(@mon) + "/" + String(@yy)
select @date = convert(Datetime ,@temp_date)
select @date = datediff(dd,@date, -1)

Regards,
sandhiya

 

by: llccooPosted on 2003-12-29 at 09:29:19ID: 10011629

Hi,

I tried the following and it works, so far. Let me knwo if my logic is wrong??

  select  *    
  from temp_div
   where datediff(mm, convert(varchar(10), getdate(), 112), convert(varchar(10), ex_date, 112)) >= -3
     and datediff(mm, convert(varchar(10), getdate(), 112), convert(varchar(10), ex_date, 112)) <= -1
order by ex_date

Thanks.

 

by: ChrisKingPosted on 2003-12-29 at 13:14:07ID: 10012982

llccoo
only the fact that this query will not and can not use and index. Please read comments above.
The only time this would be acceptable in a production environment, would be if the table will NEVER have more than 100 rows in it

if the query is going to return < 50% of the rows in the table, then I would suggest that the index (on ex_date) is required.

Whether you add then index or not, I would still write the query so it could use the index if it was there.

 

by: namasi_navaretnamPosted on 2003-12-29 at 15:33:01ID: 10013588

llccoo,
ex_date is defined as a dattime field then you will not need to convert to varchar before using datediff function.

This can be

select  *    
  from temp_div
   where datediff(mm, convert(varchar(10), getdate(), 112), convert(varchar(10), ex_date, 112)) >= -3
     and datediff(mm, convert(varchar(10), getdate(), 112), convert(varchar(10), ex_date, 112)) <= -1
order by ex_date

written as

select  *    
  from temp_div
   where datediff(mm,  getdate(),  ex_date ) >= -3
     and datediff(mm,  getdate(),  ex_date ) <= -1
order by ex_date

Datediff function is there to be used. (I am not sure if using built-in functions still forces table scan in the latest version of Sybase)

Try Chris's suggestion as well. Then select the method that performs better.


HTH

Namasi Navaretnam

 

by: llccooPosted on 2003-12-29 at 18:07:59ID: 10014063

I actually tried both today.  I found out that my ex_date has an index ... so both queries worked well in running-time. taking the varchar part out of my query thru an error cuz the ex_date was define as smalldate. I have about 35k rows give or take. I'm still testing things out. But chris ill try your suggestion again and copy that table without an index and see what changes.

Thanks so much for helping me out. I would of never had known where to start.

LLccOO

 

by: ChrisKingPosted on 2003-12-29 at 18:29:37ID: 10014118

don't forget that after you run the query once, the data is cached and there will not be a huge difference in the times after that until that cache is cleared.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...