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

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)

Related

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())

SQL Server query to get previous month name in string from another month in string

I need to get previous month name from given month name.
For example I need to get previous month as May for the given month June.
There is no date.
I tried select CAST(Monthname-1 AS Varchar(max)) from table
Because "There is no date."
You can convert an augmented string {month name} + ' 01, 1980' to a date, and then perform the date calculation
Example
Select datename(MONTH,dateadd(MONTH,-1,convert(date,'June'+' 01,1980')))
If 2012+ and potentially bogus data ... try_convert()
Select datename(MONTH,dateadd(MONTH,-1,try_convert(date,'June'+' 01,1980')))
Returns
May
Add -1 months to your starting date, then use DATENAME(month) to get its name.
DATENAME(month, DATEADD(month, -1, <yourDateField>))
You can try this:
SELECT DATENAME(MM,CAST('1 June 2018' AS datetime)-1)

Date Conversion in SQL

I have a date in following format in my DB.
10/16 - mm/yy
I need to convert it to:
October/16
Is this possible?
If it's not possible then please tell me why.
This is not a date, it's missing the day, it's a bad way to store year/month. There should be a 4 digit year to avoid confusion and the year should be listed first to enable correct sorting, e.g. '2016/10' or a numeric value 201610.
You can cast it to a DATE first and then use a FORMAT to disply only month/year:
set dateformat myd;
select format(cast(mystupidcolumn + '/1' as date), 'MMMM/yy')
Or SUBSTR the month part and use a CASE.
try this format,
SELECT DATENAME(month, DATEADD(month, #mydate-1, CAST('2008-01-01' AS datetime)))
You can display date by using this code
select datename(month, YourColumnName) + '/' + right(YEAR(YourColumnName),2)
FROM yourTableName
Simply change yourColumnName with name of your table column and yourTableName with name of table.
Yes you can, and it depend in what database you use to call date functions
If you column Datetime format
SQL server DATENAME(Month, GETDATE())
MySQL database MONTHNAME(now())
otherwise
convert it will in your choice at database or you code logic
split the value and lookup at month enum or fake the date to be accepted and complete date format like 01/10/16
so do something like SELECT DATENAME(Month, datecolumn) + '/' + YEAR (datecolumn)
also you can use instead of Year function DATEPART(yy,datecolumn)
the way you do it with format will look like
CONVERT(VARCHAR(11),GETDATE(),106)
but excepted to get first 3 char of month JUN

Month name in SQL Server

Is there a build-in function in SQL Server which return my polish (or any other language) name of month by month number? Or need I write my own function?
For example
GetMonthName(11) => Listopad (November)
You can use the function Month or datepart "month" to return a month number from date/time. Then you can use the DATENAME function:
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
The return value depends on the language environment set by using SET LANGUAGE and by the "configure the default language" server configuration option for the login.
So as per Jacek's comment, this can be summarised as:
SET LANGUAGE Polish SELECT DATENAME(month, GETDATE()) AS 'Month Name'
If literally all you have is a month number, you would need to build a datetime variable that incorporates this month number so that you can apply the above approach. In SQL Server 2012, you can use DATETIMEFROMPARTS:
SELECT #RandomDate = DATETIMEFROMPARTS(2012, #MonthNumber, 1, 1, 1, 1, 0)
SET LANGUAGE Polish SELECT DATENAME(month, #RandomDate) AS 'Month Name'

SQL get last month name

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.