473,758 Members | 4,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code for 1st day & the Last day of the Previous Month


Hi there,

See if you can help me with the following:

I need to write an SQL code that will return me:

The 1st day & the Last day of the Previous Month in the following format
(smalldatetime) :

yyyy-mm-dd hh:mi:ss (24h)

Regards,
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums...neral/200507/1
Jul 23 '05 #1
5 5761
Stu
If you don't have a calendar table (which can be very helpful for
situations like this), you can do it on-the-fly using SQL Server like
so:
SELECT DATEADD(m, -1, FirstOfCurrentM onth) as FirstOfPrevious Month,
DATEADD(d, -1, FirstOfCurrentM onth) as LastOfPreviousM onth
FROM (SELECT CONVERT(smallda tetime,DATENAME (m, GetDate())
+ ' 1,' + DATENAME(yyyy, GETDATE())) as FirstOfCurrentM onth) a

Of course, you'll want to do the formatting on the client side.

HTH,
Stu

Jul 23 '05 #2
On Wed, 20 Jul 2005 16:56:42 GMT, Ray via SQLMonster.com wrote:
Hi there,

See if you can help me with the following:

I need to write an SQL code that will return me:

The 1st day & the Last day of the Previous Month in the following format
(smalldatetime) :

yyyy-mm-dd hh:mi:ss (24h)

Regards,


declare @d1 smalldatetime
declare @d2 smalldatetime
declare @d3 smalldatetime

-- @d1 is the input date
set @d1 = CURRENT_TIMESTA MP
-- truncate hours, min, etc.
set @d1 = convert(smallda tetime, floor(convert(f loat, @d1)))

-- @d2 - last day of previous month

set @d2 = dateadd(day, - datepart(day, @d1), @d1)

-- @d3 - first day of previous month

set @d3 = dateadd(day, - datepart(day, @d2) + 1, @d2)
Jul 23 '05 #3

Hi Stu,

Thank you very very much ..... it was a great help.

Best regards,

Stu wrote:
If you don't have a calendar table (which can be very helpful for
situations like this), you can do it on-the-fly using SQL Server like
so:

SELECT DATEADD(m, -1, FirstOfCurrentM onth) as FirstOfPrevious Month,
DATEADD(d, -1, FirstOfCurrentM onth) as LastOfPreviousM onth
FROM (SELECT CONVERT(smallda tetime,DATENAME (m, GetDate())
+ ' 1,' + DATENAME(yyyy, GETDATE())) as FirstOfCurrentM onth) a

Of course, you'll want to do the formatting on the client side.

HTH,
Stu

--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums...neral/200507/1
Jul 23 '05 #4
Build a calendar table with all the temporal information you need
instead of trying to compute it on the fly. Next, the display of
temporal data has nothing to do how it is stored.

Jul 23 '05 #5

Ross,

Thank you so much ....

Ross Presser wrote:
Hi there,

[quoted text clipped - 8 lines]

Regards,


declare @d1 smalldatetime
declare @d2 smalldatetime
declare @d3 smalldatetime

-- @d1 is the input date
set @d1 = CURRENT_TIMESTA MP
-- truncate hours, min, etc.
set @d1 = convert(smallda tetime, floor(convert(f loat, @d1)))

-- @d2 - last day of previous month

set @d2 = dateadd(day, - datepart(day, @d1), @d1)

-- @d3 - first day of previous month

set @d3 = dateadd(day, - datepart(day, @d2) + 1, @d2)

--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums...neral/200507/1
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1553
by: Keith | last post by:
I have downloaded some sample code from the net to display a calendar etc. The code is below. How can I modify this code so that when called in a popup window and a date is clicked, that date is inserted into a form field on the original page. I know how to pass this value back but do not know how to modify this code to include the correct line. **** CODE FOLLOWS****
3
9720
by: Killer | last post by:
How can i calculate the last day of the previous month? Help me,please
13
3616
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not sure if it can be done, but all help welcomed. E.g. I have December and would like to see the last 2 weeks.. So this doesnt mean the last 15 days. What i mean by this is...
2
485
by: Burghew | last post by:
Thanks for the previous replies..... What I need is to generate invoices for customers whom we rent out equipment. I will be choosing the date and year say for December 2005 print the invoices for customer A or B or .../ for all the equipments they have with them. This Dec 2005 must be selected by the user through a combo box. I have no idea on how to keep the combo populated with Months + years so that users can choose the corresponding...
0
1904
by: larry | last post by:
I am in the process of rewriting one of my first PHP scripts, an event calendar, and wanted to share the code that is the core of the new calendar. My current/previous calendar processed data dates only, this code is intended to use more thrifty event descriptions (3rd saturday, last tuesday, etc.) as well as traditional one-of dates. The core here here is quite spartan, no table logic or or db cruft included (I figure you have our own...
2
4921
by: Kajsa Anka | last post by:
Before I re-invent something I would like to ask if there exists some code that can be used for create the HTML code for a calendar which I then can include on a web page. The module in the standard library can create a calendar but I would like display my schedule. My idea is that it would read a simple text file and then create a weekly calendar view (and/or a monthly) with my schedule.
0
6744
by: Vijay | last post by:
Prep Courses for International Certifications, CSTE & CSQA & ISEB & ISTQB &Business Analyst & SOA Certifications in HYDERABAD. After receiving overwhelming response to our last 50+ batches, SPECTRAMIND SOLUTIONS now announces a new batch of Prep Courses for CSQA & CSTE& ISEB & ISTQB & Business Analyst & SOA so as to prepare you thoroughly for the most prestigious certification exams conducted by International organizations. We...
9
4654
by: brymcguire | last post by:
Hi, I have a requirement to design a query that identifies items sold between two dates. There is a 'SoldDate' datetime field used to register what date the item was sold. The query needs to identify all sales between the last day of the previous month and going back one year.
1
2316
by: guyborn | last post by:
I have been trying to get data from the database from the of the previous month to the second last week of the current month.I only managed to get data from the previous month to today's date. your help will be highly appreciated Regards Guyborn
0
9492
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9299
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8744
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7287
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.