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, e
the best way to do that is with :
dateadd(month,datediff(month,0,*your_date*),0)
it will keep your datetime type
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
Use:
select datepart(mm,getdate()) --to get month value
select datename(mm,getdate()) --to get name of month
( Month(Created) + ',' + Year(Created) ) AS Date
,datename(month,(od.SHIP_DATE)) as MONTH_
Answer: MONTH_ January January September October December October September
returns the full month name, -, full year e.g. March-2017
CONCAT(DATENAME(mm, GetDate()), '-', DATEPART(yy, GetDate()))