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. Converting datetime to Desired format?

Converting datetime to Desired format?

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
3 Posts 3 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.
  • M Offline
    M Offline
    majidbhutta
    wrote on last edited by
    #1

    I want to display DateTime in the DataGrid in the Format "Wednesday,November 30,2005" stored in the sql server datbase as smalldatetime. How to Convert it in the above format from smaldatetime.Query reqired. Thnx in Advance

    T L 2 Replies Last reply
    0
    • M majidbhutta

      I want to display DateTime in the DataGrid in the Format "Wednesday,November 30,2005" stored in the sql server datbase as smalldatetime. How to Convert it in the above format from smaldatetime.Query reqired. Thnx in Advance

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO create FUNCTION dbo.udf_FormatDate (@date datetime, @format varchar(50)) RETURNS VARCHAR(50) AS BEGIN ------------------------------------------------------------------------------------- -- -- Name: FormatDate -- -- Purpose: Mimics the VB Format routine for dates -- -- Parameters: -- @date - Date, Date to be formatted -- @format - String, Template to format the date to -- -- Returns: String, Date formatted to user requested template -- -- Notes: -- 1. Time information is not accounted for in this routine -- -- 2. @format accepts the following values for each section of the date. -- Day -- dddd - Full Day Name -- ddd - Abbreviated Day Name -- dd - Zero Padded Day Number -- d - Day Number -- -- Month -- mmmm - Full Month Name -- mmm - Abbreviated Month Name -- mm - Zero Padded Month Number -- m - Month Number -- -- Year -- yyyy - Full 4 digit year -- yy - 2 digit year -- -- 3. Any unexpected characters will be returned in the string -- -- 4. Assumes database was set up with case-insensitive collation -- -- Example Usage: -- All examples use the following date 2003-07-13 00:00:00 -- -- 'dddd, mmmm dd, yyyy' --> Sunday, July 13, 2003 -- 'mmddyyyy' --> 07132003 -- 'm-d-yy' --> 7-13-03 -- 'mm/dd/yyyy' --> 07/13/2003 -- ------------------------------------------------------------------------------------- DECLARE @pos AS INTEGER DECLARE @char AS VARCHAR(1) -- -- Replace Year -- SET @pos = CHARINDEX('yyyy', @format) WHILE @pos > 0 BEGIN SET @format = STUFF(@format, @pos, 4, DATENAME(yyyy, @date)) --PRINT @format SET @pos = CHARINDEX('yyyy', @format) END SET @pos = CHARINDEX('yy', @format) WHILE @pos > 0 BEGIN SET @format = STUFF(@format, @pos, 2, RIGHT(DATENAME(yyyy, @date) ,2)) --PRINT @format SET @pos = CHARINDEX('yy', @format) END -- -- Replace Month -- SET @pos = CHARINDEX('mmmm', @format) WHILE @pos > 0 BEGIN SET @format = STUFF(@format, @pos, 4, DATENAME(month, @date)) --PRINT @format SET @pos = CHARINDEX('mmmm', @format) END SET @pos = CHARINDEX('mmm', @format) WHILE @pos > 0 BEGIN SET @format = STUFF(@format, @pos, 3, LEFT(DATENAME(month, @date), 3)) --PRINT @format SET @pos = CHARINDEX('mmm', @format) END SET @pos = CHARINDEX('mm', @format) WHILE @pos > 0 BEGIN SET @format = STUFF(@format, @pos, 2, RIGHT(('0' + CAST(DATEPART(month, @d

      1 Reply Last reply
      0
      • M majidbhutta

        I want to display DateTime in the DataGrid in the Format "Wednesday,November 30,2005" stored in the sql server datbase as smalldatetime. How to Convert it in the above format from smaldatetime.Query reqired. Thnx in Advance

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        In the DataGrid? An ASP.NET DataGrid? o a Windows Forms DataGrid? In any case, you can set a Format or FormatExpression property for the column (in your app, not in the database). Set the format to "D" to use the long date pattern. See here[^] -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

        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