patternsqlMinor
Records greater than epoch timestamp using only LIKE operator
Viewed 0 times
epochoperatorgreaterrecordsthanlikeusingtimestamponly
Problem
I have the following query so far and unfortunately, I cannot use regexp or greater than operators, I can only use the
The whole column is in a json string, I can't use
I would like to retrieve all records where the epoch unix timestamp is greater than 1645290000 using only the SQL
Any help will be much appreciated since this is a very tough unique case where I am limited to using only the
Sample table/data below:
There could be a thousand rows with this sort of data and the only ones I want are the ones where the update_date is greater than 1645290000.
Running the query on the above table I gave should only return the first row since the update_date of 2855290000 is indeed greater than 1645290000 numerically.
LIKE keyword.The whole column is in a json string, I can't use
json_value or regexp because I'm on SQL Server so I'm stuck with using LIKE. It's SQL Server 2014 and json_value is not supported until 2016.SELECT * FROM DataTableOne
WHERE update_date LIKE '%1645290000%'I would like to retrieve all records where the epoch unix timestamp is greater than 1645290000 using only the SQL
LIKE keyword (or even between 1645290000 and 9999999999 using the SQL LIKE operator).Any help will be much appreciated since this is a very tough unique case where I am limited to using only the
LIKE keyword.Sample table/data below:
CREATE TABLE DataTableOne (
ID int,
DATA varchar(MAX)
);
INSERT INTO DataTableOne (ID, DATA)
VALUES (1, '{"name":"Cole", "update_date":"2855290000"}'),
(2, '{"name":"Peter", "update_date":"1222290000"}') ;There could be a thousand rows with this sort of data and the only ones I want are the ones where the update_date is greater than 1645290000.
Running the query on the above table I gave should only return the first row since the update_date of 2855290000 is indeed greater than 1645290000 numerically.
Solution
I believe it is best to separate the problem into two parts, locate the update_date and then use ">" to filter:
Note that if there are malformed JSON in your table the query will fail. If that is the case you may want to encapsulate the extraction in a function / procedure with error handling.
Fiddle
Or, since
Updated Fiddle
select * from DataTableOne
where cast ( substring( data
, charindex('"update_date":', data)+15
, len(data)- (charindex('"update_date":', data)+15) -1 )
as bigint ) > 1645290000Note that if there are malformed JSON in your table the query will fail. If that is the case you may want to encapsulate the extraction in a function / procedure with error handling.
Fiddle
Or, since
TRY_CAST is implemented in SQL Server 2014, if the cast fails, null is returned which never satisfies >, so we can simply do:select * from DataTableOne
where TRY_CAST ( substring( data
, charindex('"update_date":', data)+15
, len(data)- (charindex('"update_date":', data)+15) -1 )
as bigint ) > 1645290000;Updated Fiddle
Code Snippets
select * from DataTableOne
where cast ( substring( data
, charindex('"update_date":', data)+15
, len(data)- (charindex('"update_date":', data)+15) -1 )
as bigint ) > 1645290000select * from DataTableOne
where TRY_CAST ( substring( data
, charindex('"update_date":', data)+15
, len(data)- (charindex('"update_date":', data)+15) -1 )
as bigint ) > 1645290000;Context
StackExchange Database Administrators Q#313750, answer score: 9
Revisions (0)
No revisions yet.