How to convert ddmmyy into SQL datetime

Posted on 2008-11-13
Last Modified: 2012-05-05

I have an IVR which takes in DTMF values for a date in the format ddmmyy.

I then want to update a date field with those values - how do I go about convert the string into a datetime field?  I'm finding I can't use Convert(Datetime....  because it is expecting the input in a different format.

Likewise if I take in, for example, 1450 to represent a time, how do I get that into a table field to log the time?

Thanks in advance....
Question by:catherinelouise
    LVL 4

    Expert Comment

    try this
    declare @dt as varchar(6)
    set @dt = '141108'
    select cast(substring(@dt,1,2) + substring(@dt,3,2) + substring(@dt,5,2) as datetime)
    LVL 142

    Accepted Solution

    you have to work the string a bit:

    DECLARE @data VARCHAR(6)
    SET @data = '131108'
    SELECT @data, CONVERT(datetime, LEFT(@data, 2) + '.' + SUBSTRING(@data,3,2) + '.' + SUBSTRING(@data, 5,2), 4) date_value

    Open in new window

    LVL 5

    Author Closing Comment

    Sweeet - thanks angelll

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now