Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL query Date Sort Problem

SQL query Date Sort Problem

Scheduled Pinned Locked Moved Database
csharpdatabasehelpasp-netsql-server
28 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AnirbanM 2

    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.

    C Offline
    C Offline
    Corporal Agarn
    wrote on last edited by
    #4

    I notice that your table has Month, Year, Days all as character. If the sort uses these columns it might produce your problem.

    A 2 Replies Last reply
    0
    • A AnirbanM 2

      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.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #5

      Always always always store date/time related values as DATE, DATETIME, etc.; never as string.

      M A 2 Replies Last reply
      0
      • P PIEBALDconsult

        Always always always store date/time related values as DATE, DATETIME, etc.; never as string.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #6

        His ToDate is stored as a Date type, first thing I looked for!

        Never underestimate the power of human stupidity RAH

        P 1 Reply Last reply
        0
        • M Mycroft Holmes

          His ToDate is stored as a Date type, first thing I looked for!

          Never underestimate the power of human stupidity RAH

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          Sure, but Period isn't.

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            Sure, but Period isn't.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #8

            And yet his OrderBy is Todate

            Never underestimate the power of human stupidity RAH

            P 1 Reply Last reply
            0
            • A AnirbanM 2

              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.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #9

              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

              P A 3 Replies Last reply
              0
              • M Mycroft Holmes

                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

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #10

                10!

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  And yet his OrderBy is Todate

                  Never underestimate the power of human stupidity RAH

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #11

                  Which is an all-you-can-eat sushi place -- very fishy indeed... :suss:

                  A 1 Reply Last reply
                  0
                  • I Ingo

                    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.

                    A Offline
                    A Offline
                    AnirbanM 2
                    wrote on last edited by
                    #12

                    Thanks, I will try but as sort is set by default as ASC in SQL server, I never put asc separately.

                    1 Reply Last reply
                    0
                    • L Lost User

                      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 and NTEXT. 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![^]

                      A Offline
                      A Offline
                      AnirbanM 2
                      wrote on last edited by
                      #13

                      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.

                      L 1 Reply Last reply
                      0
                      • C Corporal Agarn

                        I notice that your table has Month, Year, Days all as character. If the sort uses these columns it might produce your problem.

                        A Offline
                        A Offline
                        AnirbanM 2
                        wrote on last edited by
                        #14

                        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.

                        1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Always always always store date/time related values as DATE, DATETIME, etc.; never as string.

                          A Offline
                          A Offline
                          AnirbanM 2
                          wrote on last edited by
                          #15

                          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.

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            Which is an all-you-can-eat sushi place -- very fishy indeed... :suss:

                            A Offline
                            A Offline
                            AnirbanM 2
                            wrote on last edited by
                            #16

                            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.

                            1 Reply Last reply
                            0
                            • M Mycroft Holmes

                              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

                              A Offline
                              A Offline
                              AnirbanM 2
                              wrote on last edited by
                              #17

                              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.

                              1 Reply Last reply
                              0
                              • M Mycroft Holmes

                                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

                                A Offline
                                A Offline
                                AnirbanM 2
                                wrote on last edited by
                                #18

                                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.

                                M 1 Reply Last reply
                                0
                                • C Corporal Agarn

                                  I notice that your table has Month, Year, Days all as character. If the sort uses these columns it might produce your problem.

                                  A Offline
                                  A Offline
                                  AnirbanM 2
                                  wrote on last edited by
                                  #19

                                  Thanks for varchar columns are not included in sorting.

                                  1 Reply Last reply
                                  0
                                  • A AnirbanM 2

                                    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.

                                    M Offline
                                    M Offline
                                    Mycroft Holmes
                                    wrote on last edited by
                                    #20

                                    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

                                    A 2 Replies Last reply
                                    0
                                    • A AnirbanM 2

                                      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.

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #21

                                      I got a gut-feeling saying it's converting to DATE to a VARCHAR, that's the only way to have 10 precede the number 2. Contrary to the previous advice, what happens when you cast it to a DOUBLE? 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![^]

                                      1 Reply Last reply
                                      0
                                      • M Mycroft Holmes

                                        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

                                        A Offline
                                        A Offline
                                        AnirbanM 2
                                        wrote on last edited by
                                        #22

                                        Yes, that's my core question. even if my table design is not good, even if SQL may internally converting date into float or even if i need to put 'ASC' after Order By, still question remains same. Why on earth SQL query is not consistent? Why not it's producing same results 100 out of 100 times? Is that means any internal bug in the SQL Server? Thanks.

                                        1 Reply Last reply
                                        0
                                        • M Mycroft Holmes

                                          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

                                          A Offline
                                          A Offline
                                          AnirbanM 2
                                          wrote on last edited by
                                          #23

                                          Actually, based on the query results, within a loop creating new records and putting into a new SQL table 'Timesheet'. Now, when I see this new table 'Timesheet', it's rows are not created based on that sorting. For example, output of the query I got as 1/1/2012 to 1/31/2012 and now in the new table, inserting records like : 1/1/2012 - 51 - 12 1/2/2012 - 10 - 13 1/3/2012 - 15 - 20 ..... till 1/31/2012 - 40 - 30 Now, sometimes (as I said earlier), it's creating rows in 'Timesheet' table that looks like: 1/12/2012 - 10 -20 1/13/2012 - 15-12 .. then 1/31/2012 - 40 - 30 ..then 1/1/2012 - 51 - 12 1/2/2012 - 10 - 13 1/3/2012 - 15 - 20 It's not that all errors are starting from 1/12/2012, it can be 10th or 15th. So no possible pattern in these output which are giving error. May be it will give more light into this matter. thanks.

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups