January 3, 2024 at 9:47 pm
Hi
I have a field with a start and end date (sometimes no end date) .
How can I parse this out to have two fields start and end dates?
Data looks like
~__2019121407025301GMTOFFSET=-18000~__2019121415164202GMTOFFSET=-18000~
desired out is
Start Date 12/14/2019 7:02 AM
End Date 12/14/2019 3:16 PM
Thanks
January 3, 2024 at 11:49 pm
So you're ok with the loss of information assuming GMTOFFSET means what it appears to mean? In SQL Server if you need to preserve the same information then you'd be looking to CONVERT to DATETIME2(7) or DATETIMEOFFSET(7), yes? Is this really SQL Serve 2012? There really shouldn't be any instances anymore because it's waay out of official support
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
January 4, 2024 at 12:00 am
Hi Steve,
Thanks for getting back. I was given this table and figured out that this field a varchar(4000) was the field with the start and end dates times. I know the first part between '~__' and GMTOFFSET=-18000 is the start and same thing for the second part as the end date
January 4, 2024 at 4:09 am
Hi Steve,
Thanks for getting back. I was given this table and figured out that this field a varchar(4000) was the field with the start and end dates times. I know the first part between '~__' and GMTOFFSET=-18000 is the start and same thing for the second part as the end date
Yep... Steve knows that, as well. What he's saying is that your expected returns are not the correct dates and times according to the offsets that are also provided.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 4, 2024 at 4:05 pm
DROP TABLE IF EXISTS #data;
CREATE TABLE #data ( data varchar(4000) NULL );
INSERT INTO #data VALUES
('~__2019121407025301GMTOFFSET=-18000~__2019121415164202GMTOFFSET=-18000~')
SELECT data,
DATEADD(MINUTE, CAST(SUBSTRING(data, date1_start + 8, 2) AS int) * 60 +
CAST(SUBSTRING(data, date1_start + 10, 2) AS int),
CAST(SUBSTRING(data, date1_start, 8) AS datetime)) AS date1,
DATEADD(MINUTE, CAST(SUBSTRING(data, date2_start + 8, 2) AS int) * 60 +
CAST(SUBSTRING(data, date2_start + 10, 2) AS int),
CAST(SUBSTRING(data, date2_start, 8) AS datetime)) AS date2
FROM #data
CROSS APPLY (
SELECT PATINDEX('%[2][0][0-9][0-9][01][0-9][0123][0-9][01][0-9][0-5][0-9]%', data) AS date1_start
) AS ca1
CROSS APPLY (
SELECT PATINDEX('%[2][0][0-9][0-9][01][0-9][0123][0-9][01][0-9][0-5][0-9]%', SUBSTRING(data, date1_start + 12, 4000)) + date1_start + 12 - 1 AS date2_start
) AS ca2
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 4, 2024 at 8:21 pm
Hi Scott,
Thank You, that's exactly what I needed.
The only issue I have now is, I looked thru the entire table(500k+ records) , since I got a error, and discovered that there can be up to 22 start and end dates in the field...
January 4, 2024 at 8:53 pm
The code will just get the first two numbers, which might cause an "error" of bad data if the first two numbers don't have the datetimes you need. Otherwise, I wouldn't think you should get an "error".
If you need something other than the first two datetime values, are there some chars that would allow code to determine which numbers to pull?
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 4, 2024 at 9:16 pm
Hmmm,
The data seems to be all over the place ,but it would be the datetime between "~__ " and "GMTOFFSET=-18000"
even though it looks like there is a DPT number on some still between "~__ " and "GMTOFFSET=-18000"
Like this example
~__2019121322545101GMTOFFSET=-18000&DPT=205~__2019121408520502GMTOFFSET=-18000~__2019121409443301GMTOFFSET=-18000&DPT=242~__2019121416570802GMTOFFSET=-18000~
Where as most are
~__2019120422521801GMTOFFSET=-18000~__2019120507421602GMTOFFSET=-18000~
Then.. there are some that are NULL
January 5, 2024 at 7:06 pm
You really cannot just make assumptions about the data contained in that column. You need to ask the provider of that data on what the string contains and how it needs to be parsed.
I could assume that DPT means 'DEPARTMENT' - but that is just an assumption. It could mean something else - the only way to know for sure is to ask.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
January 5, 2024 at 7:11 pm
My original code gives a result for the new data, since I purposely wrote the original code generically:
~__2019121322545101GMTOFFSET=-18000&DPT=205~__2019121408520502GMTOFFSET=-18000~__2019121409443301GMTOFFSET=-18000&DPT=242~__2019121416570802GMTOFFSET=-18000~
2019-12-14 08:52:00.000
2019-12-14 09:44:00.000
If the first two values are not the correct ones, how specifically do I identify which values you do need? Is it the ones with "DPT" in them?? Keep in mind, we know NOTHING about your data (even less than you would, even if from another vendor).
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 6, 2024 at 3:04 pm
This was removed by the editor as SPAM
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply