I am trying to produce a varchar(20) which is 4 character year + 2 character month. I would like to then use this as a parameter in my stored procedure. Im unable to concatenate 2 varchars together to produce this, any ideas?
SELECT
GETDATE() AS CurrentDateTime,
CONVERT(VARCHAR(20), YEAR(GETDATE())) AS CurrentYear,
CONVERT(VARCHAR(20), MONTH(GETDATE())) AS CurrentMonth,
CurrentYear + CurrentMonth AS YearMonth
The simplest approach would be to use this:
SELECT Format(GetDate(), 'yyyyMM') as YearMonth
The function Format() was introduced with Sql Server 2012.
why not simply
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, Convert(varchar(6), GETDATE(), 112) AS YearMonth
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) +
Convert(VARCHAR(20),MONTH(GETDATE()))
Column aliases can't be selected from, so the above would do.
Like this one?
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, Convert(VARCHAR(20),YEAR(GETDATE())) + Convert(VARCHAR(20),MONTH(GETDATE())) AS YearMonth
Also you can
Year(GetDate()) * 100 + Month(GateDate()) AS YearMonth
or use construction which offered by users below
Convert(varchar(6), GETDATE(), 112) AS YearMonth
Related
am working in SQL Server 2008, while merging I got error like
conversion failed when converting datetime from character string
select *
from table_name
where cast(f_datetime as date) <=
cast(cast(datepart(year,cast(convert(varchar(250),#Year,103) as date) )as varchar(250))+ '-'+ cast(datepart(MM,cast(convert(varchar(10),#month,103) as varchar(50))+'-01' as date)
I cannot speak to the cast() on f_datetime. But for the rest, you can do:
where cast(f_datetime as date) <= convert(date, convert(varchar(250), #year * 10000 + #month * 100 + 1))
This simplifies the calculation, and prevents things like #year from being treated as a date due to the convert() function.
I assume your f_datetime field format is "dd/mm/yyyy". If yes you can easily convert this field instead of trying to merge and convert #year and #month fields. check this query :
SELECT *
FROM table_name
WHERE CONVERT(DATE,f_datetime,103)<= CAST(CONVERT(VARCHAR, #year) + '-' + CONVERT(VARCHAR, #month) + '-' + '01' AS DATE)
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')
hi i wrote this query in SqlServer 2008
but some thing goes wrong
select * from News_Table
where (DATEDIFF( DAY ,convert(datetime, NewsDate) , convert(datetime,#Todaydate )) <= #Count)
that #NewsDate and #Todaydate are two nvarchar parameters that are saved like this 2014/11/16
running this query give me an error:
Conversion failed when converting date and/or time from character string
Try adding the correct style parameter to your convert function (see MSDN: link )
ie CONVERT(DATETIME, NewsDate, 111) (111 is the style for YYYY/MM/DD)
Then you get:
SELECT *
FROM News_Table
WHERE (DATEDIFF( DAY ,
CONVERT(DATETIME, NewsDate, 111) ,
CONVERT(DATETIME,#Todaydate, 111)
) <= #Count)
use Convert(datetime, #yourvalue, 111)
select * from News_Table
where (DATEDIFF( DAY ,convert(datetime, #NewsDate, 111) , convert(datetime,#Todaydate, 111 )) <= #Count)
http://www.sqlusa.com/bestpractices/datetimeconversion/
To know more click here
SELECT convert(datetime, '2014/11/16', 111) as datetime
OP
So your query would be like this
Select * from News_Table
where (DATEDIFF( DAY ,convert(datetime, '2014/11/16', 111) , convert(datetime,#Todaydate,111 )) <= #Count)
Try like this
SELECT *
FROM News_Table
WHERE (DATEDIFF(DAY,CAST(NewsDate AS Datetime),CAST(#Todaydate AS Datetime)) <= #Count)
You will need to do something like this to convert that string into DATETIME datatype
DECLARE #Date NVARCHAR(20) = '2013/11/16'
SELECT CAST((LEFT(#Date, 4) + SUBSTRING(#Date, 6 ,2) + RIGHT(#Date, 2)) AS DATETIME)
for your query
select * from News_Table
where (DATEDIFF( DAY , CAST((LEFT(NewsDate, 4) + SUBSTRING(NewsDate, 6 ,2) + RIGHT(NewsDate, 2)) AS DATETIME)
, CAST((LEFT(#Todaydate, 4) + SUBSTRING(#Todaydate, 6 ,2) + RIGHT(#Todaydate, 2)) AS DATETIME)
) <= #Count)
Note
If variable #Todaydate is actually storing today's date then why not use simply GETDATE() function.
How can I get the month number in sql? I use the following code but it returns the month name.
SELECT DATENAME(mm, GETDATE())
Use datepart function with m extension.
SELECT DATEPART(m, getdate())
Use the month function - SELECT MONTH(GETDATE())
Use Datepart:
DATEPART(mm,getdate());
You want DATEPART:
select datepart(mm, getdate())
You can also use this to pad the month number
SELECT RIGHT('00' + RTRIM( CAST( DATEPART( MONTH, GETDATE() ) AS varchar(2)) ) , 2)
Try the below:
SELECT DATEPART(mm,getdate())
This will return with two char in case of Jan-Sep:
SELECT CASE WHEN LEN(MONTH(GETDATE())) = 1 THEN '0' + CAST(MONTH(GETDATE()) AS VARCHAR(2))
WHEN LEN(MONTH(GETDATE())) = 2 THEN CAST(MONTH(GETDATE()) AS VARCHAR(2)) END
We can use the SQL Function: MONTH(DATE) as a parameter. It will return the month number.
Trying to construct a date:
CAST('9/1/' + YEAR(GETDATE()) AS Datetime) AS test2
But it doesnt work?
Would like to get something like '9/1/2010'?
you can't concatenate the string '9/1' with the number: YEAR(GETDATE()), so try this:
select CAST('9/1/' + CONVERT(varchar(4),YEAR(GETDATE())) AS Datetime) AS test2
SELECT
CAST( '9/1/' + CAST( YEAR(GETDATE()) AS VARCHAR ) AS Datetime) AS test2
You need to cast the YEAR (integer) to a VARCHAR before you can append it.
try this:
Select DateAdd(month,
dateDiff(month, 0, getdate()) + 9 - MONTH(getdate()),
0)
You can use a string formatted YYYYMMDD.