March 28, 2018 at 6:22 am
My table holds values like 000.004.000, 000.02.000, 000.024.000. What is the best/easiest way to get 4H, 2H, or 24H respectively?
March 28, 2018 at 6:53 am
Got lazy and used DelimitedSplit8K
SELECT TextTime
, ca.Item
, ca.ItemNumber
FROM
(SELECT '000.004.000' AS TextTime
UNION ALL SELECT '000.02.000'
UNION ALL SELECT '000.024.000') x
CROSS APPLY Scratchpad.dbo.DelimitedSplit8K(x.TextTime,'.') ca
WHERE ca.ItemNumber = 2;
March 28, 2018 at 7:01 am
Another method, slightly more efficient
😎
WITH SAMPLE_DATA(TSTR) AS
(
SELECT '000.004.000' UNION ALL
SELECT '000.02.000' UNION ALL
SELECT '000.024.000'
)
SELECT
SD.TSTR
,CONVERT(INT,REPLACE(REPLACE(SD.TSTR,'000.',''),'.000',''),0) AS NUM_HOURS
FROM SAMPLE_DATA SD;
Output
TSTR NUM_HOURS
----------- -----------
000.004.000 4
000.02.000 2
000.024.000 24
March 28, 2018 at 7:01 am
Thanx but, I was hoping for something I can use as a field value.
March 28, 2018 at 7:02 am
NineIron - Wednesday, March 28, 2018 7:01 AMThanx but, I was hoping for something I can use as a field value.
Now you have to explain exactly what you want!
😎
March 28, 2018 at 7:03 am
NineIron - Wednesday, March 28, 2018 6:22 AMMy table holds values like 000.004.000, 000.02.000, 000.024.000. What is the best/easiest way to get 4H, 2H, or 24H respectively?
RIGHT 2 of LEFT(CHARINDEX second decimal.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
March 28, 2018 at 7:04 am
NineIron - Wednesday, March 28, 2018 7:01 AMThanx but, I was hoping for something I can use as a field value.
You should have said that in the first place. "Use as a field value" how? How about an example? "I have X, I need Y, so I can do Z with it"?
March 28, 2018 at 7:17 am
The OP will have to explain why 024 = 24 and 02 = 2 and not 20
😎
March 28, 2018 at 7:21 am
Thanx.
March 28, 2018 at 7:34 am
NineIron - Wednesday, March 28, 2018 6:22 AMMy table holds values like 000.004.000, 000.02.000, 000.024.000. What is the best/easiest way to get 4H, 2H, or 24H respectively?
If the data always has 3 parts separated by periods as you've indicated above, then this is simple...
SELECT PARSENAME(TextTime,2)+'H'
FROM dbo.YourTable
;
--Jeff Moden
Change is inevitable... Change for the better is not.
March 28, 2018 at 7:41 am
Nice. I'll use replace(PARSENAME(TextTime,2)+'H','0','') to remove the leading zero's but, this is perfect.
March 28, 2018 at 7:44 am
NineIron - Wednesday, March 28, 2018 7:41 AMNice. I'll use replace(PARSENAME(TextTime,2)+'H','0','') to remove the leading zero's but, this is perfect.
No... do no use REPLACE here because it will change things like 020 to just 2. Convert to an INT and then back to character based to add the "H".
--Jeff Moden
Change is inevitable... Change for the better is not.
March 28, 2018 at 7:46 am
k.
March 28, 2018 at 10:58 am
Jeff Moden - Wednesday, March 28, 2018 7:44 AMNineIron - Wednesday, March 28, 2018 7:41 AMNice. I'll use replace(PARSENAME(TextTime,2)+'H','0','') to remove the leading zero's but, this is perfect.No... do no use REPLACE here because it will change things like 020 to just 2. Convert to an INT and then back to character based to add the "H".
Or use the CONCAT function...DECLARE @TextTime VARCHAR(11) = '000.002.000';
SELECT CONCAT(CONVERT(INT, PARSENAME(@TextTime, 2)), 'H');
March 28, 2018 at 4:23 pm
Jason A. Long - Wednesday, March 28, 2018 10:58 AMJeff Moden - Wednesday, March 28, 2018 7:44 AMNineIron - Wednesday, March 28, 2018 7:41 AMNice. I'll use replace(PARSENAME(TextTime,2)+'H','0','') to remove the leading zero's but, this is perfect.No... do no use REPLACE here because it will change things like 020 to just 2. Convert to an INT and then back to character based to add the "H".
Or use the CONCAT function...
DECLARE @TextTime VARCHAR(11) = '000.002.000';
SELECT CONCAT(CONVERT(INT, PARSENAME(@TextTime, 2)), 'H');
Damn. I hate what MS does behind the scenes on these supposed improvements. Here's what PARSNAME and CONCAT renders out to. I'd forgotten that PARSNAME produces an NVARCHAR result.
[Expr1093] = Scalar Operator(concat(CONVERT_IMPLICIT(varchar(12),CONVERT(int,parsename(CONVERT_IMPLICIT(nvarchar(11),[@TextTime],0),(2)),0),0),'H'))
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply