I need the month+year from the datetime in SQL Server like 'Jan 2008'. I'm grouping the query by month, year. I've searched and found functions like datepart, convert, etc., but none of them seem useful for this. Am I missing something here? Is there a function for this?
select
datepart(month,getdate()) -- integer (1,2,3...)
,datepart(year,getdate()) -- integer
,datename(month,getdate()) -- string ('September',...)
If you mean you want them back as a string, in that format;
SELECT
CONVERT(CHAR(4), date_of_birth, 100) + CONVERT(CHAR(4), date_of_birth, 120)
FROM customers
Here are the other format options
Beginning with SQL Server 2012, you can use:
SELECT FORMAT(#date, 'yyyyMM')
Use:
select datepart(mm,getdate()) --to get month value
select datename(mm,getdate()) --to get name of month
In SQL server 2012, below can be used
select FORMAT(getdate(), 'MMM yyyy')
This gives exact "Jun 2016"
Funny, I was just playing around writing this same query out in SQL Server and then LINQ.
SELECT
DATENAME(mm, article.Created) AS Month,
DATENAME(yyyy, article.Created) AS Year,
COUNT(*) AS Total
FROM Articles AS article
GROUP BY
DATENAME(mm, article.Created),
DATENAME(yyyy, article.Created)
ORDER BY Month, Year DESC
It produces the following ouput (example).
Month | Year | Total
January | 2009 | 2
How about this?
Select DateName( Month, getDate() ) + ' ' + DateName( Year, getDate() )
That format doesn't exist. You need to do a combination of two things,
select convert(varchar(4),getdate(),100) + convert(varchar(4),year(getdate()))
( Month(Created) + ',' + Year(Created) ) AS Date
the best way to do that is with :
dateadd(month,datediff(month,0,*your_date*),0)
it will keep your datetime type
cast(cast(sq.QuotaDate as date) as varchar(7))
gives "2006-04" format
The question is about SQL Server 2005, many of the answers here are for later version SQL Server.
select convert (varchar(7), getdate(),20)
--Typical output 2015-04
SQL Server 2005 does not have date function which was introduced in SQL Server 2008
returns the full month name, -, full year e.g. March-2017
CONCAT(DATENAME(mm, GetDate()), '-', DATEPART(yy, GetDate()))
I had the same problem and after looking around I found this:
SELECT DATENAME(yyyy, date) AS year
FROM Income
GROUP BY DATENAME(yyyy, date)
It's working great!
Converting the date to the first of the month allows you to Group By and Order By a single attribute, and it's faster in my experience.
declare #mytable table(mydate datetime)
declare #date datetime
set #date = '19000101'
while #date < getdate() begin
insert into #mytable values(#date)
set #date = dateadd(day,1,#date)
end
select count(*) total_records from #mytable
select dateadd(month,datediff(month,0,mydate),0) first_of_the_month, count(*) cnt
from #mytable
group by dateadd(month,datediff(month,0,mydate),0)
---Lalmuni Demos---
create table Users
(
userid int,date_of_birth date
)
---insert values---
insert into Users values(4,'9/10/1991')
select DATEDIFF(year,date_of_birth, getdate()) - (CASE WHEN (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()),date_of_birth)) > getdate() THEN 1 ELSE 0 END) as Years,
MONTH(getdate() - (DATEADD(year, DATEDIFF(year, date_of_birth, getdate()), date_of_birth))) - 1 as Months,
DAY(getdate() - (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()), date_of_birth))) - 1 as Days,
from users
Yes, you can use datename(month,intime) to get the month in text.
,datename(month,(od.SHIP_DATE)) as MONTH_
Answer:
MONTH_
January
January
September
October
December
October
September
It's work great.
DECLARE #pYear VARCHAR(4)
DECLARE #pMonth VARCHAR(2)
DECLARE #pDay VARCHAR(2)
SET #pYear = RIGHT(CONVERT(CHAR(10), GETDATE(), 101), 4)
SET #pMonth = LEFT(CONVERT(CHAR(10), GETDATE(), 101), 2)
SET #pDay = SUBSTRING(CONVERT(CHAR(10), GETDATE(), 101), 4,2)
SELECT #pYear,#pMonth,#pDay
The following works perfectly! I just used it, try it out.
date_format(date,'%Y-%c')
Related
I searched a lot online and could not find it. I found just the opposite - date to day
For example: Day 221 ==> 09/08/2017
This query do from this date to day number:
SELECT DATEPART(DAYOFYEAR, SYSDATETIME())
or
SELECT DATEDIFF(day, CAST(DATEPART(YEAR, GETDATE()) AS CHAR(4)) + '-01-01', GETDATE() + 1) AS number_of_today
Thanks
Yet another option is with DateFromParts() and GetDate() for the current year
Example
Select DateAdd(DAY,221,DateFromParts(Year(GetDate())-1,12,31))
Returns
2017-08-09
This will give you the date as it relates to January 1st, 1900:
SELECT
dateadd(day, dayToConvert, 0)
FROM
myTable
I think you can change it to base off of January 1st, 2017 by:
SELECT
dateadd(day, dayToConvert, '2016-12-31')
FROM
myTable
It is December 31st because you want to add that many days to the date in the third argument.
The DATEADD function should work, as so:
SELECT DATEADD(day,221,someDate) AS someDate2 FROM sometable
Docs here
Tutorial here
Stored Procedure Option:
CREATE PROCEDURE PROC_NUM_TO_DATE #NUM INT,#DATESTART DATE AS
BEGIN
SELECT DATEADD(DAY,#NUM ,DATEADD(Day,-1,#DATESTART))
END
GO
EXECUTE:
EXEC PROC_NUM_TO_DATE 221,'2017-01-01'
RESULT:
2017-08-09
Please let me know how can i get number of days in a month when I give date in MMM-YYYY format.
Eg: JAN-2017 = 31
Any helps appreciated..
Thanks
Check This.
declare #D varchar(20)
set #D ='JAN-2017'
select
datediff(day, '01-'+#D, dateadd(month, 1,'01-'+ #D)) as NoOfDay
try this..........enter code here
SELECT day(GETDATE() )
In Sql Server you can do that with the first day of the specified month
select datediff(day, #date, dateadd(month, 1, #date))
For MySQL users...
SET #convertedDate = STR_TO_DATE( CONCAT( '01-',
'Jan-2017' ),
'%d-%b-%y' );
SELECT DATEDIFF( DATE_ADD( #convertedDate,
INTERVAL 1 MONTH ),
#convertedDate ) AS NumOfDays;
The 'Feb-2017' bit is your column or variable
SELECT DATEPART(DAY, EOMONTH('01-' + 'Feb-2017'))
StartDate = 01/01/2013
EndDate = 12/31/2019
I need to get the year column like '2013-14' , '2014-15' , 2015-16 and so on.
For example, the 2013-14 year should contains date details from Jun 2013 to May 2014. Like wise i need to get the year upto End date. Please help me out.
concat(datepart(YY,StartDate ),'-', RIGHT(YEAR(StartDate ),2) + 1)
I used the above format. i can get the output as 2013-14. But i need to specify the range of month for splitting years.
Regards,
Vanmathi
Use the FORMAT function:
SELECT
FORMAT(StartDate, 'yyyy-MM'),
FORMAT(EndDate, 'yyyy-MM')
Read all about the FORMAT function on the official MSDN documentation page.
Use a user defined function to slice up the date and then text concatenation.
CREATE FUNCTION GetSlicedYear(#TheDate date)
RETURNS varchar(7)
AS
BEGIN
DECLARE #SlicedYear varchar(7)
IF DATEPART(month, #TheDate) < 6
SELECT #SlicedYear = (DATEPART(year, #TheDate) -1 ) +"-"+ RIGHT(CAST(YEAR(#TheDate) As varchar(4)),2)
ELSE
SELECT #SlicedYear = DATEPART(year, #TheDate) +"-"+ RIGHT(CAST((YEAR(#TheDate) +1) As varchar(4)),2)
RETURN #SlicedYear
END
Then something like this to Order (or group).
SELECT SomeField, GetSlicedYear(SomeDate) FROM SomeTable ORDER BY GetSlicedYear(SomeDate)
You can use the below select statement -
select FORMAT(StartDate , 'yyyy') + '-' + FORMAT(EndDate , 'yyyy')
Here is what I have tried thus far:
select CAST(
DATEPART(month,getDate())+'-'+
DATEPART(day,getDate())+'-'+
2012
as datetime)
I end up with the date: 1905-08-02 00:00:00.0. I was expecting to get today's date. I have rearranged the order and it doesn't seem to change. Can anyone offer as to why it gives me this? For the record, I plan to use other values than 2012 for the year.
Thanks in advance.
CAST() each piece as a varchar first:
select
cast(
cast(DATEPART(month,getDate()) as varchar(2))+'-'+
cast(DATEPART(day,getDate()) as varchar(2))+'-'+
'2012' as datetime)
select CAST ('2012'+
CAST(DATEPART(month,getDate()) as char(2))+
CAST(DATEPART(day,getDate()) as char(2))
as datetime)
You have to concatenate strings. Your code is casting the number 2039 to date.
If the goal with this little exercise is to be able to change the year of a given date you can do like this instead.
declare #NewYear int = 2003
-- with time part
select dateadd(year, #NewYear - year(getdate()), getdate())
-- time part removed
select dateadd(year, #NewYear - year(getdate()), dateadd(day, 0, datediff(day, 0, getdate())))
This code will work, you need to make sure that you are concatenating same data types and use convert with specific DateTime Format:
SELECT CONVERT(DATETIME,
CAST(DATEPART(month,getDate()) AS NVARCHAR(50))
+'-'+CAST(DATEPART(day,getDate()) AS NVARCHAR(50))
+'-2012'
,121)
Well this is my case: I have an input date X (dd-mm-yyyy), and I want to count the number of days between it with the year part is changed into current year and today's date in SQL. I t comes with the following condition, after the year is changed temporarily: (Here's my current idea of the logic)
- If date X is earlier than today, then difference = datediff(X,now), with the X year is current year
- If date X is later than today, then difference = datediff(X,now), with the X year is one year before
Sample case:
1st case: The input date is 6-6-1990. Today (automatically generated) is 22-8-2011. Then the difference will be = datediff(6-6-2011,22-08-2011)
2nd case: The input date is 10-10-1990. Today (automatically generated) is 22-8-2011. Then the difference will be = datediff(10-10-2010,22-08-2011)
Any idea how to do this in SQL (in SQL Server)? Or is there any other more simple alternatives for this problem? I'd also like this to be done in the query and not using a stored procedure or function
Sorry if there's already a similar question, I just don't know the exact keyword for this problem :( if there's a question like this previously, feel free to direct me there.
Thanks in advance
Here is the implementation (if I understood the logic you need correctly):
USE YourDbName
GO
CREATE FUNCTION YearPartDiff (#date datetime)
RETURNS int
AS
BEGIN
DECLARE #dateCurrentYear datetime
SET #dateCurrentYear = DATEADD(year, YEAR(GETDATE()) - YEAR(#date), #date)
DECLARE #result int
IF #dateCurrentYear < GETDATE()
SET #result = ABS(DATEDIFF(day, #dateCurrentYear, GETDATE()))
ELSE
SET #result = ABS(DATEDIFF(day, DATEADD(year, -1, #dateCurrentYear), GETDATE()))
RETURN(#result)
END
GO
And the example of usage:
USE YourDbName
GO
DECLARE #someDate datetime
SET #someDate = '2011-06-06'
SELECT dbo.YearPartDiff(#someDate) /*returns 77*/
SET #someDate = '2010-10-10'
SELECT dbo.YearPartDiff(#someDate) /*returns 316*/
Basically, #Andrei's solution, but in a single statement:
SELECT
DayDiff = DATEDIFF(
DAY,
DATEADD(YEAR, CASE WHEN LastOcc > GETDATE() THEN -1 ELSE 0 END, LastOcc),
GETDATE()
)
FROM (
SELECT LastOcc = DATEADD(YEAR, YEAR(GETDATE()) - YEAR(#InputDate), #InputDate)
) s
This seems to do the job
SELECT DATEDIFF(DAY, CONVERT(DATETIME, N'2011-06-06'), CONVERT(DATETIME, N'2011-08-22'))
So the basic syntax is
SELECT DATEDIFF(DAY, CONVERT(DATETIME, N'yyyy-mm-dd'), CONVERT(DATETIME, N'yyyy-mm-dd '))
Alternatively, you can use GETDATE() instead of the string for today's date
I have used "SELECT DATEDIFF( D, "+myDate+", GETDATE())" in my code, on SQL Server 2005. It works for me. The value myDate of course would be the DateTime input value.
you should try this query:
create table #T (inp_date datetime)
insert #T values ('06-06-1990')
insert #T values ('08-22-1990')
insert #T values ('10-10-1990')
--select * from #T
select inp_date, GETDATE(),
CASE
WHEN DATEADD(yy,DATEDIFF(yy,inp_date,GETDATE()),inp_date) <= GETDATE()
THEN DATEDIFF(dd,DATEADD(yy,DATEDIFF(yy,inp_date,GETDATE()),inp_date),GETDATE())
ELSE DATEDIFF(dd,DATEADD(yy,DATEDIFF(yy,inp_date,GETDATE())-1,inp_date),GETDATE())
END
from #T