Month name in SQL Server - sql

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'

Related

Eomonth function on SQL Server 2014

I am getting an error on this function, even it is defined. What could cause this problem?
EOMONTH() was introduced in SQL Server 2012.
I suspect the Compatibilty level of your database is set below that (or you are in fact not on SQL Server 2014).
ALTER DATABASE database_name
SET COMPATIBILITY_LEVEL = 120 -- 120 == SQL Server 2014
To determine your SQL Server version run:
SELECT ##VERSION
An alternative way of calculating EOMONTH is:
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, #Date) + 1, 0))
EOMONTH() returns a date type
REPLACE ( string_expression , string_pattern , string_replacement )
REPLACE ( EOMONTH( cast(#variable as date) ) '-','.')
You are attempting an implicit conversion from date to string, and it seems you are assuming that a date data type actually contains a dash, which isn't true. Just use format to the desired output. No need for replace and use a set of DATEADD() functions to substitute for EOMONTH()
select format(dateadd(day,-1,dateadd(month, datediff(month,0, getDate()), 0)),'yyyy.MM.dd')

Date Manipulation in SQL

I tried different functions to convert Datepart for year, month and day to a date but I am getting errors.
This is what I tried:
SELECT
CONVERT(datetime, CHAR(YEAR(GETDATE()) + CHAR(DATEPART(MM, '02')) + CHAR(DATEPART(DD, '28')));
When running this code, I get an error:
Conversion failed when converting date and/or time from character string.
In case I remove those single quotes on 02 and 28 then SQL will return null.
Thanks in advance
If 2012+ you could also try DateFromParts()
Select DateFromParts(2016,2,28)
Returns
2016-02-28
Try this instead:
SELECT CONVERT(datetime, DATENAME(YEAR, GETDATE()) + '-02-28')
You can't extract a day or month from a string such as '02'. But there is no need to do that anyway.
As you can read in the documentation on DATEPART, the second parameter should be a date parameter:
Returns an integer that represents the specified datepart of the specified date.
In your SQL statement you are misusing this function twice... following are invalid because the second parameter cannot be converted to a date type:
DATEPART(MM,'02')
DATEPART(DD,'28')
You can construct a date from its parts using the DATEFROMPARTS function in SQL Server 2012 and later versions:
Returns a date value for the specified year, month, and day.
Or if you need the time parts filled in as well, DATETIMEFROMPARTS
Returns a datetime value for the specified date and time.
In versions prior to SQL Server 2012, this can be done like this:
DATEADD(MM, (#year - 1900) * 12 + #month - 1 , #day - 1);

Function get the last day of month in sql

I need to get the last day of month with input of month and year. For example, with input 06/2016 it will return 30. I use SQL Server 2005. Thanks for any help.
Suppose your input is VARCHAR in the form of MM/YYYY.
Use RIGHT and LEFT to get the year and month respectively. Then use DATEFROMPARTS to generate the starting date. Next, use EOMONTH to get the last day of the month. Finally use DAY to extract the day part.
DECLARE #input VARCHAR(7) = '06/2016'
SELECT
DAY(
EOMONTH(
DATEFROMPARTS(CAST(RIGHT(#input,4) AS INT),CAST(LEFT(#input, 2) AS INT),1)
)
)
The above only works for SQL Server 2012+.
For SQL Server 2005, you can use DATEADD to generate the dates:
SELECT
DAY( -- Day part
DATEADD(DAY, -1, -- Last day of the month
DATEADD(MONTH, CAST(LEFT(#input, 2) AS INT), -- Start of next month
DATEADD(YEAR, CAST(RIGHT(#input, 4) AS INT) - 1900, 0) -- Start of the year
)
)
)
Reference:
Some Common Date Routines
You would do something like this:
select eomonth(getdate(), 0);
If you want it formatted as MM/YYYY then you'd do this:
select format(eomonth(getdate(), 0), 'MM/yyyy');
Pardon me for tossing-in a response that is not specific to "SQL Server," nor thence to "2005," but the generalized way to compute the answer that you seek is as follows:
Break down the input that you have, e.g. 06/2016, into two parts. Call 'em #MONTH and #YEAR. Define a third value, #DAY, equal to 1.
Typecast this into a date-value ... "June 1, 2016."
Now, using the date-handling functions that you're sure to have, "first add one month, then subtract one day."
One thing that you must be very careful of, when designing code like this, is to be certain(!) that your code for decoding 06/2016 works for every(!) value that actually occurs in that database, or that it can be relied upon to fail.
try this,
declare #input varchar(20)='06/2016'
set #input=#input+'/01'
declare #dtinput datetime=#input
select dateadd(day,-1,dateadd(month,datediff(month,0,#dtinput)+1,0))
--OR in sql server 2012
select eomonth(#dtinput)

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)

SQL automatic date range using DateSerial function

We've been using MS Access, with the following syntax for MTD Data that works for us:
Between DateSerial(Year(Date()),Month(Date()),1)
And DateSerial(Year(Date()),Month(Date())+1,0)
We need to transition the above logic to SQL/SSRS for automatic emailed reports, but I cannot get this DateSerial logic to work with SQL.
In the Filter field of the SQL query, I can successfully use BETWEEN '8/1/2014' AND '8/31/2014' for MTD data, but would like to have a DateSerial logic applied so that reports don't need to be created for every month, quarter, year, etc.
When trying to use the DateSerial function, we get the error "Invalid or missing Expression". I've seen a few topics on this that Parameters are required, but really believe that this is a simple syntax issue for the filter field, since actual dates work with the BETWEEN command.
There are several different ways to get this. Here is just one way.
Get todays date. In this case 8/27/2014
Declare #Today date = cast(getdate() as date)
Get the first of the month, 26 days in the past
Declare #StartDate date = dateadd(d, -1 * (day(#Today) - 1), #Today)
select #Today, #StartDate
You can use the function CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Or the function DATEFROMPARTS if you are using SQL Server 2012:
http://msdn.microsoft.com/en-us/library/hh213228.aspx
Or DATEADD:
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0); -- first day of current month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1) -- last day of current month
This last one I took from: https://stackoverflow.com/a/11746042/1274092
See mine at:
http://sqlfiddle.com/#!3/d41d8/38333
This has been resolved. The ODBC driver does not apparently play well with SSRS. The DateSerial command would not work within the query itself. The workaround was to add the filter to the Dataset. This syntax is what works, but again only in the Dataset filter: [expression] Between [first value box] =DateSerial(Year(Now()),1,1) [second value box] =DateSerial(Year(Now()),12,31)
This gives us the YTD reporting data that we require.