Sorting Interview Questions
Part - 17
CREATE TABLE Numeric_table (ID Varchar(20)) -- Drop table Numeric_table
INSERT Numeric_table VALUES (0), (-1), (-2), (1), (2),(3)
SELECT * FROM Numeric_table
SELECT * FROM Numeric_table ORDER BY ID ASC
SELECT * FROM Numeric_table ORDER BY CAST(ID AS INT) ASC
It implicitly converts to INT and compares with 0
It sorts -1 first and then -2 which is wrong sorting
Here the data format is MM-DD-YYYY and is defined as text data type.
It will sort wrongly if its defined as text data type. So, we need to explicitly cast/ convert to date data type.
Please Like and Subscribe :)
No comments:
Post a Comment