SQL query Date Sort Problem
-
I have a simple SQL table as follows:
CREATE TABLE [dbo].[DaysMonth](
[SNo] [int] NOT NULL,
[Month] [varchar](20) NOT NULL,
[Year] [varchar](4) NOT NULL,
[NoOfDays] [int] NOT NULL,
[Days] [nchar](10) NULL,
[Holiday] [nchar](1) NULL,
[ToDate] [date] NULL,
[Period] [nchar](30) NULL,Following is the query:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop. using SQL Server 2008. basic purpose here is to get all days within a period, say January,2012 in a sorted manner. Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th. Getting puzzled, as it's a huge application running on multi-user environment, getting very difficult to take care of error which is absolutely unpredictable and illogical. Please suggest where is the problem, thanks to all in advance.
Did you try:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate ASC
Perhaps the direct order of sorting with "ASC" could help.
AnirbanM 2 wrote:
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop
Can't you let the SQL-Statement do the work of the loop? It would be more effective!
------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.
-
I have a simple SQL table as follows:
CREATE TABLE [dbo].[DaysMonth](
[SNo] [int] NOT NULL,
[Month] [varchar](20) NOT NULL,
[Year] [varchar](4) NOT NULL,
[NoOfDays] [int] NOT NULL,
[Days] [nchar](10) NULL,
[Holiday] [nchar](1) NULL,
[ToDate] [date] NULL,
[Period] [nchar](30) NULL,Following is the query:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop. using SQL Server 2008. basic purpose here is to get all days within a period, say January,2012 in a sorted manner. Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th. Getting puzzled, as it's a huge application running on multi-user environment, getting very difficult to take care of error which is absolutely unpredictable and illogical. Please suggest where is the problem, thanks to all in advance.
AnirbanM 2 wrote:
Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th.
From that, I'd say you have different collations (or regional settings on the client). But AFAIK, that would only apply to string-types, like
VARCHAR
andNTEXT
. Try converting the datetime to a fixed-format string, and sort on that.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
I have a simple SQL table as follows:
CREATE TABLE [dbo].[DaysMonth](
[SNo] [int] NOT NULL,
[Month] [varchar](20) NOT NULL,
[Year] [varchar](4) NOT NULL,
[NoOfDays] [int] NOT NULL,
[Days] [nchar](10) NULL,
[Holiday] [nchar](1) NULL,
[ToDate] [date] NULL,
[Period] [nchar](30) NULL,Following is the query:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop. using SQL Server 2008. basic purpose here is to get all days within a period, say January,2012 in a sorted manner. Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th. Getting puzzled, as it's a huge application running on multi-user environment, getting very difficult to take care of error which is absolutely unpredictable and illogical. Please suggest where is the problem, thanks to all in advance.
I notice that your table has Month, Year, Days all as character. If the sort uses these columns it might produce your problem.
-
I have a simple SQL table as follows:
CREATE TABLE [dbo].[DaysMonth](
[SNo] [int] NOT NULL,
[Month] [varchar](20) NOT NULL,
[Year] [varchar](4) NOT NULL,
[NoOfDays] [int] NOT NULL,
[Days] [nchar](10) NULL,
[Holiday] [nchar](1) NULL,
[ToDate] [date] NULL,
[Period] [nchar](30) NULL,Following is the query:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop. using SQL Server 2008. basic purpose here is to get all days within a period, say January,2012 in a sorted manner. Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th. Getting puzzled, as it's a huge application running on multi-user environment, getting very difficult to take care of error which is absolutely unpredictable and illogical. Please suggest where is the problem, thanks to all in advance.
Always always always store date/time related values as DATE, DATETIME, etc.; never as string.
-
Always always always store date/time related values as DATE, DATETIME, etc.; never as string.
His ToDate is stored as a Date type, first thing I looked for!
Never underestimate the power of human stupidity RAH
-
His ToDate is stored as a Date type, first thing I looked for!
Never underestimate the power of human stupidity RAH
Sure, but Period isn't.
-
Sure, but Period isn't.
And yet his OrderBy is Todate
Never underestimate the power of human stupidity RAH
-
I have a simple SQL table as follows:
CREATE TABLE [dbo].[DaysMonth](
[SNo] [int] NOT NULL,
[Month] [varchar](20) NOT NULL,
[Year] [varchar](4) NOT NULL,
[NoOfDays] [int] NOT NULL,
[Days] [nchar](10) NULL,
[Holiday] [nchar](1) NULL,
[ToDate] [date] NULL,
[Period] [nchar](30) NULL,Following is the query:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop. using SQL Server 2008. basic purpose here is to get all days within a period, say January,2012 in a sorted manner. Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th. Getting puzzled, as it's a huge application running on multi-user environment, getting very difficult to take care of error which is absolutely unpredictable and illogical. Please suggest where is the problem, thanks to all in advance.
There are a bunch of uglies in this table design 1. Storing numerics as character 2. Mixing varchar and nchar for no obvious reason 3. Using nchar instead of char (I would use varchar) 4. Using nchar instead of a bit (holiday) Personally I think you should have only 3 fields in the table SNo,ToDate and Holiday. All the others should be calculated in a view!
Never underestimate the power of human stupidity RAH
-
There are a bunch of uglies in this table design 1. Storing numerics as character 2. Mixing varchar and nchar for no obvious reason 3. Using nchar instead of char (I would use varchar) 4. Using nchar instead of a bit (holiday) Personally I think you should have only 3 fields in the table SNo,ToDate and Holiday. All the others should be calculated in a view!
Never underestimate the power of human stupidity RAH
10!
-
And yet his OrderBy is Todate
Never underestimate the power of human stupidity RAH
Which is an all-you-can-eat sushi place -- very fishy indeed... :suss:
-
Did you try:
Select SNo, NoOfDays, [Month], [Year], [Days], [Holiday] From DaysMonth Where [Period]='November,2012' order by ToDate ASC
Perhaps the direct order of sorting with "ASC" could help.
AnirbanM 2 wrote:
Now, i'm using this query in web application using ASP.Net environment and C# language within a loop
Can't you let the SQL-Statement do the work of the loop? It would be more effective!
------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.
Thanks, I will try but as sort is set by default as ASC in SQL server, I never put asc separately.
-
AnirbanM 2 wrote:
Out of 100 times running this query, 97-98 times, it's producing correct result, however, 1-2 times, sort is not working, so instead of 1st to 31st, output coming like 10th to 31st then 1st to 10th.
From that, I'd say you have different collations (or regional settings on the client). But AFAIK, that would only apply to string-types, like
VARCHAR
andNTEXT
. Try converting the datetime to a fixed-format string, and sort on that.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
Thanks for your reply. In SQL server, regional setting is set as default. Moreover, I need to do sorting based on Date only otherwise, it will defeat my purpose. However, as you are saying, i will try to do so.
-
I notice that your table has Month, Year, Days all as character. If the sort uses these columns it might produce your problem.
Thanks for your answer, but as you can see in the query, sort is being done only in date column not in any other varchar column.
-
Always always always store date/time related values as DATE, DATETIME, etc.; never as string.
Thanks, but sort is based on Date column only. As you rightly said, I always keep date as date column only. Month, Year, NoofDays etc, these columns are meant to be varchar, these are not included for sorting.
-
Which is an all-you-can-eat sushi place -- very fishy indeed... :suss:
Thanks for your reply, however, i'm not getting your point, if i'm sorting based on date and other fields are characters, what's the problem? If anything there, i will surely change it.
-
There are a bunch of uglies in this table design 1. Storing numerics as character 2. Mixing varchar and nchar for no obvious reason 3. Using nchar instead of char (I would use varchar) 4. Using nchar instead of a bit (holiday) Personally I think you should have only 3 fields in the table SNo,ToDate and Holiday. All the others should be calculated in a view!
Never underestimate the power of human stupidity RAH
Thanks for your valuable feedback. I will change all nchar to varchar and holiday to bit. Moreover, I can safely remove Month and Date. But as you are suggesting, rest of the fields are calculative, then I need to make query then construct data table, make calculation and then put it into grid, otherwise, i'm simply getting all values from sql and putting straightaway into grid. So I made a trade off here, though it may not be the best way to do so.
-
There are a bunch of uglies in this table design 1. Storing numerics as character 2. Mixing varchar and nchar for no obvious reason 3. Using nchar instead of char (I would use varchar) 4. Using nchar instead of a bit (holiday) Personally I think you should have only 3 fields in the table SNo,ToDate and Holiday. All the others should be calculated in a view!
Never underestimate the power of human stupidity RAH
What I can't understand and one of the puzzle is why SQL Server is not consistent and giving different results based on same query? Why out of 100 times, 97-98 times giving correct results and 1-2 times giving wrong.
-
I notice that your table has Month, Year, Days all as character. If the sort uses these columns it might produce your problem.
Thanks for varchar columns are not included in sorting.
-
What I can't understand and one of the puzzle is why SQL Server is not consistent and giving different results based on same query? Why out of 100 times, 97-98 times giving correct results and 1-2 times giving wrong.
Please note I did not offer ANY help on that subject - it does not make any sense to me so I concentrated on the other areas hoping they may have an impact on your results. To me a query cannot return inconsistent results!
Never underestimate the power of human stupidity RAH
-
Thanks for your reply. In SQL server, regional setting is set as default. Moreover, I need to do sorting based on Date only otherwise, it will defeat my purpose. However, as you are saying, i will try to do so.
I got a gut-feeling saying it's converting to
DATE
to aVARCHAR
, that's the only way to have 10 precede the number 2. Contrary to the previous advice, what happens when you cast it to aDOUBLE
? Internally, a date is a floating-point number.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]