Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2008
    Posts
    4

    Unanswered: Date Conversion in SQL Server 2005

    Hello Everyone, thank you for taking the time to read my post. I'm creating a view in SQL Server 2005 to base a report on Crystal Reports XI. I've been trying to figure out how I can convert a date field in the format YYYYMMDD to MM/DD/YYYY. I'm not quite sure about the steps I need to take to accomplish this since I'm pretty new to this. The date is stored as an Int on the database, and I've tried converting it directly to the DATETIME data type like this: CONVERT(DATETIME, datefield,101) but I'm getting an error saying illegal data type conversion. Thanks a lot for your help, I really appreciate it.

    MS

  2. #2
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    select convert(char(10), convert(datetime, cast(20080425/*Today's date */as char(8))), 101)
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    first, are you sure that your column is an int.

    what does sp_help yourTableName say?

    if it is an int, you could always run some thing like...

    Code:
    DECLARE @myNumber int
    
    SET @myNumber = 19750213
    
    SELECT SUBSTRING(CAST(@myNumber as Char(8)),5,2) + '/' + RIGHT(CAST(@myNumber as Char(8)),2) + '/' + LEFT(CAST(@myNumber as Char(8)),4)
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #4
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by Dominican1979
    Hello Everyone, thank you for taking the time to read my post. I'm creating a view in SQL Server 2005 to base a report on Crystal Reports XI. I've been trying to figure out how I can convert a date field in the format YYYYMMDD to MM/DD/YYYY. I'm not quite sure about the steps I need to take to accomplish this since I'm pretty new to this. The date is stored as an Int on the database, and I've tried converting it directly to the DATETIME data type like this: CONVERT(DATETIME, datefield,101) but I'm getting an error saying illegal data type conversion. Thanks a lot for your help, I really appreciate it.

    MS
    Hello rdjabarov thank you very much for your reply it helped me a lot. Just one thing though, the table i'm creating the view from has about 9000 records, when I ran the view against it with your code like this:
    SELECT CONVERT(char(10), CONVERT(DATETIME, CAST(Dt3 AS char(8))), 101) AS Date
    FROM dbo.FreeInf3

    It converted about 3000 records with the right format then I got the following error message: "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value" The weird thing is that the dates on this field are entered automatically by pressing the "+" sign to enter the current date on our front end application, so the dates are entered in a consistent way, not just by the users typing whatever they want. I looket through some of the records that were not converted, and they are in the same exact format as the ones that were. Any ideas why? thank you so much

  5. #5
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    Check if the data is stored in YYYYMMDD format, not YYYYDDMM or something like that. Also make sure that the resulting character string is 8 characters long. In addition, check for 1 and 8 in the output from the following:

    select isdate(cast(Dt3 as char(8)), datalength(cast(Dt3 as varchar(10)) from dbo.FreeInf3
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  6. #6
    Join Date
    Apr 2008
    Posts
    4
    Hey rdjabarov thank you sooooooooooooooooooooooooooooooooooooo much for your help, it was because of 2 records out of all 9000 that had some weird date for some reason. I just deleted them and now good to go. Can't thank you enough :-)

  7. #7
    Join Date
    Apr 2008
    Posts
    4
    hi Thrasymachus yeah thank you so much for your reply as well, yes it is stored as an int on the table

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Well it shouldn't be
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •