September 18, 2008 at 8:54 am
I was wondering if there is a wildcard version of the following IN statement:
select top 10 * from dbo.MyTable
where ActualTime NOT IN
('0','00','000','0000')
There is a field in this table, dbo.MyTable, ActualTime which is a char(4). Instead of a datetime value, this table is fed the actual time as a seperate field. I was hoping to get a query like below so I do not have to repeat every possible "0" scenario as I did above.
select top 10 * from dbo.MyTable
where ActualTime NOT IN
('0*')
Thanks! 🙂
Argue for your limitations, and sure enough they're yours (Richard Bach, Illusions)
September 18, 2008 at 9:06 am
It's not a wildcard expression but you could use something like:
WHERE REPLACE(actualTime, '0', '') <> ''
or
WHERE CAST(actualTime AS INT) <> 0
The replace would result in any actualTime string containing only zeros to be a zero-length string. Although I have no idea on the performance of either of these on mass.
You might wish to consider changing the column definition to an INT and the check for anything that is not zero. I'm sure the performance of that would be better than any fuction performed in a WHERE clause.
September 19, 2008 at 4:27 am
Or use something like
select top 10 * from dbo.MyTable
where ActualTime NOT like '0%'
but this construction is a performance killer.
Wilfred
The best things in life are the simple things
September 19, 2008 at 7:55 am
I am not sure if this works
where ActualTime >0
Failing to plan is Planning to fail
September 19, 2008 at 8:35 am
Thanks for the suggestions all! 🙂
Doing a replace would not work, as any occurrences such as 0901 would become 9 1 (char data). But doing WHERE CAST(ActualTime AS INT) <> 0 will work.
Excluding any occurrences NOT like '0%' would skip 0901 (the char data is in military time).
The suggestion that works at the moment is the where ActualTime > '0000' (has to be 4 zeros to exlude 0, 00, 000 as well as 0000 - again, char data).
The downside is missing anything that midnight, which I never accounted for in my original post, but that is a problem for another day. Unfortunately midnight is 0000 and "no data" can also be represented by 0000, amoung other things (0,00,000 and blank).
😀
Argue for your limitations, and sure enough they're yours (Richard Bach, Illusions)
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply