Datetime Format in Month and Year - sql

I want to format a datetime column like so: "March 2004." Currently, I tried
DECLARE #Date VARCHAR(20) = '2004-03-05 01:00'
SELECT CONVERT(VARCHAR(20),CAST(#Date AS DATETIME),13) AS DateFormats
but not getting the right result.

You should really apply formatting when you present your data, not in the query, but to answer the question, if you're using SQL Server 2012 or above you can use FORMAT
DECLARE #Date datetime = '2004-03-05 01:00'
SELECT FORMAT( #Date, 'MMMM yyyy' ) AS DateFormats

Something like:
DECLARE #Date VARCHAR(20) = '2004-03-05 01:00'
SELECT DATENAME(MONTH, #Date) + ' ' + DATENAME(YEAR, #Date)
More on DATENAME can be found here https://msdn.microsoft.com/en-gb/library/ms174395.aspx
Essentially it gets the month part of the date and the year part of the date and concatenate them together.

None of the built-in formats include the full month. Instead, use datename():
select datename(month, #date) + ' ' + datename(year, #date)

Related

How to convert '2017-11' to 'November 2017' in SQL

I want to convert or turn a string to date, but display like 'November 2017'
My field is: [Fiscal_post_year_month] nvarchar(max)
I'm just going to put a guess that you are using MSSQL, this should do what you want.
DECLARE #THEDATE NVARCHAR(MAX)
--need to have some kind of 'DAY' but we won't use it
SET #THEDATE = '2017-11' + '-01'
--we turn the number back into a VARCHAR and then concatenate them together as per your request
SELECT DATENAME (MONTH, #THEDATE) + ' ' + CAST(DATEPART (YEAR ,#THEDATE) AS VARCHAR(MAX))
results:

How to use dateadd function in SQL Server 2017

I am trying to add character type in SQL Server using DATEADD function.
I want the 25th of the next month of the variable.
Example
DECLARE #Date char(6)
SET #Date = '201712'
I want result = 20180125
DECLARE #Date char(6)
SET #Date = '201801'
I want result = 20180225
Thanks in advance
How about this?
dateadd(month, 1, dateadd(day, 25, cast(#date + '01' as date)))
You can shorten this to:
dateadd(month, 1, cast(#date + '25' as date))
Get the month end date by adding '01' to the yyyymm string using EOMONTH function and add 25 days to it.
select dateadd(day,25,eomonth(#date+'01'))

How to update smalldatetime field with only current date(without current time) in SQL Server 2008

I have added a new column [PriceStartOn] of type smalldatetime to a table with existing data. Now I want to fill the column with the current date.
I used the GETDATE() function but it also add the current time.
The final result should be like this
2014-07-24 00:00:00
I searched the web and couldn’t find any sample on this. Based on other answers, I tried following code, it shows the correct format but the date was wrong, it returned 1905-08-08 00:00:00
DECLARE #date DATETIME
SET #date = GETDATE()
UPDATE Product
SET [PriceStartOn] = ('' + DATEPART( year, #date ) + '-' + DATEPART( MONTH, #date) + '-' + DATEPART( DAY, #date) + '')
WHERE [PriceStartOn]IS NULL;
Does anyone have any ideas?
Thanks
Just cast the datetime to date:
UPDATE Product
SET PriceStartOn = cast(#date as date)
WHERE PriceStartOn IS NULL;
You don't actually need a variable for this, you can do this directly on the function:
UPDATE Product
SET PriceStartOn = cast(getdate() as date)
WHERE PriceStartOn IS NULL;

Subtracting One Year

SELECT
#begindate = dateadd(mm,-1,CAST(datepart(mm,getdate()) AS VARCHAR(2)) + '/15/' + CAST(datepart(YYYY,getdate() -1) AS varchar(4))),
#enddate = CAST(datepart(mm,getdate()) AS VARCHAR(2)) + '/14/' + CAST(datepart(YYYY,getdate() -1) AS varchar(4))
Right now this code returns the dates May 15th - June 14th. Those are the required dates that I need, but what I also need is for it to be in the year 2013, and this returns the year 2014. I've tried doing a dateadd(yyyy, -1) and it didn't work. So I was wondering if anyone knew how I would be able to get last years date.
If DateAdd didn't work, there must have been an error in your implementation. Try it like this after your existing code:
SET #begindate = dateadd(year, -1, #begindate);
SET #enddate = dateadd(year, -1, #enddate)
declare #begindate datetime
declare #enddate datetime
set #begindate = dateadd(mm,-1,CAST(datepart(mm,getdate()) AS VARCHAR(2)) + '/15/' + CAST(datepart(YYYY, getdate()) - 1 AS varchar(4)))
set #enddate = CAST(datepart(mm,getdate() ) AS VARCHAR(2)) + '/14/' + CAST(datepart(YYYY, getdate()) - 1 AS varchar(4))
select #begindate
union all
select #enddate
Instead of working with strings you can use date variables and date arithmetic:
declare #lastyear date=dateadd(yy,-1,cast(getdate() as date))
declare #currentMonth date=dateadd(d,-day(#lastyear),#lastyear)
declare #prevMonth date=dateadd(m,-1,#currentMonth)
select #lastyear,DATEADD(d,14,#currentMonth),DATEADD(d,15,#prevMonth)
In SQL Server 2012 it's even easier because you can use the DATEFROMPARTS function to construct a new date from its parts.

How do I concatenate numbers to create a custom date?

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)