Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2003
    Posts
    111

    Unanswered: CAST a variable into a datetime object

    CAST a variable into a datetime object
    I need to do a CAST(@variable_name as datetime)
    this won't work because @variable_name has the following format

    'dd/mm/yy hh:mi:ss:mmmAM'

    like how do i specify a style for it.

    please help..


    James : (

  2. #2
    Join Date
    Aug 2002
    Location
    Scotland
    Posts
    1,578
    You can use default values (style 0 or 100) to represent, refer to books online for CAST & CONVERT topic.

    HTH.
    --Satya SKJ
    Microsoft SQL Server MVP
    [IMG]http://sqlserver-qa.net/google_bart.gif[/IMG]

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    My suggestion is to write some code (which could be a transact-sql expression) to convert your chacter date to a standard form. I would strongly suggest using the ISO standard 'yyyy-mm-dd hh:mm:ss.ttt" format. I've seen a euro-to-ISO time function, if you can't find it, I can either find or write one for you.

    -PatP

  4. #4
    Join Date
    Aug 2003
    Posts
    111
    hi

    thanx for the reply.

    books online wasn't of much help because it didn't provide any examples of converting a string to datetime. but, I have found the following to work.

    convert (datetime, '30/12/04 1:10:30:000PM', 3);

    i remember i used to use a combination of convert and cast to get datetime conversion working. I am just surprised that format such as
    'dd/mm/yyyy' is not automatically supported in ms sql server. I had to parse the string so that i get dd/mm/yyyy in order for the conversion to work.

    thanx for helping.

    james : )

  5. #5
    Join Date
    Aug 2003
    Posts
    111
    Originally posted by Pat Phelan
    My suggestion is to write some code (which could be a transact-sql expression) to convert your chacter date to a standard form. I would strongly suggest using the ISO standard 'yyyy-mm-dd hh:mm:ss.ttt" format. I've seen a euro-to-ISO time function, if you can't find it, I can either find or write one for you.

    -PatP
    hi, it would be good if you can show me how it's done, i don't know how to do any string manipulation in mssql.

    thank you.


    james

Posting Permissions

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