SQL get last month name - sql

Following command giving me current month (March) in short form.
SELECT left(datename(month, getdate()), 3)
But i need last month Feb.
I meant whenever i run this command will give me last month name.
What would be the sql command?

Assuming you are using T-SQL:
SELECT left(datename(month, dateadd(month,-1,getdate())), 3)
http://msdn.microsoft.com/fr-fr/library/ms186819.aspx

SELECT left(datename(month, dateadd(dd, -1, getdate())), 3)

try this:
SELECT left(datename(month, dateadd(m,-1,getdate())), 3)

Try this:
SELECT left(datename(month, date_sub( getdate(), interval 1 month ) ), 3)
This will work for MySQL (you didn't specify your SQL server), might need something similar to date_sub for different DBs.

SELECT left(datename(month, DATEADD(MM,-1,getdate())), 3)

select left(datename(month, dateadd(month, -1, getdate()),3)

SELECT DATENAME(MM,DATEADD(MM,-1,GETDATE())) AS 'The Name of last Month'
to print Name of the particular day we have to use DATENAME() function. So, we used this function here for that purpose.
MM represents month here.
-1 is used in Date function to get previous month.

Assuming TSQL, you're going to want to incorporate the DATEADD() function into that query:
http://msdn.microsoft.com/en-us/library/ms186819.aspx
wrap getdate in that and you should be golden.

Try this:
select substring(DATENAME(MONTH,getdate()),1,3)
i think it will help you much better
Regards,
Azarudhin S.

Related

Enter Parameter Value when getting the last dat of the month

I want to get the last day of the month by using the following formula I found here at Stackoverflow:
SELECT DATEADD(month, ((YEAR(#test) - 1900) * 12) + MONTH(#test), -1)
I'm sure this formula should work but when I use it I get a pop-up where I'm asked to enter a parameter value for Month.
Does anyone know how I could solve this issue?
Thank you in advance.
This looks like SQL Server. If so, just use EOMONTH():
select EOMONTH(#test)
I realize that you might be using MS Access. If that is the case you can use date functions to get the value:
select DateAdd(d, -1,
DateAdd(m, 1,
DateSerial(Year(#test), Month(#test), 1))
)
)

Find Birthday in upcoming month

I Need help in finding upcoming birthday in a month.
My Data is something like below , Both data types are nvarchar
Could anyone help me with the sql query please? how to set the DOB column into a date format and then find the birthday with month as 11 and date as 24.
Thanks in advance
Assuming SQL Server, you can use month() to extract the month from a date, for example getdate(), which is the current point in time. With left() you can extract the first characters of a string. That leads to something like:
SELECT [Name],
[Dob(mmdd)]
FROM elbat
WHERE month(getdate()) = left([Dob(mmdd)], 2);
In Microsoft SQL Server you can Create a date using the DATEFROMPARTS(int year, int month, int day) function. To get your month and day you would have to get the 2 parts of the string, the first 2 characters for month and the third and fourth characters as the day, you can use the SUBSTRING function for this. Then take each pair of characters for month and day and cast to int and use them in the DATEFROMPARTS function.
Then you want to see if your newly created date is BETWEEN today AND one month from today. So you could do something like this:
SELECT *
FROM SomeTable
WHERE
DATEFROMPARTS(YEAR(GETDATE()), CAST(SUBSTRING([Dob(mmdd)], 1, 2) as INT), CAST(SUBSTRING([Dob(mmdd)], 3, 2) as INT))
BETWEEN
DATEADD(DAY, -1, GETDATE()) AND DATEADD(MONTH, 1, GETDATE())
Note: this assumes [Dob(mmdd)] is always 4 characters.
You don't need the DOB in a date format. I am unclear what "upcoming" month means, but I suspect that it means a calendar month. If the current month, then:
where month(getdate()) = cast(left(dob, 2) as int)
If the next month, then:
where month(dateadd(month, 1, getdate())) = cast(left(dob, 2) as int)
Thanks, Everyone for the help.. I got this working,. both works perfect
select [USER_ID],[EMP_FULL_NM],[Birthday_Date] from [dbo].[COE]
where month(getdate())=left([Birthday_Date],2)
select [USER_ID],[JOINING_DT],[EMP_FULL_NM] from [dbo].[COE]
where SUBSTRING(CONVERT(VARCHAR(10), [JOINING_DT], 101),1,2) = month(getdate())

How to return month short form from date in SQL Server?

I am using this code
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
to get the month name. But how can I limit the name of the month up to 3 characters only? Example is FEB in this month.
Assuming SQL server:
Select FORMAT(getdate(),'MMM') as shortMonthName
Assuming you're using SQL Server, you can use the SUBSTRING function, i.e.
SELECT SUBSTRING(DATENAME(month, GETDATE()), 1, 3) AS 'Month Name'
To get your date in format "FEB" you can try
Select LEFT(UPPER(DATENAME(MM, Getdate())),3)

Getting MonthAndYear name in sql?

I need a small script to get the current month name and current year. So as to create a unique batch to store statements. My preferred format is January2011 or Feb2011 whichever is easier to get. i.e. MonthYear
Try a query that looks like this:
SELECT DATENAME(month, GETDATE()), DATENAME(year, GETDATE());
You can find some more information over here.
Have a look at the DATEPART function and combine that with finding the name of a month, this SO question/answer has information on doing that.
You can use the following:
select datename(month,getdate()) + cast(datepart(year,getdate()) as char(4))

Get the 2 digit year in T-SQL

I need to get the current 2 digit year and increment by one. So the current number I'm looking for should be 11. How?
You can do ( YEAR( GETDATE() ) % 100 ) + 1
See GETDATE & YEAR
This will work for you
select Right(Year(getDate())+ 1,2)
For SQL Server 2012 and above, I'd suggest using FORMAT(#DATE, 'yy'):
SELECT FORMAT(DATEADD(year, 1, GETDATE()), 'yy')
Format offers a cleaner, more readable solution. Thus, less guesswork, and a better maintainability.
SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 1),2) as YEAR
You can try this in sql server
SELECT FORMAT(GETDATE(), 'yy')
If you are always going to be using GetDate() why not just do something like this:
Select (Year(GetDate()) - 2000) + 1
Dang people. Always making things so complicated. It's not like you are going to be living for another 1000 years!
select CAST( DAY(GETDATE()) as varchar(10))+'/'+CAST( month(GETDATE()) as varchar(10))+'/' +cast(right(year(getDate()),2) as varchar)