How to extract the YEAR in SQL Server 2008? - sql

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))
The output give me the Last Date of the Previous Month of the current date.
How can I extract Year from DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))?
Select DATEPART(YYYY,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0)))
Will the above Select statement extract the Year?

The answer to your question is "yes, you can use datepart(yyyy, ....). It turns out that "yyyy" is a synonym for the more commonly used "year" in this context.
However, most people would just use year(<whatever>) for this purpose.

SQL Server has a function YEAR() so to extract year from DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))
just do SELECT YEAR(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0)))
http://sqlfiddle.com/#!3/1fa93/5208

Related

Get first day of first month of previous year in yyyy-mm-dd format

How do I get the first day of the first month of previous year in yyyy-mm-dd format? ie. 2019-01-01.
This is the code I have tried:
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))
You can use DATEFROMPARTS() function in SQL Server for creating date from given year, month and date in integer as shown below. To get the previous year you can use Year() function and subtract 1 from that. First date and month is always 1 so it has been hard-coded here.
declare #IntYear int = Year(Getdate()) - 1 --Previous Year
Select datefromparts(#Intyear, 1, 1)
The output in SSMS is as shown below.
To get the output in the different format you can follow this link.
You seem to be looking to generate a string, not a date. Consider using date functions and string concatenation: you just need to substract 1 year from the current date, and then append '-01-01'
concat_ws('-', year(getdate()) - 1, '01', '01')
Demo on DB Fiddle
You basically have it. You just need the FORMAT function.
SELECT FORMAT (DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)), 'yyyy-MM-dd') as date

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)

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)

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.

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