How to get month and year from the date - sql

Using SQL Server 2000
Date like 20120101, 20120201, 20120301...
Fomat is yyyymmdd
I want to display month and year from the date like 01/2012, 02/2012, 03/2012...
Expected Output
01/2012
02/2012
03/2012
I don't want 1/2012, 2/2012, 3/2012...
Can any one give me a idea or query help....

You can use something like this:
DECLARE #input VARCHAR(20)
SET #input = '20120101'
-- convert the 20120101 to a DATETIME
DECLARE #thedate DATETIME
SELECT #thedate = CONVERT(DATETIME, #input, 112)
-- reformat that DATETIME to your needs
DECLARE #output VARCHAR(20)
SET #output = RIGHT('00' + CAST(DATEPART(MONTH, #thedate) AS VARCHAR(2)), 2) +
'/' + CAST(DATEPART(YEAR, #thedate) AS VARCHAR(4))
SELECT #output
You could "hide" this functionality into a user-defined function to make it more easily usable in your code:
CREATE FUNCTION dbo.MonthYearFromDate (#input VARCHAR(20))
RETURNS VARCHAR(20)
AS BEGIN
DECLARE #thedate DATETIME
SELECT #thedate = CONVERT(DATETIME, #input, 112)
DECLARE #output VARCHAR(20)
SET #output = RIGHT('00' + CAST(DATEPART(MONTH, #thedate) AS VARCHAR(2)), 2) + '/' + CAST(DATEPART(YEAR, #thedate) AS VARCHAR(4))
RETURN #output
END
and then you can call this like so:
SELECT dbo.MonthYearFromDate('20120515')
and get the output
05/2012

SELECT CAST(datepart(month,getdate()) AS CHAR) + '/' + CAST(datepart(year,getdate()) AS CHAR)
Instead of the function getdate() you need your date field.

have a look at the SUBSTRING function http://msdn.microsoft.com/en-us/library/ms187748.aspx
You should also consider storing your dates as a DATETIME (I assume you are using VARCHAR?)

Search on extract() function. It will solve your problem.

Related

Extract month and day from a field in sql

how do you select Month and day like in 'MMDD' format from a column, which has dates in '2014-01-12'(Datetime) format?
If your filed is a Date or DateTime, I would use the function DATEPART.
For example, this gets the current year:
DATEPART(year, GETDATE())
View the msdn page for the full documentation.
If your field is text, use CONVERT to convert your field to a DATE, then use the first method with the converted date as your value.
For example:
DATEPART(year, CONVERT(DATE, '11/1/2014'))
Complete Copy/Paste Example
DECLARE #DateVal VARCHAR(10) = '11/01/2014'
DECLARE #Month VARCHAR(2) = CAST(DATEPART(MONTH, CONVERT(DATE, #DateVal)) AS VARCHAR),
#Day VARCHAR(2) = CAST(DATEPART(DAY, CONVERT(DATE, #DateVal)) AS VARCHAR)
PRINT REPLICATE('0', 2 - LEN(#Month)) + #Month + REPLICATE('0', 2 - LEN(#Day)) + #Day

How to format datetime as M/D/YYYY in SQL Server?

To convert a datetime to MM/DD/YYYY, this works:
declare #datetime datetime = '2015-01-01'
select convert(varchar(10),convert(date,#datetime),101)
This evaluates to 01/01/2015. How can I have the date convert to 1/1/2015 instead?
Nothing on http://www.sql-server-helper.com/tips/date-formats.aspx matches the M/D/YYYY format.
I think the only possibility you have is to do something like this:
DECLARE #datetime DATETIME = '2015-01-01'
SELECT LTRIM(STR(MONTH(#datetime))) + '/' +
LTRIM(STR(DAY(#datetime))) + '/' +
STR(YEAR(#datetime), 4)
With SQL Server 2012 and above, you can do this:
SELECT FORMAT(#datetime, 'M/d/yyyy')
DECLARE #datetime DATETIME = '2015-01-01';
SELECT STUFF(REPLACE('/' + CONVERT(CHAR(10), #datetime, 101),'/0','/'),1,1,'')
This is how it works:
First CONVERT the DATETIME to CHAR
Then Add a '/' character at the begining
REPLACE all '/0' with '/'
With STUFF, get rid of the first '/'
There's no convert style that uses single digit day or month. This could work, though.
declare #datetime datetime = '2015-01-01'
select
cast(month(#datetime) as varchar(2)) + '/' +
cast(day(#datetime) as varchar(2)) + '/' +
cast(year(#datetime) as varchar(4))

How to Convert int value 20140401 to 'April 2014' in SQL Server 2008

This is what I tried .....
declare #date varchar(8)
set #date = (select max(convert(varchar(10), fileextractperiod))
from [info-CentralReturns-DEV].CentralReturns.FactCancer)
select CONVERT(varchar(10), cast(#date as DATE), 103)
Resulting output: 01/04/2014
But I need output as April 2014
SQL Server 2008 only supports the limited set of system-provided styles for CONVERT - if none of those match your needs, you'll need to handle it yourself. SQL Server 2012 has a FORMAT function for this purpose..
So in SQL Server 2008, you could create a function like this:
CREATE FUNCTION dbo.FormatDateMonthYear(#input DATE)
RETURNS VARCHAR(50)
AS BEGIN
DECLARE #Year INT = YEAR(#input)
DECLARE #MonthName VARCHAR(15) = DATENAME(MONTH, #input)
DECLARE #Result VARCHAR(50)
SET #Result = #MonthName + ' ' + CAST(#Year AS VARCHAR(4))
RETURN #Result
END
and then call it to get the format you need:
SELECT dbo.FormatDateMonthYear('2014-04-22')
and you should get
April 2014
SELECT
CONVERT(CHAR(4), #date, 100) + CONVERT(CHAR(4), #date, 120)
SELECT
DateName( month , DateAdd( month , cast(SUBSTRING(#date, 5, 2) as int), -1 ) )
+ ' ' + SUBSTRING(#date, 1, 4)
Select DATENAME(MONTH,#date)+' '+CONVERT(VARCHAR(4),YEAR(#Date))

Getting Datetime by Year, Month, Date

I want to get date by Using Year, Month and Day functions. Example
declare #Date smalldatetime
select #Date = Year('20140530') + Month('20140530') + Day('20140530')
What I want is to assign #Date = '20140530' as smalldatetime. But I want to do this by means of somwething similar to above expression. How can Ido this. Thanks in advance.
Instead try something like below
declare #Date varchar(20)
select #Date = cast(Year('20140530') as varchar) +
cast(Month('20140530') as varchar) +
cast(Day('20140530') as varchar)
select #Date
results in: 2014530.
(OR) like below
declare #Date VARCHAR(20)
select #Date = cast(Year('20140530') as varchar) + '-' +
cast(Month('20140530') as varchar) + '-' +
cast(Day('20140530') as varchar)
select cast(#Date as smalldatetime)
results in: 2014-05-30 00:00:00
Year()/Month()/DaY() functions returns the year/Month/Day as Integer. What you are actually doing can be simulated as below
declare #Date smalldatetime
set #Date = 2049
select #Date
which will result in : 1905-08-12 00:00:00
Use Like, Set will give the Date in #Date instead of select
declare #Date smalldatetime
set #Date = Year('20140530') + Month('20140530') + Day('20140530')
print #Date

Return Static date using SQL Function

I have a situation where i need to return a date.Here for this function i will be supplying month number and i need to return result like "3/13/2012".
declare #date varchar(20)
select #date=datepart(month,getdate())+'/13/'+datepart(year,getdate())
return #date(#date)
This should do it for ya.
CREATE FUNCTION dbo.fnStaticDate(#month varchar(2))
RETURNS DATETIME
AS
BEGIN
DECLARE #year VARCHAR(4)
SET #year = DATEPART(year, GETDATE())
RETURN CONVERT(DATETIME, #year + '-' + #month + '-' + '13')
END
Here is the working solution which i have used for one of my project.
created a store procedure with input parameter of month
declare #mon varchar(2)
set #mon = '3'
select CONVERT(varchar, #mon + '/13/' + convert(varchar, datepart(year, getdate())), 111 )
execute the above lines in SQL server you will get the result.
test by changing the #mon value in set statement.
Hope it helps you.
conversion error it gives
declare #date varchar(20)
select #date=convert(varchar(2),datepart(month,getdate()))+'/13/'+convert(varchar(4),datepart(year,ge tdate()))
print (#date)