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. Convert SQL datetime to just date

Convert SQL datetime to just date

Scheduled Pinned Locked Moved Database
databasequestion
8 Posts 4 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.
  • K Offline
    K Offline
    kozu
    wrote on last edited by
    #1

    Hi, I need to convert the DateTime to a date format. I know I have to use Convert, but I don't think I use it right because I'm not getting the expected result. Here's my scenarion: I want to do a sum for all products in the DB that were bought during a given period of time. My query looks like this select convert(datetime,convert(char(10),OrderDate,101)), SUM(Qty) from Orders where convert(datetime,convert(char(10),OrderDate,101)) between '05/29/05' and '06/10/05' group by convert(datetime,convert(char(10),OrderDate,101)) order by convert(datetime,convert(char(10),OrderDate,101)) the result looks like this: OrderDate Qty 2005-05-30 00:00:00.000 1 2005-05-31 00:00:00.000 4 How can I get rid of the 0s at the end of OrderDate? thanx

    K S M 3 Replies Last reply
    0
    • K kozu

      Hi, I need to convert the DateTime to a date format. I know I have to use Convert, but I don't think I use it right because I'm not getting the expected result. Here's my scenarion: I want to do a sum for all products in the DB that were bought during a given period of time. My query looks like this select convert(datetime,convert(char(10),OrderDate,101)), SUM(Qty) from Orders where convert(datetime,convert(char(10),OrderDate,101)) between '05/29/05' and '06/10/05' group by convert(datetime,convert(char(10),OrderDate,101)) order by convert(datetime,convert(char(10),OrderDate,101)) the result looks like this: OrderDate Qty 2005-05-30 00:00:00.000 1 2005-05-31 00:00:00.000 4 How can I get rid of the 0s at the end of OrderDate? thanx

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      You can change your where clause to be: where OrderDate >= '2005-05-29' and OrderDate <= '2005-06-10 23:59:59' Hope that helps. Ben

      K 1 Reply Last reply
      0
      • K kubben

        You can change your where clause to be: where OrderDate >= '2005-05-29' and OrderDate <= '2005-06-10 23:59:59' Hope that helps. Ben

        K Offline
        K Offline
        kozu
        wrote on last edited by
        #3

        Kubben, Thanks for the reply, but changing the where clause didn't work. I need to change the 'convert' part of the select in order to alter how the data is being displayed, but to what? I tried casting it to char, and it partially fixed my problem: select cast(OrderDate as char(11)), SUM(Qty) from Orders where convert(datetime,convert(char(10),OrderDate,101)) between '05/29/05' and '06/10/05' group by cast(OrderDate as char(11)) order by cast(OrderDate as char(11)) The result is: Jun 1 2005 2 Jun 2 2005 1 May 30 2005 1 but now the problem is that the ordering will be done alphabetically and not by date. stuck again :(

        1 Reply Last reply
        0
        • K kozu

          Hi, I need to convert the DateTime to a date format. I know I have to use Convert, but I don't think I use it right because I'm not getting the expected result. Here's my scenarion: I want to do a sum for all products in the DB that were bought during a given period of time. My query looks like this select convert(datetime,convert(char(10),OrderDate,101)), SUM(Qty) from Orders where convert(datetime,convert(char(10),OrderDate,101)) between '05/29/05' and '06/10/05' group by convert(datetime,convert(char(10),OrderDate,101)) order by convert(datetime,convert(char(10),OrderDate,101)) the result looks like this: OrderDate Qty 2005-05-30 00:00:00.000 1 2005-05-31 00:00:00.000 4 How can I get rid of the 0s at the end of OrderDate? thanx

          S Offline
          S Offline
          Shahil Shah
          wrote on last edited by
          #4

          Hi If you are viewing the data in a datagrid you can use the following data formating expression: {0:dd/MM/yyyy} without the need to use the convert staement you are using otherwise try this: SET DATEFORMAT mdy select CAST(OrderDate as varchar(11)) as OrderDate, SUM(Qty) where OrderDate between '05/29/05' and '06/10/05' group by OrderDate order by OrderDate Shahil

          K 1 Reply Last reply
          0
          • S Shahil Shah

            Hi If you are viewing the data in a datagrid you can use the following data formating expression: {0:dd/MM/yyyy} without the need to use the convert staement you are using otherwise try this: SET DATEFORMAT mdy select CAST(OrderDate as varchar(11)) as OrderDate, SUM(Qty) where OrderDate between '05/29/05' and '06/10/05' group by OrderDate order by OrderDate Shahil

            K Offline
            K Offline
            kozu
            wrote on last edited by
            #5

            Shahil, The 'order by' will do the sorting in alphabetical order, since now OrderDate is a string, so I'll get 'June' before 'May'. I need to be able to still sort by date. thanx

            K S 2 Replies Last reply
            0
            • K kozu

              Shahil, The 'order by' will do the sorting in alphabetical order, since now OrderDate is a string, so I'll get 'June' before 'May'. I need to be able to still sort by date. thanx

              K Offline
              K Offline
              kozu
              wrote on last edited by
              #6

              okay, I think I found a way around this: add a dummy column and sort by that select cast(OrderDate as char(11)), SUM(Qty), convert(datetime,convert(char(10),OrderDate,101)) from Orders where convert(datetime,convert(char(10),OrderDate,101)) between '05/29/05' and '06/10/05' group by cast(OrderDate as char(11)), convert(datetime,convert(char(10),OrderDate,101)) order by convert(datetime,convert(char(10),OrderDate,101))

              1 Reply Last reply
              0
              • K kozu

                Shahil, The 'order by' will do the sorting in alphabetical order, since now OrderDate is a string, so I'll get 'June' before 'May'. I need to be able to still sort by date. thanx

                S Offline
                S Offline
                Shahil Shah
                wrote on last edited by
                #7

                Hi kozu try this: create table #tmp ( OrderDate datetime, qty int ) insert into #tmp(orderdate, qty) select OrderDate, sum(qty) from orders where OrderDate between '05/29/80' and '06/10/05' group by OrderDate order by OrderDate select CAST(OrderDate as varchar(11)) as OrderDate, Qty from #tmp DROP TABLE #tmp Shahil

                1 Reply Last reply
                0
                • K kozu

                  Hi, I need to convert the DateTime to a date format. I know I have to use Convert, but I don't think I use it right because I'm not getting the expected result. Here's my scenarion: I want to do a sum for all products in the DB that were bought during a given period of time. My query looks like this select convert(datetime,convert(char(10),OrderDate,101)), SUM(Qty) from Orders where convert(datetime,convert(char(10),OrderDate,101)) between '05/29/05' and '06/10/05' group by convert(datetime,convert(char(10),OrderDate,101)) order by convert(datetime,convert(char(10),OrderDate,101)) the result looks like this: OrderDate Qty 2005-05-30 00:00:00.000 1 2005-05-31 00:00:00.000 4 How can I get rid of the 0s at the end of OrderDate? thanx

                  M Offline
                  M Offline
                  Michael Potter
                  wrote on last edited by
                  #8

                  Form your result set then use it as the source for the final query where you can format your data.

                  SELECT
                      CONVERT(VARCHAR(20),OrderDate,101) AS OrderDate,
                      TotalQty
                  FROM
                      (
                          SELECT  
                              CAST(CONVERT(VARCHAR(20),OrderDate,101) AS DATETIME) AS OrderDate, 
                              SUM(Qty) AS TotalQty
                          FROM 
                              Orders
                          WHERE 
                              CAST(CONVERT(VARCHAR(20),OrderDate,101) AS DATETIME) between '05/29/05' and '06/10/05'
                          GROUP BY 
                              CAST(CONVERT(VARCHAR(20),OrderDate,101) AS DATETIME)
                      ) AS subqry
                  ORDER BY 
                      OrderDate
                  

                  Speed wise, I think kubben is correct. I would remove the CAST/CONVERT in the WHERE clause of the subquery with a little intelligent manipulation of the date ranges.

                  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