How to Format date without "Convert " function in T-SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arti
    New Member
    • May 2007
    • 13

    How to Format date without "Convert " function in T-SQL

    I dont want to use Convert(Char(9) ,date,106) function to show date in dd/MM/yyyy format. It changes the datatype of my column to char & I cant perform other date operations on it without changing it back to datetime.
    Is there any T-sql way of doing it?
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Why would you care what format date has when you do data manipulation.
    Use convert function only in select close when you try to display this date to a user.

    Code:
    SELECT Convert(Char(9),date,106) Date
    FROM some_table 
    WHERE date > dateadd(d, -10, getdate())
    ORDER BY date
    Good Luck.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      You are trying to get the datetime formatted in a non-standard format but still be able to use it in the date?

      I would suggest creating a second column with the altered format, use that for viewing, and use the original for altering, updating, and calculating.

      Comment

      • ronicard
        New Member
        • Dec 2006
        • 3

        #4
        A couple of other options:
        A bit verbose, but you can do this:

        select
        dtForm = cast(day(dateFi eld) as varchar) + '/' + cast(month(date Field) as varchar) + '/' + cast(year(dateF ield) as varchar)

        obviously, "dateField" is the name of whatever date field you're pulling from the database. If you still wanted that as a date, you could always recast that back to datetime with the cast function, just like I was doing with the varchar.

        Comment

        • greedy4chips
          New Member
          • Jun 2007
          • 2

          #5
          If you are doing it in a datagrid, just change the defaultcellstyl e of the date column. "d" is the short date format mm/dd/yyyy

          Me.dataGridView 1.Columns("Name OfDateColumn"). DefaultCellStyl e.Format = "d"

          The cell will still hold the datetime value from the database, but will only show the converted date to the user.

          Comment

          • manpakhong
            New Member
            • Aug 2007
            • 3

            #6
            Originally posted by iburyak
            Why would you care what format date has when you do data manipulation.
            Use convert function only in select close when you try to display this date to a user.

            Code:
            SELECT Convert(Char(9),date,106) Date
            FROM some_table 
            WHERE date > dateadd(d, -10, getdate())
            ORDER BY date
            Good Luck.

            Excuse me, can you tell me why char(9) is used for an argument for Convert()
            Thanks

            Dave

            Comment

            • manpakhong
              New Member
              • Aug 2007
              • 3

              #7
              this link has the details explanation:
              http://www.dreamincode .net/forums/showtopic42872. htm

              manpakhong@hotm ail.com


              Originally posted by manpakhong
              Excuse me, can you tell me why char(9) is used for an argument for Convert()
              Thanks

              Dave

              Comment

              Working...