Getting MonthAndYear name in sql? - 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))

Related

How to Convert String to Date in SQL Server?

I have date column in my table as string type example Feb-18. How do I change it to date type as 01-02-2018?
Use convert() with add one day :
select convert(date, '01-' + 'Feb-18')
SQL Server is pretty good about figuring out dates. But you need a day, so:
select convert(date, '01-' + datecol)
Note: You should be very careful about storing dates as strings. I would recommend that you test the conversion to be sure it works for all values:
select datecol
from t
where try_convert(date, '01-' + datecol) is null and
datecol is not null;
If this returns any rows, then you have bad dates in your data. Oh, it would have been better to catch these by rejecting the insert/updates in the first place. However, you might be able to figure out how to fix them.
You can also try the following way.
Select try_cast('01-' + 'Feb-18' as Date) as [String To Date]

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

How to format a date to "MM/dd"

So I currently have a field that is outputting dates as
03/26/14
This is a weird question, but is there anyway where I can have the date output without the year?
I would like the date to look like
03/26
Thanks!
If using SQL Server, you could:
SELECT CONVERT(VARCHAR(5), GETDATE(), 101)
Use this:
SELECT CONVERT(VARCHAR(5), GETDATE(), 03)

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.

showing only the year portion of a date

I want to change the date field in my sql view so that the date will show only the year.
For example, my date field is the typical '07/01/2011'. I want to be able to have the field that changes only to the year, then another one for month, etc.
Ive tried using CONVERT (VARCHAR(10), GETDATE(), 101), but that only shows the current date plus the format (101) isn't right.
Use the YEAR function:
SELECT OrderID, YEAR(OrderDate) AS Date
FROM dbo.Orders
Use the YEAR function?
There is also MONTH however you may want DATENAME to give July not 7
This is for SQL Server but every RDBMS has these or similar
For SQL Server, you can use datepart to extract portions of a date:
SELECT datepart(yyyy, getdate())
See the MSDN link for more information.
If it's T-SQL you're talking about, you can do DATEPART(year, GETDATE())