Varchar to datetime

  • I have a column with datatype as varchar.

    I am writing a select statement and need to show the data in the below format.

    MM/DD/YYYY

    2011-03-09 15:16:41 should be 03-09-2011 15:16:41

    How do I do this.I have been able to get the date part from the varchar filed using this statement.Default date should be 1/1/1899 if no data(NULL) is found.

    The statement:

    select............

    ........................

    logindate=ISNULL(CONVERT(varchar,cast(Tablename.Logindate as datetime),101),'1/1/1899')

    from

    ..........

    SAMPLE DATA:

    1899/1/1

    1899/1/1

    1899/1/1

    2011-03-09 15:16:41

    2011-02-23 15:07:03

    1899/1/1

    1899/1/1

    1899/1/1

    2010-07-26 10:54:25

    2009-08-14 10:44:31

    2011-02-24 12:55:24

    2009-05-04 14:46:29

    1899/1/1

    2009-07-08 08:57:47

    2010-06-08 13:47:13

    1899/1/1

    1899/1/1

    2010-07-08 08:04:09

    1899/1/1

    2010-09-10 11:20:01

    2011-03-10 08:18:14

    2011-03-09 08:30:29

    2009-09-14 14:12:53

    2011-03-09 16:59:00

    2011-02-24 13:09:08

    1899/1/1

    2010-06-18 11:47:04

    1899/1/1

    2009-08-12 10:22:57

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    2011-01-05 11:11:40

    2010-12-13 09:10:21

    1899/1/1

    1899/1/1

    2011-02-18 08:55:33

    1899/1/1

    1899/1/1

    2011-03-07 09:30:18

    1899/1/1

    1899/1/1

    2010-05-17 15:31:17

    2010-12-08 10:43:48

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    2011-01-11 10:43:43

    2011-02-10 13:36:23

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    2009-08-21 10:16:19

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    1899/1/1

    2011-03-07 12:16:36

    1899/1/1

    1899/1/1

    2011-03-08 08:48:02

    1899/1/1

    1899/1/1

    2009-08-13 11:12:04

    1899/1/1

    2011-02-07 11:35:44

    1899/1/1

    1899/1/1

  • Something like this?

    SELECT CONVERT(CHAR(10),Tablename.Logindate,110) + ' ' + CONVERT(CHAR(8),Tablename.Logindate,108)



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Try this:

    SELECT

    REPLACE(CONVERT(varchar,cast('2011-03-09 15:16:41'as datetime),101),'/','-')

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Try this:

    SELECT ...,

    REPLACE(CONVERT(CHAR(10), CONVERT(DATETIME, [column_name]), 101), '/', '-') + ' ' + CONVERT(CHAR(12), CONVERT(DATETIME, [column_name]), 114) AS my_new_date,

    ...

    FROM [some_table];

    Edit: missed the replace, thanks bitbucket

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • DOes not work....

  • Does not work.....

    Msg 241, Level 16, State 1, Line 1

    Conversion failed when converting datetime from character string.

  • Means you may have a non-date value.

    Try this: Select * from My Table where IsDate(MyColumn) = 0

    (may not get you everything/anything but it's a good place to start)

  • Here's a thread about identifying bad date values. Maybe it will help

    http://www.sqlservercentral.com/Forums/Topic1056892-146-1.aspx

  • sqlserver12345 (3/10/2011)


    I have a column with datatype as varchar.

    I am writing a select statement and need to show the data in the below format.

    MM/DD/YYYY

    Having dates and times stored in a VARCHAR column is probably one of the biggest design mistakes there is in the world of SQL. If you can, you need to change it to a DATETIME datatype for more reasons that I care to list in a single post. It's just not a good idea to store dates or times and anything other than a proper date or time datatype.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • sqlserver12345 (3/15/2011)


    Does not work.....

    Msg 241, Level 16, State 1, Line 1

    Conversion failed when converting datetime from character string.

    Date conversion is also dictated by the language setting of the login as to whether sql server treats the text date as YMD or YDM

    Try adding SET DATEFORMAT YMD before your query

    Note: This does not apply to YYYYMMDD formated dates.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Jeff Moden (3/15/2011)


    Having dates and times stored in a VARCHAR column is probably one of the biggest design mistakes there is in the world of SQL. If you can, you need to change it to a DATETIME datatype for more reasons that I care to list in a single post. It's just not a good idea to store dates or times and anything other than a proper date or time datatype.

    This sounds like a great article. I can think of about a zillion reasons myself but I bet you can name about a zillion more that I have never even thought of.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply