Concatenate ints to date and inserting to DB in ssis - sql

I'm new to SSIS and have an issue with a inserting a date to my table with ssis.
this is what I'm trying to do:
select cast (cast(YEAR(getdate()) as nvarchar) + '-' + cast(#IndexmonthS as nvarchar) + '-' + cast(#day as nvarchar) as date) as startdate,
cast (cast(YEAR(getdate()) as nvarchar) + '-' + cast(#IndexmonthE as nvarchar) + '-' + cast(#day as nvarchar) as date) as enddate
when I copy it to the commend text in ssis and run the package I get an error -
"Error at insert to ... cannot convert between types "DT_DBDATE" and
"DT_14"..."
the destination table has a the column and its type is date.
please help.

First, never use varchar() or nvarchar() without a length parameter. In SQL Server, the default length varies by context and is just a bug waiting to happen.
In SQL Server 2012, you can use datefromparts(). Assuming the values are numerical:
select datefromparts(year(getdate()), #IndexmonthS, #day) as startdate,
Even in earlier versions, you should use datename() rather than datepart():
select cast(datename(year, getdate()) + '-' + cast(#IndexmonthS as varchar(255)) + '-' cast(#day as varchar(255)) as date)

Related

Two dates having same values failing when joined in SQL Server 2014 using CONVERT and DATEPART functions

I am facing a very weird problem in joining on two dates in SQL Server 2014. I have a condition like below in my where clause :-
SELECT
a.*, w.*
FROM
TABLE1 a, TABLE2 w
WHERE
CONVERT(varchar(8), ISNULL(a.Col1,999), 112) + ' ' + CONVERT(VARCHAR, DATEPART(hh, ISNULL(a.Col1,999))) + ':' + RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, ISNULL(a.Col1,999))), 2)
= CONVERT(varchar(8),ISNULL(w.Col2,999),112) + ' ' + CONVERT(VARCHAR, DATEPART(hh, ISNULL(w.Col2,999))) + ':' + RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, ISNULL(w.Col2,999))), 2)
Here Col1 in Table1 and Col2 in Table2 are of type VARCHAR(18).
When selected separately both using the CONVERT and DATEPART function, both return the same value but when used in a join condition it fails with below error:
ERROR encountered - 241Conversion failed when converting date and/or time from character string
Example of Col1 in Table1 data:- "20190227 17:20:40"
Any help is truly appreciated!!

SQL SERVER -- YEAR

Select (SELECT YEAR(GETDATE())+(Select('-'))+(SELECT YEAR(GETDATE())+1));
I want output as "2018-2019".
Anyone?
You can use + to append string since you are working with SQL 2008, higher version has CONCAT() function :
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) + ' - ' + CAST(YEAR(DATEADD(YEAR, 1, GETDATE())) AS VARCHAR(4))
Use datename():
select (datename(year, getdate()) + '-' + datename(year, dateadd(year, 1, getdate()))
That way, you don't have to deal with converting integers to strings.
SELECT CONCAT(YEAR(GETDATE()), '-', YEAR(GETDATE()) + 1)
CONCAT takes care of conversion to string, so you don't have to worry about that.
edit: Unfortunately I have been made aware that CONCAT is only available in sql server 2012 and higher.
You can also use
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(YEAR(GETDATE()) + 1 AS VARCHAR(4))
OR
SELECT CAST(DATEPART(Year,GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(DATEPART(Year, GETDATE()) + 1 AS VARCHAR(4))

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 losing hh:mm when changing CREATE TABLE to SELECT INTO

I am currently migrating all of my company's reports into Splunk Data Labs input for ingestion. The reports create temp tables using the CREATE TABLE format, which is incompatible with Splunk, however, SELECT INTO format works just fine.
The error that I am getting however when changing to the SELECT INTO format, is the DATETIME variable which should be MM/DD/YYYY hh:mm format loses the hh:mm end, and instead shows MM/DD/YYYY MM/DD/YYYY:
Original SQL:
CREATE TABLE #Stats#(date_slice DATETIME NULL, raw_value REAL NULL)
INSERT INTO #Stats#
SELECT CONVERT(CHAR(11), data_datetime, 111) + ' ' +
CASE WHEN DATEPART(MINUTE, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END AS date_hour
,SUM(ship_qty) AS moves
FROM #tmpAllData
GROUP BY CONVERT(CHAR(11), data_datetime, 111) + ' ' +
case when DATEPART(minute, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END
ORDER BY 1
Modified SQL:
--CREATE TABLE #Stats#(date_slice DATETIME NULL, raw_value REAL NULL)
SELECT CONVERT(CHAR(11), data_datetime, 111) + ' ' +
CASE WHEN DATEPART(MINUTE, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END date_slice
,SUM(ship_qty) raw_value
INTO #Stats#
FROM #tmpAllData
GROUP BY CONVERT(CHAR(11), data_datetime, 111) + ' ' +
case when DATEPART(minute, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END
ORDER BY 1
Original Output: "07/12/2018 10:00:00 "
Modified Output: "2018/07/12 2018/07/12"
You second statement is creating the temporary table #Stats without any preset column definitions. It instead creates the columns based on the return data type of the SELECT
This means that the SQL Server is not reading the output in question as a DATETIME but instead as a STR.
I would try to use a CONVERT in your modified statement to see if you get different functionality.
It sounds like this question is mostly for your curiosity so I will add that the original statement is the standard way to accomplish what you've outlined.
This is because SELECT... INTO statements are harder to read and revise for new users and because they can lead to some unexpected functionality as you have displayed above.
So, similar to what Edward had said. The query I had posted was returning a DATETIME data type, however, at the very end of the query when it selects all information to display to the user, it got converted to CHAR somewhere in between, so I just converted it to DATETIME before converting to CHAR and splitting up the values.

With SQL Server format date as '%Y-%m-%d %I:%M%P' / '2012-04-05 11:56am'

I've always found SQL Server date formatting to be counter intuitive. I am pretty sure I already know the only answer is to use a slew of convert and string functions but thought I would ask just in case.
How do you get SQL Server to format a datetime to look like:
%Y-%m-%d %I:%M%P or '2012-04-05 11:56am'
My current approach involves pulling back data with SELECT CONVERT(VARCHAR(19), GETDATE(), 120) and then looping server/client side in code. This I find inefficient in some scenarios when exporting data say to CSV.
DECLARE #d AS datetime;
SET #d = '2012-04-04 16:43:00'
SELECT LEFT(CONVERT(VARCHAR, #d, 120), 11)
+ RIGHT('0' + LTRIM(SUBSTRING(CONVERT(VARCHAR, #d, 131), 12, 5)), 5)
+ LOWER(RIGHT(CONVERT(VARCHAR, #d, 131), 2))
--2012-04-04 04:43pm
select
cast(datepart(YYYY, GETDATE()) as varchar) + '-' +
right('0' + cast(datepart(MM, GETDATE()) as varchar), 2) + '-' +
right('0' + cast(datepart(DD, GETDATE()) as varchar), 2) + ' ' +
LTRIM(RIGHT(cast(GETDATE() AS varchar), 8))
I think the real answer is "You don't", unless you upgrade your SQL Server instances to SQL Server 2012.
FORMAT (Transact-SQL)