Need to print one month before date - sql

Wanted to print the one month before the actual Date
If I mentioned FromPeriod=01.10.2023 then it should print the FromPeriod date=01.09.2023
and this FromPeriod column was already mentioned on other table and I leftjoin that table
DECLARE #yourDate DATE = '01-OCT-2021'
----select DATEADD(MONTH, DATEDIFF(MONTH, -1, #yourDate)-1, -1)
--SELECT DATEADD(m, DATEDIFF(m, 0, #yourDate), -1)
SELECT EOMONTH(#yourDate, -1)
I tried this and from above query i got the last date of september month
but i wanted to print 01-Sept-2021

how about
SELECT DATEADD(DAY,1,EOMONTH(#yourDate,-2))
This returns you the date in your default format. If you need it to be in the same format as you entered:
SELECT UPPER(FORMAT(DATEADD(DAY,1,EOMONTH(#yourDate,-2)),'dd-MMM-yyyy'))
but this will now be a string and not a date.

Related

date format : get the last and the first day in the month-sql-DB2

how i can write the following date with date function.. IN DB2
L2.FOM >= '2015-08-01' // the first day of the current Month
L2.TOM <= '2015-08-31' // the last day of the current Month
i tried this but it doesnot work
L2.FOM=DATEADD(month,DATEDIFF(month,0,CURRENT DATE), 0) //for the first day of the month
last date
DECLARE #date DATETIME = '12/1/2011';
SELECT EOMONTH ( #date ) AS Result;
GO
first date - is known to start from 1.
but here it is for name sake.
SELECT DATEADD(month, DATEDIFF(month, 0, #date), 0) AS StartOfMonth
Your code looks like SQL Server.
In that database, I would suggest using eomonth():
select eomonth(getdate()) as last_date_of_month,
dateadd(day, 1, eomonth(getdate(), -1)) as first_date_of_month
For the first date of the month, I also often use:
datefromparts(year(getdate()), month(getdate()), 1)

Storing the previous month as a char with a leading 0 for single digit months

I have a field filled with dates, but they are stored as varchars. They are in the following format:
01312013
01302013
09232007
I am trying to write a query that only returns records that meet the following three criteria:
The last four characters of the aforementioned varchar "date" match the year of the previous month
The first two characters of the aforementioned varchar "date" match the previous month.
The value in an entirely different date field (that is actually stored as a date) falls within the previous month
My attempts have left me trying to store the year of the previous month and the previous month in two variables, and then perform a LEFT() and RIGHT() lookup on the varchar "date" to see if they match those two variables. Whew.
The problem is that the MONTH is getting stored as a single digit, and I need to store it as a "0" + the month digit (in the case of single digit months only). Also it's not even working for just the year so maybe this is completely off. Really hope someone can help!
Here's what I have so far:
DECLARE #monthAgo dateTime
DECLARE #MonthString char
DECLARE #YearString char
SET #monthAgo = DATEADD(month, -1, GETDATE())
SET #MonthString = MONTH(#monthAgo) -- not working
SET #YearString = YEAR(#monthAgo) -- not working
SELECT
COUNT(*)
FROM
jwemaildb.LogFileRecords
WHERE
date > = DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0) AND
date < DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) AND
RIGHT(eAlertSentDate, 4) = #YearString AND
LEFT (eAlertSentDate, 2) = #MonthString
Actually, you need convert varchar "date" into ISO standard
SELECT COUNT(*)
FROM jwemaildb.LogFileRecords
WHERE
date > = DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0) AND
date < DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) AND
CAST(RIGHT(eAlertSentDate, 4) + LEFT(eAlertSentDate, 4) AS date) BETWEEN
DATEADD(dd , 1 - DAY(GETDATE()), DATEADD(mm, -1 ,GETDATE())) AND
DATEADD(dd , 1 - DAY(GETDATE()), GETDATE())
Simple demo on SQLFiddle
Fortunately you can cast those strings as dates, unless you have some strange values, such as 'oh dear'. Then it's a simple dateadd().
Just to make sure it works:
declare #dateString as char(8);
set #dateString = '02132013';
select cast(left(#dateString, 2) + '/' +
substring(#dateString, 3,2) +'/' +
right(#dateString, 4) as date) theDate;
returns
theDate
2013-02-13

Specific day of current month and year

I have problem with return of specific day of current month and year. I need for example 15th day. Until now I used in FB/IB existing function:
IB_EncodeDate(EXTRACT(YEAR FROM Current_Date),EXTRACT(Month FROM Current_Date),15)
Does it exist a simply way to convert this for MSSQL database?
edit. I need output in OLE format (41,348 by example) to compare date with another date. I compare date from database with 15th day of current month.
For the 15th day of current month:
SELECT DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0));
To get the silly OLE representation based on this "magic" date, 1899-12-30:
SELECT DATEDIFF(DAY, -2, DATEADD(DAY, 14,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)));
Answer (on March 11th, when I updated this answer for the changed requirement):
-----
41348
So, you have a date, and want to return the 15th day of the same month?. Well, assuming SQL Server 2008, you could do this:
SELECT CONVERT(DATE,CONVERT(VARCHAR(6),GETDATE(),112)+'15',112)
For Previous versions of SQL Server:
SELECT CONVERT(DATETIME,CONVERT(VARCHAR(6),GETDATE(),112)+'15',112)
This seems like a quick answer.
declare #OLEDate int
declare #currentDate as datetime
set #currentDate = DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
set #OLEDate = convert(float, #currentdate)
-- PRINT #OLEDate
based on Aaron Bertrand's answer and your need for the integer conversion
To get 10th day of current day
declare #cur_month int,#cur_yr int,#tenth_dt date
set #cur_month=month(getdate())
set #cur_yr=YEAR(getdate())
set #tenth_dt=convert(date,'10/'+convert(varchar(5),#cur_month)+'/'+convert(varchar(5),#cur_yr),103)
select #tenth_dt
Not sure if you after Day or Date. This gives both dayOfWeek and specificDate for any culture
declare #myDay int = 15
select convert(date,myday) specificDate, datename(dw,myday) dayOfWeek
from (
select convert(varchar(6),getdate(),112) + convert(varchar, #myDay) myday
) x
Fiddle Demo Here
| SPECIFICDATE | DAYOFWEEK |
----------------------------
| 2013-02-15 | Friday |
Current_Date in SQL Server would be getdate().
To get the 15th day in OLE Automation format, try:
select datediff(day, '18991230', dateadd(day, -day(getdate()) + 15, getdate()))
A bit more straightforward approach:
CAST(FORMAT(GETDATE(), 'yyyy-MM-15') AS DateTime)

Select date range from two tables from present day to first day of that month

Using Microsoft SQL 2008. I have two tables that I want to select from the current date to the first day of that month (or within the same month). Let's say today's date is 05/09/2012 and the date column is 'datecolumn'. Fom the tables below I should only get rowsets 6,7 from table1 and rowsets 9,2 from table 2 because those dates are within the same month as 05/09/2012.
table1
4 02/01/2012
5 01/02/2011
6 05/01/2012
7 05/20/2012
table2
8 02/01/2012
9 05/14/2012
3 01/02/2011
2 05/18/2012
I tried this but it didnt work:
DECLARE #daterange
SET #daterange = (DATEPART(MONTH,GETDATE()) + '/' + DATEPART(YEAR,GETDATE()))
SELECT blah from table where (DATEPART(MONTH,datecolumn) + '/' + DATEPART(YEAR,datecolumn)) = #daterange
You can simplify it, no need to reconstitute the date field from GETDATE():
SELECT blah
FROM table
WHERE DATEPART(MONTH,datecolumn) = DATEPART(MONTH,getdate()) AND
DATEPART(YEAR,datecolumn) = DATEPART(YEAR,getdate())
You can receive the first of the current month like this:
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
By adding one month to the above result, you'll receive the first of the next month:
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)
Using those two dates, you can retrieve the required rows like this:
SELECT
…
FROM table1
WHERE date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0)
AND date < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)
How about determining the beginning and end of the current month, and then selecting for records between them:
declare #monthBeginning datetime
set #monthBeginning = (select dateadd(dd,-(day(dateadd(mm,1,getdate()))-1),dateadd(mm,0,getdate()))
declare #monthEnd datetime
set #monthEnd = (select dateadd(dd, -day(dateadd(m,1,getdate())), dateadd(m,1,getdate())))
select *
from dateTable
where datecolumn between #monthBeginning and #monthEnd
If you find yourself using month beginning and ending calculations a lot, I'd recommend putting them into scalar functions - the logic is convoluted enough to not want to repeat it.

first day of the same month

I have a date parameter (#rptMonth) that is selected by the user from a datepicker calendar. The date must be the first day of the month. No matter what the user selects I'd like to turn that into mm/01/yyyy. For example- I need the first day of the month. So if the user selects 06/22/2010, I need to turn that into 06/01/2010. So in my query it would be something like WHERE YEAR_MONTH = DATEADD("m",datediff("m","1900-01-01",#RptMonth),"1900-01-01"),"mm/dd/yyyy" but when I try this I get incorrect syntax near ','. Don't know if this will even work.
Update:
select dateadd(month, datediff(month, 0, getdate()), 0)
Older:
Try this:
declare #arbitraryDate datetime;
set #arbitraryDate = getdate();
set #arbitraryDate = dateadd(dd, datediff(dd, 0, #arbitraryDate), 0) --strip time
select dateadd(dd, -day(#arbitraryDate)+1,#arbitraryDate) --strip days
Or this:
select cast(convert(varchar(6), getdate(), 112) + '01' as datetime)
This should work too:
SELECT CAST(CAST(YEAR(#pInputDate) AS VARCHAR(4)) +
CAST(MONTH(#pInputDate) AS VARCHAR(2)) + '01' AS DATETIME)