September 12, 2017 at 12:59 pm
Hello, I've got a Teradata Case statement, and I'm trying to convert it to a T-SQL Case statement, and I keep getting an invalid data type operator being used.
Here is the Teradata:
CASE
WHEN actvt_dt BETWEEN '2015-08-14' AND '2015-09-10'
THEN actvt_dt - CAST('2015-08-14' AS DATE) + 1
Here is one of the variations I've made, and I keep getting the error: Operand data type date is invalid for subtract operator.
, CASE
WHEN actvt_dt BETWEEN '2015-08-14' AND '2015-09-10'
THEN actvt_dt - cast('2015-08-14' as DATE) -+ 1
The actvt_dt is a 'date' data type in SQL Server
Any ideas on whatelse I can try?
Thanks
September 12, 2017 at 1:40 pm
Like this, perhaps?
DECLARE @actvt_dt DATE = '20150815';
SELECT CASE
WHEN @actvt_dt BETWEEN '20150814' AND '20150910' THEN
DATEDIFF(DAY, '20150814', @actvt_dt) + 1
END;
September 12, 2017 at 1:45 pm
Phil Parkin - Tuesday, September 12, 2017 1:40 PMLike this, perhaps?
DECLARE @actvt_dt DATE = '20150815';
SELECT CASE
WHEN @actvt_dt BETWEEN '20150814' AND '20150910' THEN
DATEDIFF(DAY, '20150814', @actvt_dt) + 1
END;
Yes, but why have to declare to hold that date value when I could just hard code that set period date?
September 12, 2017 at 1:57 pm
quinn.jay - Tuesday, September 12, 2017 1:45 PMPhil Parkin - Tuesday, September 12, 2017 1:40 PMLike this, perhaps?
DECLARE @actvt_dt DATE = '20150815';
SELECT CASE
WHEN @actvt_dt BETWEEN '20150814' AND '20150910' THEN
DATEDIFF(DAY, '20150814', @actvt_dt) + 1
END;Yes, but why have to declare to hold that date value when I could just hard code that set period date?
I had to hard-code it to make the example self-contained and runnable. You, of course, do not!
September 12, 2017 at 2:32 pm
Phil Parkin - Tuesday, September 12, 2017 1:57 PMquinn.jay - Tuesday, September 12, 2017 1:45 PMPhil Parkin - Tuesday, September 12, 2017 1:40 PMLike this, perhaps?
DECLARE @actvt_dt DATE = '20150815';
SELECT CASE
WHEN @actvt_dt BETWEEN '20150814' AND '20150910' THEN
DATEDIFF(DAY, '20150814', @actvt_dt) + 1
END;Yes, but why have to declare to hold that date value when I could just hard code that set period date?
I had to hard-code it to make the example self-contained and runnable. You, of course, do not!
Ok, I like using the declare, however now I'm trying to create a view, and I can't declare.
September 13, 2017 at 12:55 pm
quinn.jay - Tuesday, September 12, 2017 2:32 PMPhil Parkin - Tuesday, September 12, 2017 1:57 PMquinn.jay - Tuesday, September 12, 2017 1:45 PMPhil Parkin - Tuesday, September 12, 2017 1:40 PMLike this, perhaps?
DECLARE @actvt_dt DATE = '20150815';
SELECT CASE
WHEN @actvt_dt BETWEEN '20150814' AND '20150910' THEN
DATEDIFF(DAY, '20150814', @actvt_dt) + 1
END;Yes, but why have to declare to hold that date value when I could just hard code that set period date?
I had to hard-code it to make the example self-contained and runnable. You, of course, do not!
Ok, I like using the declare, however now I'm trying to create a view, and I can't declare.
That's what stored procedures are for, but without more information on why you were choosing a view, it might be better as an ITVF (in-line table-valued function), but there's nothing in your post to help make that decision. What's your overall objective?
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
September 14, 2017 at 2:34 am
The suggested code is correct. When performing atrihmetics on DATE in Teradata - in this case subtracting the dates from each other - the result will be the difference expressed in number of days.
Teradata doesn't do the same thing when subtracting or adding an integer to/from a DATE (assuming INTERVAL DAY). Here the result is a new DATE.
So the clostes thing in SQL Server is DATEDIFF as correctly proposed by Phil.
Maybe this example makes it easier to understand
DECLARE @actvt_dt TABLE
(actvt_dt DATE)
INSERT INTO @actvt_dt
VALUES ('2015-01-01'),
('2015-08-14'),
('2015-08-16'),
('2015-09-01'),
('2015-10-14')
SELECT
CASE
WHEN actvt_dt BETWEEN '2015-08-14' AND '2015-09-10'
THEN DATEDIFF(DAY, CAST('2015-08-14' AS DATE), actvt_dt) + 1
END
FROM @actvt_dt
September 17, 2017 at 8:38 am
From TeraData SQL assist, you get 7 as result from the query.
In TSQL, you may able to do the following
declare @actvt_dt date = '2015-08-22'
SELECT CASE WHEN @actvt_dt BETWEEN '2015-08-14' AND '2015-09-10'
THEN ABS(DATEDIFF(DAY,@actvt_dt,DATEADD(DAY,1, '2015-08-14')))
END AS 'test'
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy