I've experienced today strange issue. One of my projects is running .NET + SQL Server 2005 Express. There is one query I use for some filtering.
SELECT *
FROM [myTable]
where UI = 2011040773395012950010370
GO
SELECT *
FROM [myTable]
where UI = '2011040773395012950010370'
GO
UI column is nvarchar(256)
and UI value passed to filter is always 25 digits.
On my DEV environment - both queries return same row and no errors. However at my customers, after few months of running fine, first version started to return type conversion error.
Any idea why?
I'm not looking for solution - I'm looking for explanation why on one environment it works and on other doesn't and why out of sudden it started to return errors instead of results. I'm using same tools on both (SQL Server Management Studio Express and 2 different .NET clients)
Environments are more or less the same (W2k3 + SQL Server 2005 Express)
This is completely predictable and expected because of Datatype precedence
For this, the UI column will be changed to decimal(25,0)
This one is almost correct. The right hand side is varchar and is changed to nvarchar
This is the really correct version where both types are the same
Errors will have started because the UI column now contains a value that won't CAST to decimal(25,0).
Some unrelated notes:
char(25)
ornchar(25)
is values are always fixed length? Your queries use too much memory as the optimiser assumes an average length of 128 characters based onnvarchar(256)
Edit, after comment
Don't assume "why does it works sometimes" when you don't know that it does work
Examples:
Edit 2 for hopefully more clarity
Chat
gbn:
Random:
gbn
As Tao mentions, it's important to understand that another unrelated can break the query even if this one is OK.