SQL help - Need to know the first date of previous month and last date of previous month...

  • mw112009 - Tuesday, April 18, 2017 9:57 AM

    Thom: My job is not to write SQL. When I need to extract data I need help. I can write some SQL  ( In fact if I were to write that by myself I would have written a scalar function.. That's just my way of thinking ) But when you ask a group of people, they will present a mix of answers ( that is good ) and I get to pick the best one. So I like to ask. I hope that is not wrong. and in this case someone was nice enough to help me with one single expression that gives me what I wanted.

    If you think a user is abusing.. then simply do not reply (  Or just say go search google or learn from this or that  book, or point me to a resource  )
    I am sure  there are many people who just love to throw the best answer they think would be best to use. So one person did that! Great! ) 
    BTW .. no hard feelings

    So, what is your job?  If you just need to extract data from time to time, isn't there someone there that can help, like a DBA?  I mean, you have to understand the structure of the database and the data that it holds or have access to someone that does, because we can't help you with that aspect of what you do.

  • Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

  • I have been in positions where I too had to wear many hats. Something valuable I learned during those stints was to slow down. Many things come at you all at once. Best thing to do is not react quickly. Slow things down a bit and be deliberate. That gives one the opportunity to learn and internalize what has been learned to accomplish the task. I am not saying move at a snails pace, but reacting hastily to the 15 tasks that dropped on the desk at the same time is not a recipe for success.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • mw112009 - Tuesday, April 18, 2017 10:20 AM

    Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

    So what is your job?

  • mw112009 - Tuesday, April 18, 2017 9:19 AM

    To All ( Bran,Luis,Jeff,Phil and anyone who comments ... ) 
    You guys are a great help. Sorry I am not able to pay you.

    Hey,  Helping a needy person is a act of Good Will.  You may not get paid by me but you will receive a bigger return ( Count on that ).
    Why do I say this ? I have complete trust in the Bible.
    Also one more point... When helping others do it with love ( Never complain ...only then will you receive a return for your favor )
    That is how real "Faith" works  ( Galatians 5:6 ,  The only thing that counts is faith expressing itself through love. ) .

    Quoting the Bible to justify living off of other people's work is not impressive, especially considering the entitlement that comes with assuming that everyone on this site practices the same religion. Don't go religious on this site. Just don't. Third rail, dude.

    FYI: If you are the only one doing this data-extract job because everyone else is so busy, then it absolutely is your job to learn T-SQL.

    I don't mind helping out people who don't understand things, but (as many of us here feel) I do expect the people who come here to do a bare minimum of effort and not steal code so they can coast on this part of the job and do "other things." Your initial comment that I responded to came across as flippant and disrespectful of the effort that many people on this site put into learning how to do this on their own. It also put you near the top of the list that people are going to start ignoring unless you prove that you care enough about your job to actually... I dunno ... do it instead of relying on volunteers don't know enough about your environment to consistently give you workable solutions.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Lynn Pettis - Tuesday, April 18, 2017 10:52 AM

    mw112009 - Tuesday, April 18, 2017 10:20 AM

    Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

    So what is your job?

    C# and Crystal Report Developer + Claims Analyst + Business Analyst + What ever they assign me to do

  • mw112009 - Tuesday, April 18, 2017 11:06 AM

    Lynn Pettis - Tuesday, April 18, 2017 10:52 AM

    mw112009 - Tuesday, April 18, 2017 10:20 AM

    Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

    So what is your job?

    C# and Crystal Report Developer + Claims Analyst + Business Analyst + What ever they assign me to do

    Okay, as a Crystal Report Developer you need to know how to extract data from a SQL database.  As your reports are most likely NOT one off reports you also need to know how to do this in a proficient and scalable manner.  All this means you need to learn how to write T-SQL well.

    My suggestion, get SQL Server 2016 Developer Edition (it is actually free), install it on your personal computer at home and start learning.  yes, you need to spend some of your own off-work time to learn if you expect to get good at what you need to do for work.  This is also true for the C# work you do.  Don't rely on your company to pay for the training, you need to do what is needed to be done to stay proficient and relevant in today's technical work environment.

  • Lynn Pettis - Tuesday, April 18, 2017 11:14 AM

    mw112009 - Tuesday, April 18, 2017 11:06 AM

    Lynn Pettis - Tuesday, April 18, 2017 10:52 AM

    mw112009 - Tuesday, April 18, 2017 10:20 AM

    Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

    So what is your job?

    C# and Crystal Report Developer + Claims Analyst + Business Analyst + What ever they assign me to do

    Okay, as a Crystal Report Developer you need to know how to extract data from a SQL database.  As your reports are most likely NOT one off reports you also need to know how to do this in a proficient and scalable manner.  All this means you need to learn how to write T-SQL well.

    My suggestion, get SQL Server 2016 Developer Edition (it is actually free), install it on your personal computer at home and start learning.  yes, you need to spend some of your own off-work time to learn if you expect to get good at what you need to do for work.  This is also true for the C# work you do.  Don't rely on your company to pay for the training, you need to do what is needed to be done to stay proficient and relevant in today's technical work environment.

    Wrong !  The crystal report that I am developing directly connects with C# code, gets data off of a DataSet structure ( similar to a table ) . The part where C# interacts with SQL server was not done by me ( Was already done when I arrived ).
    The crystal report that I develop is not for front end use ( C# converts it to a PDF file ).  Anyhow, I agree I have to learn T-SQL ( no doubt about that ) , I will do the learning at my pace. Hey but you missed the point. I told you earlier I could have written a SCALAR function to get the 2 dates that I wanted ( Lot more code  .. but it works ).  By asking I was able to get a very short expression  ( Now, for someone to write an expression like that ( what Des wrote ) you need to be experienced.  Thanks to Des

  • mw112009 - Tuesday, April 18, 2017 11:42 AM

    Lynn Pettis - Tuesday, April 18, 2017 11:14 AM

    mw112009 - Tuesday, April 18, 2017 11:06 AM

    Lynn Pettis - Tuesday, April 18, 2017 10:52 AM

    mw112009 - Tuesday, April 18, 2017 10:20 AM

    Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

    So what is your job?

    C# and Crystal Report Developer + Claims Analyst + Business Analyst + What ever they assign me to do

    Okay, as a Crystal Report Developer you need to know how to extract data from a SQL database.  As your reports are most likely NOT one off reports you also need to know how to do this in a proficient and scalable manner.  All this means you need to learn how to write T-SQL well.

    My suggestion, get SQL Server 2016 Developer Edition (it is actually free), install it on your personal computer at home and start learning.  yes, you need to spend some of your own off-work time to learn if you expect to get good at what you need to do for work.  This is also true for the C# work you do.  Don't rely on your company to pay for the training, you need to do what is needed to be done to stay proficient and relevant in today's technical work environment.

    Wrong !  The crystal report that I am developing directly connects with C# code, gets data off of a DataSet structure ( similar to a table ) . The part where C# interacts with SQL server was not done by me ( Was already done when I arrived ).
    The crystal report that I develop is not for front end use ( C# converts it to a PDF file ).  Anyhow, I agree I have to learn T-SQL ( no doubt about that ) , I will do the learning at my pace. Hey but you missed the point. I told you earlier I could have written a SCALAR function to get the 2 dates that I wanted ( Lot more code  .. but it works ).  By asking I was able to get a very short expression  ( Now, for someone to write an expression like that ( what Des wrote ) you need to be experienced.  Thanks to Des

    Actually, I didn't miss the point.  I think you missed the point that we all have been trying to make.  You need to learn instead of relying on the internet to solve all your problems.  Do you know why or how what Des provided even works?  What happens down the road when it suddenly stops working, could you dig in and find out what may be wrong?
    We are here to help you learn and become better, and possibly join us in helping others as you gain more experience.

  • mw112009 - Tuesday, April 18, 2017 11:42 AM

    Lynn Pettis - Tuesday, April 18, 2017 11:14 AM

    mw112009 - Tuesday, April 18, 2017 11:06 AM

    Lynn Pettis - Tuesday, April 18, 2017 10:52 AM

    mw112009 - Tuesday, April 18, 2017 10:20 AM

    Lynn
    I wish that was the case. I wear many hats and work for a company with very limited resources ( and their plates are all full.. no bandwith ).
    Your help is very valuable.
    I am not trying to challenge you guys here.

    So what is your job?

    C# and Crystal Report Developer + Claims Analyst + Business Analyst + What ever they assign me to do

    Okay, as a Crystal Report Developer you need to know how to extract data from a SQL database.  As your reports are most likely NOT one off reports you also need to know how to do this in a proficient and scalable manner.  All this means you need to learn how to write T-SQL well.

    My suggestion, get SQL Server 2016 Developer Edition (it is actually free), install it on your personal computer at home and start learning.  yes, you need to spend some of your own off-work time to learn if you expect to get good at what you need to do for work.  This is also true for the C# work you do.  Don't rely on your company to pay for the training, you need to do what is needed to be done to stay proficient and relevant in today's technical work environment.

    Wrong !  The crystal report that I am developing directly connects with C# code, gets data off of a DataSet structure ( similar to a table ) . The part where C# interacts with SQL server was not done by me ( Was already done when I arrived ).
    The crystal report that I develop is not for front end use ( C# converts it to a PDF file ).  Anyhow, I agree I have to learn T-SQL ( no doubt about that ) , I will do the learning at my pace. Hey but you missed the point. I told you earlier I could have written a SCALAR function to get the 2 dates that I wanted ( Lot more code  .. but it works ).  By asking I was able to get a very short expression  ( Now, for someone to write an expression like that ( what Des wrote ) you need to be experienced.  Thanks to Des

    I guess the problem may be HOW you asked the question.  
    Had you said "I need to format a date like this..." and followed with "here is what I tried..." and then said "is there a better way?", you may not be getting so much flack.

    And, as for sympathy because you are so busy and wear lots of hats, guess what.  We are all in the same boat. 
    You really do not want to go there.

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • mw112009 - Tuesday, April 18, 2017 11:42 AM

    Wrong !  The crystal report that I am developing directly connects with C# code, gets data off of a DataSet structure ( similar to a table ) . The part where C# interacts with SQL server was not done by me ( Was already done when I arrived ).
    The crystal report that I develop is not for front end use ( C# converts it to a PDF file ).  Anyhow, I agree I have to learn T-SQL ( no doubt about that ) , I will do the learning at my pace. Hey but you missed the point. I told you earlier I could have written a SCALAR function to get the 2 dates that I wanted ( Lot more code  .. but it works ).  By asking I was able to get a very short expression  ( Now, for someone to write an expression like that ( what Des wrote ) you need to be experienced.  Thanks to Des

    Aren't you supposed to be experienced? You've been a member of this site for seven years. Some people giving advice have less time working with SQL Server than that.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares - Tuesday, April 18, 2017 12:33 PM

    mw112009 - Tuesday, April 18, 2017 11:42 AM

    Wrong !  The crystal report that I am developing directly connects with C# code, gets data off of a DataSet structure ( similar to a table ) . The part where C# interacts with SQL server was not done by me ( Was already done when I arrived ).
    The crystal report that I develop is not for front end use ( C# converts it to a PDF file ).  Anyhow, I agree I have to learn T-SQL ( no doubt about that ) , I will do the learning at my pace. Hey but you missed the point. I told you earlier I could have written a SCALAR function to get the 2 dates that I wanted ( Lot more code  .. but it works ).  By asking I was able to get a very short expression  ( Now, for someone to write an expression like that ( what Des wrote ) you need to be experienced.  Thanks to Des

    Aren't you supposed to be experienced? You've been a member of this site for seven years. Some people giving advice have less time working with SQL Server than that.

    Yeah but my relationship with SQL sever has been on and off..I mean I get dragged into various projects that require other work.   So I learn then I forget, then I have to ask you...  Some employers only give read-only access to the SQL sever.. So I just have limited access...

  • This thread has gone quite a ways sideway, imo.

    Let's try to get this thing turned around.

    mw112009 has said s/he is going to devote more effort to learning.

    We have a recommendation to rephrase questions somewhat which may or may not help with the intended message and reception of that message.

    mw112009 has a solution to the topic.

    The rest of us will continue to volunteer and try to teach people to fish.

    Let's not beat on this thing any further folks.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • SQLRNNR - Tuesday, April 18, 2017 1:14 PM

    This thread has gone quite a ways sideway, imo.

    Let's try to get this thing turned around.

    mw112009 has said s/he is going to devote more effort to learning.

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    DesNorton - Monday, April 17, 2017 3:19 PM

    This will get the start and end dates of the prev month, based on today's date

    SELECT
    FirstOfPrevMonth = CONVERT(VARCHAR(8), DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) -1, 0), 112) ,
    LastOfPrevMonth = CONVERT(VARCHAR(8), DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0) -1, 112)

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    SQLRNNR - Tuesday, April 18, 2017 1:14 PM

    This thread has gone quite a ways sideway, imo.

    Let's try to get this thing turned around.

    mw112009 has said s/he is going to devote more effort to learning.

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    DesNorton - Monday, April 17, 2017 3:19 PM

    This will get the start and end dates of the prev month, based on today's date

    SELECT
    FirstOfPrevMonth = CONVERT(VARCHAR(8), DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) -1, 0), 112) ,
    LastOfPrevMonth = CONVERT(VARCHAR(8), DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0) -1, 112)

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

Viewing 15 posts - 16 through 30 (of 44 total)

You must be logged in to reply to this topic. Login to reply