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 Decimal To Date?

Convert Decimal To Date?

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

    Dear All, I have a date values stored as Decimal in the SQL server 2005. I need to convert these decimal values into Date in the format (yyyy-mm-dd). Example: I need to convert 20080721 To 2008-07-21 I wrote the following statement for this purpose, but the result contains 00:00:00:00 in the end, and I think this is because I am using datetime function in the query. select convert(datetime, cast(DayDate AS char(8))) AS Date from TableName where DayDate = 20080721 Result from the previous query is: 2008-07-21 00:00:00:00, but I need it to be 2008-07-21 without 00:00:00:00

    Kind Regards OBarahmeh

    R B Z L 4 Replies Last reply
    0
    • O obarahmeh

      Dear All, I have a date values stored as Decimal in the SQL server 2005. I need to convert these decimal values into Date in the format (yyyy-mm-dd). Example: I need to convert 20080721 To 2008-07-21 I wrote the following statement for this purpose, but the result contains 00:00:00:00 in the end, and I think this is because I am using datetime function in the query. select convert(datetime, cast(DayDate AS char(8))) AS Date from TableName where DayDate = 20080721 Result from the previous query is: 2008-07-21 00:00:00:00, but I need it to be 2008-07-21 without 00:00:00:00

      Kind Regards OBarahmeh

      R Offline
      R Offline
      Rocky
      wrote on last edited by
      #2

      hi, well as far as my experience goes, whenever u user datetime in SQL Server it always appends a default time, -- even if u're not specifying it. So I suggest u should stick to this result and try and improve the logic of ur front end to compensate to that. Hope it helps, Regards,

      Rocky Success is a ladder which you can't climb with your hands in your pockets.

      P 1 Reply Last reply
      0
      • O obarahmeh

        Dear All, I have a date values stored as Decimal in the SQL server 2005. I need to convert these decimal values into Date in the format (yyyy-mm-dd). Example: I need to convert 20080721 To 2008-07-21 I wrote the following statement for this purpose, but the result contains 00:00:00:00 in the end, and I think this is because I am using datetime function in the query. select convert(datetime, cast(DayDate AS char(8))) AS Date from TableName where DayDate = 20080721 Result from the previous query is: 2008-07-21 00:00:00:00, but I need it to be 2008-07-21 without 00:00:00:00

        Kind Regards OBarahmeh

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        select substring(convert(varchar, convert(datetime, cast(DayDate AS char(8))),20),1,10) AS Date from TableName where DayDate = 20080721


        I Love T-SQL "Don't torture yourself,let the life to do it for you."

        1 Reply Last reply
        0
        • O obarahmeh

          Dear All, I have a date values stored as Decimal in the SQL server 2005. I need to convert these decimal values into Date in the format (yyyy-mm-dd). Example: I need to convert 20080721 To 2008-07-21 I wrote the following statement for this purpose, but the result contains 00:00:00:00 in the end, and I think this is because I am using datetime function in the query. select convert(datetime, cast(DayDate AS char(8))) AS Date from TableName where DayDate = 20080721 Result from the previous query is: 2008-07-21 00:00:00:00, but I need it to be 2008-07-21 without 00:00:00:00

          Kind Regards OBarahmeh

          Z Offline
          Z Offline
          zahedonline
          wrote on last edited by
          #4

          Hi obarahmeh, I have a solution for your question hope this helps you out ;) What you can do is that create a custom function in your Database, Code given below Function: *********

          CREATE FUNCTION dbo.FormatDateTime
          (
          @dt DATETIME,
          @format VARCHAR(16)
          )
          RETURNS VARCHAR(64)
          AS
          BEGIN
          DECLARE @dtVC VARCHAR(64)
          SELECT @dtVC = CASE @format

          WHEN 'LONGDATE' THEN 
          
              DATENAME(dw, @dt) 
              + ',' + SPACE(1) + DATENAME(m, @dt) 
              + SPACE(1) + CAST(DAY(@dt) AS VARCHAR(2)) 
              + ',' + SPACE(1) + CAST(YEAR(@dt) AS CHAR(4)) 
          
          WHEN 'LONGDATEANDTIME' THEN 
          
              DATENAME(dw, @dt) 
              + ',' + SPACE(1) + DATENAME(m, @dt) 
              + SPACE(1) + CAST(DAY(@dt) AS VARCHAR(2)) 
              + ',' + SPACE(1) + CAST(YEAR(@dt) AS CHAR(4)) 
              + SPACE(1) + RIGHT(CONVERT(CHAR(20), 
              @dt - CONVERT(DATETIME, CONVERT(CHAR(8), 
              @dt, 112)), 22), 11) 
          
          WHEN 'SHORTDATE' THEN 
          
              LEFT(CONVERT(CHAR(19), @dt, 0), 11) 
          
          WHEN 'SHORTDATEANDTIME' THEN 
          
              REPLACE(REPLACE(CONVERT(CHAR(19), @dt, 0), 
                  'AM', ' AM'), 'PM', ' PM') 
          
          WHEN 'UNIXTIMESTAMP' THEN 
          
              CAST(DATEDIFF(SECOND, '19700101', @dt) 
              AS VARCHAR(64)) 
          
          WHEN 'YYYYMMDD' THEN 
          
              CONVERT(CHAR(8), @dt, 112) 
          
          WHEN 'YYYY-MM-DD' THEN 
          
              CONVERT(CHAR(10), @dt, 23) 
          
          WHEN 'YYMMDD' THEN 
          
              CONVERT(VARCHAR(8), @dt, 12) 
          
          WHEN 'YY-MM-DD' THEN 
          
              STUFF(STUFF(CONVERT(VARCHAR(8), @dt, 12), 
              5, 0, '-'), 3, 0, '-') 
          
          WHEN 'MMDDYY' THEN 
          
              REPLACE(CONVERT(CHAR(8), @dt, 10), '-', SPACE(0)) 
          
          WHEN 'MM-DD-YY' THEN 
          
              CONVERT(CHAR(8), @dt, 10) 
          
          WHEN 'MM/DD/YY' THEN 
          
              CONVERT(CHAR(8), @dt, 1) 
          
          WHEN 'MM/DD/YYYY' THEN 
          
              CONVERT(CHAR(10), @dt, 101) 
          
          WHEN 'DDMMYY' THEN 
          
              REPLACE(CONVERT(CHAR(8), @dt, 3), '/', SPACE(0)) 
          
          WHEN 'DD-MM-YY' THEN 
          
              REPLACE(CONVERT(CHAR(8), @dt, 3), '/', '-') 
          
          WHEN 'DD/MM/YY' THEN 
          
              CONVERT(CHAR(8), @dt, 3) 
          
          WHEN 'DD/MM/YYYY' THEN 
          
              CONVERT(CHAR(10), @dt, 103) 
          
          WHEN 'HH:MM:SS 24' THEN 
          
              CONVERT(CHAR(8), @dt, 8) 
          
          WHEN 'HH:MM 24' THEN 
          
              LEFT(CONVERT(VARCHAR(8), @dt, 8), 5) 
          
          WHEN 'HH:MM:SS 12' THEN 
          
              LTRIM(RIGHT(CONVERT(VARCHAR(20), @dt, 22), 11)) 
          
          WHEN 'HH:MM 12' THEN 
          
              LTRIM(SUBSTRING(CONVERT( 
              VARCHAR
          
          1 Reply Last reply
          0
          • O obarahmeh

            Dear All, I have a date values stored as Decimal in the SQL server 2005. I need to convert these decimal values into Date in the format (yyyy-mm-dd). Example: I need to convert 20080721 To 2008-07-21 I wrote the following statement for this purpose, but the result contains 00:00:00:00 in the end, and I think this is because I am using datetime function in the query. select convert(datetime, cast(DayDate AS char(8))) AS Date from TableName where DayDate = 20080721 Result from the previous query is: 2008-07-21 00:00:00:00, but I need it to be 2008-07-21 without 00:00:00:00

            Kind Regards OBarahmeh

            L Offline
            L Offline
            leoinfo
            wrote on last edited by
            #5

            As there is no *DATE* data type in SQL 2005, you cannot store a date in the form that you want. The only options you have to store a date (and time) are DATETIME and SMALLDATETIME (see Data Types[^]) If you need only the day part of a date I suggest you to use SMALLDATETIME ...

            DECLARE @d DECIMAL(15,6) ;SET @d = 20080721.150825

            SELECT
            @d AS [as DECIMAL]
            , CAST( LEFT(@d ,8) AS SMALLDATETIME ) AS [as SMALLDATETIME]
            , CAST( LEFT(@d ,8) AS DATETIME ) AS [as DATETIME]
            , CONVERT( NVARCHAR(10), CAST( LEFT(@d ,8) AS DATETIME ) , 121 ) AS [as NVARCHAR(10)]

            Please... SAVE my time by rating the posts that you read!


            There are 10 kinds of people in the world: those who understand binary and those who don't.

            O 1 Reply Last reply
            0
            • L leoinfo

              As there is no *DATE* data type in SQL 2005, you cannot store a date in the form that you want. The only options you have to store a date (and time) are DATETIME and SMALLDATETIME (see Data Types[^]) If you need only the day part of a date I suggest you to use SMALLDATETIME ...

              DECLARE @d DECIMAL(15,6) ;SET @d = 20080721.150825

              SELECT
              @d AS [as DECIMAL]
              , CAST( LEFT(@d ,8) AS SMALLDATETIME ) AS [as SMALLDATETIME]
              , CAST( LEFT(@d ,8) AS DATETIME ) AS [as DATETIME]
              , CONVERT( NVARCHAR(10), CAST( LEFT(@d ,8) AS DATETIME ) , 121 ) AS [as NVARCHAR(10)]

              Please... SAVE my time by rating the posts that you read!


              There are 10 kinds of people in the world: those who understand binary and those who don't.

              O Offline
              O Offline
              obarahmeh
              wrote on last edited by
              #6

              Hello Every body, I've solved my problem... And this is the solution to convert a decimal to a date in the format (yyyy-mm-dd): select STUFF(STUFF(CAST(DayDate AS char(8)), 7, 0, '-'), 5, 0, '-') AS Date from TableName; ;P ;P :rose:

              Kind Regards OBarahmeh

              M 1 Reply Last reply
              0
              • O obarahmeh

                Hello Every body, I've solved my problem... And this is the solution to convert a decimal to a date in the format (yyyy-mm-dd): select STUFF(STUFF(CAST(DayDate AS char(8)), 7, 0, '-'), 5, 0, '-') AS Date from TableName; ;P ;P :rose:

                Kind Regards OBarahmeh

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

                a more appropriate solution would be to use the date data type SELECT CONVERT(VARCHAR(20),CONVERT(DATETIME,'20080819'),106) SELECT CONVERT(VARCHAR(20),CONVERT(DATETIME,'20080819'),103) SELECT CONVERT(VARCHAR(20),CONVERT(DATETIME,'20080819'),110) SELECT CONVERT(VARCHAR(20),CONVERT(DATETIME,'20080819'),111)

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • R Rocky

                  hi, well as far as my experience goes, whenever u user datetime in SQL Server it always appends a default time, -- even if u're not specifying it. So I suggest u should stick to this result and try and improve the logic of ur front end to compensate to that. Hope it helps, Regards,

                  Rocky Success is a ladder which you can't climb with your hands in your pockets.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Rocky# wrote:

                  whenever u user datetime in SQL Server it always appends a default time

                  No it doesn't. You need to use GETDATE() or a fixed date if you don't supply a date into a non-nullable database field. [EDIT]Rocky# is right - we are talking at cross purposes here, and he's entirely right to say that there is no date only type in SQL Servers up to 2005. 2K8 has a date type which stores the date only, but datetime does what it says on the tin and has the time portion as part of it. My apologies for any confusion caused by my misreading of Rocky#'s post.[/EDIT]

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  modified on Wednesday, July 23, 2008 7:08 AM

                  R 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Rocky# wrote:

                    whenever u user datetime in SQL Server it always appends a default time

                    No it doesn't. You need to use GETDATE() or a fixed date if you don't supply a date into a non-nullable database field. [EDIT]Rocky# is right - we are talking at cross purposes here, and he's entirely right to say that there is no date only type in SQL Servers up to 2005. 2K8 has a date type which stores the date only, but datetime does what it says on the tin and has the time portion as part of it. My apologies for any confusion caused by my misreading of Rocky#'s post.[/EDIT]

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    modified on Wednesday, July 23, 2008 7:08 AM

                    R Offline
                    R Offline
                    Rocky
                    wrote on last edited by
                    #9

                    my point is that datetime field always has a time part in it. try

                    Select Getdate()

                    or may be we're not on the same page at this point :suss:

                    Rocky Success is a ladder which you can't climb with your hands in your pockets.

                    P 1 Reply Last reply
                    0
                    • R Rocky

                      my point is that datetime field always has a time part in it. try

                      Select Getdate()

                      or may be we're not on the same page at this point :suss:

                      Rocky Success is a ladder which you can't climb with your hands in your pockets.

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      No, no, no. You're just plain wrong. GETDATE() is a system function, not field data. It's no different in effect to you typing SELECT 3. This doesn't mean that every integer field defaults to 3. Tell you what, as a test, run the following script:

                      USE [<<database>>]
                      GO

                      SET ANSI_NULLS ON
                      GO
                      SET QUOTED_IDENTIFIER ON
                      GO

                      CREATE TABLE [dbo].Test(
                      [ID] [int] IDENTITY(1,1) NOT NULL,
                      [Text] NVARCHAR(20) NOT NULL,
                      [Created] [datetime] NOT NULL,
                      CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
                      (
                      [ID] ASC
                      )WITH (PAD_INDEX = OFF,
                      STATISTICS_NORECOMPUTE = OFF,
                      IGNORE_DUP_KEY = OFF,
                      ALLOW_ROW_LOCKS = ON,
                      ALLOW_PAGE_LOCKS = ON)
                      ON [PRIMARY]
                      )
                      GO

                      INSERT INTO Test([Text]) VALUES ('Hello')

                      GO

                      Guess what. Created doesn't get a default value. What you do get is an error: Msg 515, Level 16, State 2, Line 2 Cannot insert the value NULL into column 'Created', table 'database.dbo.Test'; column does not allow nulls. INSERT fails. The statement has been terminated. So no default value there then. Please, before you pass information like this along, will you please check to see if you are right? [EDIT]Rocky# is right - we are talking at cross purposes here, and he's entirely right to say that there is no date only type in SQL Servers up to 2005. 2K8 has a date type which stores the date only, but datetime does what it says on the tin and has the time portion as part of it. My apologies for any confusion caused by my misreading of Rocky#'s post.[/EDIT]

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles

                      modified on Wednesday, July 23, 2008 7:12 AM

                      R 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        No, no, no. You're just plain wrong. GETDATE() is a system function, not field data. It's no different in effect to you typing SELECT 3. This doesn't mean that every integer field defaults to 3. Tell you what, as a test, run the following script:

                        USE [<<database>>]
                        GO

                        SET ANSI_NULLS ON
                        GO
                        SET QUOTED_IDENTIFIER ON
                        GO

                        CREATE TABLE [dbo].Test(
                        [ID] [int] IDENTITY(1,1) NOT NULL,
                        [Text] NVARCHAR(20) NOT NULL,
                        [Created] [datetime] NOT NULL,
                        CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
                        (
                        [ID] ASC
                        )WITH (PAD_INDEX = OFF,
                        STATISTICS_NORECOMPUTE = OFF,
                        IGNORE_DUP_KEY = OFF,
                        ALLOW_ROW_LOCKS = ON,
                        ALLOW_PAGE_LOCKS = ON)
                        ON [PRIMARY]
                        )
                        GO

                        INSERT INTO Test([Text]) VALUES ('Hello')

                        GO

                        Guess what. Created doesn't get a default value. What you do get is an error: Msg 515, Level 16, State 2, Line 2 Cannot insert the value NULL into column 'Created', table 'database.dbo.Test'; column does not allow nulls. INSERT fails. The statement has been terminated. So no default value there then. Please, before you pass information like this along, will you please check to see if you are right? [EDIT]Rocky# is right - we are talking at cross purposes here, and he's entirely right to say that there is no date only type in SQL Servers up to 2005. 2K8 has a date type which stores the date only, but datetime does what it says on the tin and has the time portion as part of it. My apologies for any confusion caused by my misreading of Rocky#'s post.[/EDIT]

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

                        modified on Wednesday, July 23, 2008 7:12 AM

                        R Offline
                        R Offline
                        Rocky
                        wrote on last edited by
                        #11

                        Pete O'Hanlon wrote:

                        Guess what. Created doesn't get a default value. What you do get is an error:

                        :wtf: what else would it give, trying to insert a null into a not null column, try this

                        Insert into Test(text, created)
                        values ('my test', CAST('7/23/2008' as datetime))

                        The result looks like this on my end: 7/23/2008 12:00:00 AM My point was simple, if u r getting a dateTime object, it will ALWAYS show the time, if u dont wanna see the time then cast it to varchar or something but datetime will always show u the time. here in the above query I'm not giving the time but its still comming. Thats the simple point I was talking abt. Pardon me if this doesnt satisfy the guys original question but my point was this alright! :rose: I deeply respect ur place Pete, I dont mean any offense alright

                        Rocky Success is a ladder which you can't climb with your hands in your pockets.

                        P 1 Reply Last reply
                        0
                        • R Rocky

                          Pete O'Hanlon wrote:

                          Guess what. Created doesn't get a default value. What you do get is an error:

                          :wtf: what else would it give, trying to insert a null into a not null column, try this

                          Insert into Test(text, created)
                          values ('my test', CAST('7/23/2008' as datetime))

                          The result looks like this on my end: 7/23/2008 12:00:00 AM My point was simple, if u r getting a dateTime object, it will ALWAYS show the time, if u dont wanna see the time then cast it to varchar or something but datetime will always show u the time. here in the above query I'm not giving the time but its still comming. Thats the simple point I was talking abt. Pardon me if this doesnt satisfy the guys original question but my point was this alright! :rose: I deeply respect ur place Pete, I dont mean any offense alright

                          Rocky Success is a ladder which you can't climb with your hands in your pockets.

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #12

                          Crap. My apologies - I just reread your original post, and it seems you and I were talking about different things. So sorry - and I will amend my answers to reflect this. I really shouldn't post answers late at night.

                          Deja View - the feeling that you've seen this post before.

                          My blog | My articles

                          R 1 Reply Last reply
                          0
                          • P Pete OHanlon

                            Crap. My apologies - I just reread your original post, and it seems you and I were talking about different things. So sorry - and I will amend my answers to reflect this. I really shouldn't post answers late at night.

                            Deja View - the feeling that you've seen this post before.

                            My blog | My articles

                            R Offline
                            R Offline
                            Rocky
                            wrote on last edited by
                            #13

                            No problem Pete! :)

                            Rocky Success is a ladder which you can't climb with your hands in your pockets.

                            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