How to concatenate declare date variables - sql

How do I concatenate declare date variables so they are in one column? I need to show the dates as between dates. When I run the following I get an error message.
Declare #startdate date = '20180101'
Declare #enddate date = '20180731'
SELECT
'Dates' = #startdate+' - '+#enddate
FROM TABLE
Error Message:
The data types date and varchar are incompatible in the add operator.

Convert them to strings before concatenating them. For the default format on your system:
select dates = convert(varchar(255), #startdate) + ' - ' + convert(varchar(255), #enddate)
To specifically convert to YYYYMMDD use format 112:
select dates = convert(varchar(255), #startdate, 112) + ' - ' + convert(varchar(255), #enddate, 112)

You can use concat() :
SELECT CONCAT(#startdate, ' - ', #enddate) AS Dates
FROM TABLE;

Related

Converting a FLOAT into DATE in SQL?

I wanted to know how to properly convert FLOAT value into a DATE?
The data we receive has a value oriented as such: YYYYMM ex. 201911 (today's Year + Month). However, all the values passing under the YYYYMM column signifies the first of the month ex. 201911 = 11/01/2019.
RIGHT([DATE],2) + '/01/' + LEFT([DATE],4) AS [DATE]
When I try converting it, it doesn't put it in a date format because I tried using it in a DATEADD function and it errored on the field I converted.
If your value is YYYYMM, then one simple method is to convert to a string and then a date:
select convert(date, convert(varchar(255), yyyymm) + '01')
Or, use datefromparts():
select datefromparts(floor(yyyymm / 100), yyyymm % 100, 1)
Please check this :
DECLARE #Date AS FLOAT;
SET #Date = 201911;
SELECT CONVERT(VARCHAR, CAST(CAST(LEFT(#Date,4) AS VARCHAR(4)) + '/01' + '/' + CAST(RIGHT(#Date,2) AS VARCHAR(2)) AS DATE) , 103) As CREATEDDATE
Will output 11/01/2019

Convert MonthName-Year to Year-MonthNumber

I have a custom date string in SQL. I need to convert the date into custom format. Below is the example for it.
SQL Input : 'January-2019' (Month Name-Year format)
Output : '201901' (Year-Month Number format, Month Number must be two digit)
Since you're using SQL 2008 you can use CONVERT(DATE, ...) + CONVERT(VARCHAR, ...):
DECLARE #input AS VARCHAR(20) = 'January-2019'
SELECT CONVERT(VARCHAR(6), CONVERT(DATE, '01-' + #input), 112)
Note that you need to prepend 01- to the string.
You can also try this.
DECLARE #input AS VARCHAR(20) = 'January-2019'
Select Convert(Varchar(6), Cast(Replace(#input, '-', ' 01 ') as DATE), 112)
Live Demo

Change date format dd/mm/yyyy to yyyy-mm-dd

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)

Datetime Format in Month and Year

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)

Combine two DateTimes

In SQL I have the following code fragment :
DECLARE
#DayPart as datetime,
#TimePart as datetime
SET #DayPart='2012-01-10 00:00:00.000'
SET #TimePart='2012-08-30 15:41:10.403'
Now I Need :
'2012-01-10 15:41:10.403'
How can I get it?
SELECT CONVERT(VARCHAR(10),#DayPart,111) + ' ' +
CONVERT(VARCHAR(10),#TimePart,108);
You should get #DayPart in 'yyyy-mm-dd' format and #TimePart in 'HH:MI:SS:MMM(24H)' format and concatenate two strings.
Try this
SELECT
CONVERT(char(10), #DayPart,126) + ' ' +
CONVERT(VARCHAR(12), #TimePart, 114)
More on SQL Server date formatting
SQL Server Date Formats
SQL2K8;
select #DayPart + cast(#TimePart as time)
SELECT REPLACE(CONVERT(VARCHAR(10),#DayPart,102),'.','-') + ' ' +
CONVERT(VARCHAR(10),#TimePart,108);
Other Date Formatting
But if you are using SQL Server 2008+
SELECT CONVERT(date, #DayPart) + ' ' + CONVERT(time, #TimePart)