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.
Related
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!!
I have a SQL query that is pulling data into excel. My VBA code reads in numbers from user inputs and writes the SQL code with these parameters in it. I can make the comparisons that I need like part number doing it this way, but I cannot get date comparisons to work. In the database I am pulling from is a date column and I have user inputs of day, month, and year.
Basically I need this to work:
WEEKENDING_DT >= DATE(2017,01,01)
But there is no date function in SQL. From my research I also found these ways to do it and none of them work.
WEEKENDING_DT >= (CAST(2017 AS VARCHAR(4)) + RIGHT('0' + CAST(1 AS VARCHAR(2)), 2) + RIGHT('0' + CAST(1 AS VARCHAR(2)), 2))
WEEKENDING_DT >= CAST(CAST(2017 AS VARCHAR(4)) + RIGHT('0' + CAST(1 AS VARCHAR(2)), 2) + RIGHT('0' + CAST(1 AS VARCHAR(2)), 2) AS DATETIME)
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)
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)
What is the DATE FORMAT CODE for "yyyy.mm.dd.hh.mm.ss"?
I know that 34 (date format code) is "yyyymmddhhmmss", but what about the code for "yyyy.mm.dd.hh.mm.ss"?
This is on SQL 2005.
CAST and CONVERT on MSDN says "no".
You have to CONVERT twice with styles 102 and 108, with a concatenation and REPLACE.
Where did you get the "34" date format code from?
As gbn said, using one of the existing formats with some string concatenation would work. Another option is:
SELECT
CAST(YEAR(my_date) AS CHAR(4)) + '.' +
RIGHT('0' + CAST(MONTH(my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DAY(my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(HOUR, my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(MINUTE, my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(SECOND, my_date) AS VARCHAR(2)), 2)
Considering
SELECT CONVERT(VARCHAR(16), GETDATE(), 120) AS [YYYY-MM-DD]
---returns--- yyyy-mm-dd hh:ss
and you can use REPLACE to convert strings
REPLACE('a b c',' ','.')
----returns--- a.b.c
and you can recursively stack things you get to this
Select (
replace((replace((replace(CONVERT(VARCHAR(16), GETDATE(), 120),' ','.')), ':', '.')), '-', '.')
)
which returns: yyyy.mm.dd.hh.ss
works great for datetime stamps or filenames!