Converting float fields to date Time

I have an application that stores datetimes into float fields, the data is formatted yyyymmddhhmmss or 20081122091030. I need to convert this data to a datetime data type so I can do datetime calculations (differences) for reporting purposes. It would be preferable to have the data formatted yyyy/mm/dd hh:mms. I have tried numerous methods with out success. As I understand it I need to CONVERT to a VARCHAR first.  I am new to TSQL & this one turning out to be a knotty problem so I would really appreciate anyone’s assistance,
SELECT (
  LEFT(CAST(IS_DUE_DATE AS VARCHAR(14)),8) + ' ' +
    SUBSTRING(CAST(IS_DUE_DATE AS VARCHAR(14)),9,2) + ':' +
    SUBSTRING(CAST(IS_DUE_DATE AS VARCHAR(14)),11,2) + ':' +
    SUBSTRING(CAST(IS_DUE_DATE AS VARCHAR(14)),13,2))
FROM  ISSUES
However when I try to Covert to DATETIME it all goes pear shaped.

Try this
Code Snippet
DECLARE @MyDate float
SET @MyDate = 20090922112143997
SELECT CONVERT(DATETIME,
LEFT(CAST(CAST(@MyDate as BigInt) as varchar(20)),8)+' '+
SUBSTRING(CAST(CAST(@MyDate as BigInt) as varchar(20)),9,2)+':'+
SUBSTRING(CAST(CAST(@MyDate as BigInt) as varchar(20)),11,2)+':'+
SUBSTRING(CAST(CAST(@MyDate as BigInt) as varchar(20)),13,2) + '.' +
SUBSTRING(CAST(CAST(@MyDate as BigInt) as varchar(20)),15,3)
Added using a temp table:
Code Snippet
CREATE TABLE #T
fdate float
go
INSERT INTO #T VALUES (20090922112143997)
INSERT INTO #T VALUES (20090921182143997)
INSERT INTO #T VALUES (20090921012323997)
SELECT CONVERT(DATETIME,
LEFT(CAST(CAST(fdate as BigInt) as varchar(20)),8)+' '+
SUBSTRING(CAST(CAST(fdate as BigInt) as varchar(20)),9,2)+':'+
SUBSTRING(CAST(CAST(fdate as BigInt) as varchar(20)),11,2)+':'+
SUBSTRING(CAST(CAST(fdate as BigInt) as varchar(20)),13,2) + '.' +
SUBSTRING(CAST(CAST(fdate as BigInt) as varchar(20)),15,3)
FROM #T
DROP TABLE #T

Similar Messages

  • Convert varchar2 field into date formatted: DD-MON-YYYY

    Thanks in advance for anyone's help on this matter as I know it takes your time and expertise. I am pretty new to SQL but learning my way through it just have an issue with a text to date field conversion. It is an Oracle 10g database and I am writing in SQL. There is a field called Demand which is formatted in varchar2 format of ddmmyy. There is also a field that is formatted as a date called Payment which is formatted as DD-MON-YYYY.
    Essentially I need to do a simple Payment >= Demand, however as you can see that is some issue with that being a varchar2 field. Does anyone know if it is possible to do that type of expression against those two fields. Was thinking about possibly converting the varchar2 to a date but not sure how to get to that DD-MON-YYYY format.
    Also there are situations where this Demand field will often times be null as it would have never recieved any outbound correspondence in the past and would not have a date at all.
    Thanks
    Edited by: user10860766 on Aug 18, 2009 8:14 AM
    Edited by: user10860766 on Aug 18, 2009 8:19 AM

    Hi,
    It's hard to detect bad dates in pure SQL, especially if you need to be precise about when February 29 is valid.
    It's easy with a user-define function, like the one in [this thread|http://forums.oracle.com/forums/thread.jspa?messageID=3669932&#3669932].
    Edited by: Frank Kulash on Aug 18, 2009 3:50 PM
    To create a stand-alone function:
    CREATE OR REPLACE FUNCTION     to_dt
    (     in_txt          IN     VARCHAR2                    -- to be converted
    ,     in_fmt_txt     IN     VARCHAR2     DEFAULT     'DD-MON-YYYY'     -- optional format
    ,     in_err_dt     IN     DATE          DEFAULT     NULL
    RETURN DATE
    DETERMINISTIC
    AS
    BEGIN
         -- Try to convert in_txt to a DATE.  If it works, fine.
         RETURN     TO_DATE (in_txt, in_fmt_txt);
    EXCEPTION     -- If TO_DATE caused an error, then this is not a valid DATE: return in_err_dt
         WHEN OTHERS
         THEN
              RETURN in_err_dt;
    END     to_dt
    /To use it:
    SELECT  primary_key  -- and/or other columns to identify the row
    ,       demand
    FROM    table_x
    WHERE   demand          IS NOT NULL
    AND     to_dt ( demand
               , 'DDMMYY'
               )          IS NULL;

  • MS Excel automatically converts numeric fields to date format

    I have at least 2 different spreadsheets where this happens.  I maintain them for months, adding, changing, deleting data.  They contain lots of data elements (numeric, text, date, etc).   Some columns are all numeric, some all dates,
    etc.  Then suddenly when I open the spreadsheet, my numeric formatted data has been converted to date format.  I see lots of # signs in cells, which is an indication that this happened, and selecting an affected cell shows a date, generally in the
    early 1900s, depending on the original numeric value that was in the cell.  I convert the fields back to numeric, but sometime later the same problem occurs -- numeric fields are now date formatted.  I cannot identfy any specific change that I make
    that would cause this.  And I only recognize it as having occurred when I open the spreadsheet, not anytime during its maintenance.  I am using MS Excel 2007 SP2 in a Windows XP SP3 environment.  The machine is well maintained and updated /
    kept current by our corporate IT group.  I have done numerous Internet searches and cannot find anything like this in any other blog.  Thanks for your help.

    Hi,
    Were some of the these data imported from a .csv file? As far as I know, data formats cannot be saved in .csv file. If this is the situation, try this:
    Save a copy of your .csv file changing the extension of your file from .csv to .txt
    open excel.
    go to DATA --> Import external data --> Import data --> browse to find the .txt file you made earlier
    The text import wizard should open.
    Click on delimited, click next
    click comma, click next
    You should now see all columns appear with heading GENERAL --> highlight the columns with the values that are changing to date and click TEXT (top right) instead. The heading of these columns should now change to TEXT.
    click finish
    Excel will now treat the column as text when it imports the data and will not change them to dates
    If this is not the situation, this issue might be caused by some add-ins.
    Start Excel in safe mode:  Press and hold the
    CTRL key, and then click the Excel program to start. Then, click File > Open to open the Excel file.
    If the problem does not occur in the safe mode, this issue might be related to some third-party add-ins in the Office program, we can try to disable them.
    Best Regards,
    Sally Tang

  • Function module to convert float value to data type 'dec'

    Hi experts,
      In a report i need to convert float value to the data type 'DEC'. How to convert it. Is there any function module for this conversion.
    Thanks and Regards,
    Vaibhav Tiwari.

    Hi ..
    We can do like this...
    Data : V_float type F value '12345.67'.
    Data: V_dec type P Decimals 2.
    Write:/ V_float exponent 0. "This will display it like Type P
    or
    Write V_float to V_dec EXPONENT 0.
    Write:/ V_dec.
    reward if Helpful.
    <b></b>

  • Converting varchar field to date

    I got a varchar field in DB with format YYYY/MM/DD HH:MI:SS in DB . I want to convert this to date field in RPD. When I did
    CAST ( VarChar field AS DATE) in RPD it is not working I am getting sql error.
    Thanks for your help.

    Hi,
    can you try once to cast your char as a timestamp.
    Hope that works.
    Kr,
    A

  • Problem converting U8 array to date-time string

    Hi All,
    How can I convert U8 array (time_t data type from C dll) to date time string?
    A dll function that I am calling has a structure of string, integer and time_t as one of the parameters. Instead of passing cluster, I pass an array of U8 of the size that the structure should be. All the members of the strcuture and parsed correctly except the date/time.
    Function Parameter:
    typedef struct {
    Int AlarmState ;
    Int AlarmGrade ;
    TCHAR AlarmMessage [100] ;
    time_t AlarmTimeStamp ;
    } WV_ALARM_INFO ;
    Total size = 4 + 4 + 100 + 4 = 112
    Using Call Library Function, I set the parameter type to Array of U8 and size 112.
    After the array is populated when the function is called, I have done the following to interpret date and time.
    1) Using Extract Zero Terminated String.VI I converted U8 array (of size 4) to string.
    2) Type casted string to integer (I32).
    3) Swap bytes
    4) Swap Words
    5) Format Date/Time String.
    Please see the attached screenshot for visual display of the above.
    Ideas on why the year is incorrect?
    Mimansa
    Attachments:
    Time.JPG ‏102 KB

    Hi Mimansa,
    Is it only the year that is incorrect? Also, where did you find that ABC\0 VI that you use 3 times? Did you make it or is it in a labview library somewhere?
    Thanks,
    Laura

  • GregorianCalendar datetime format convert to  sql server date time format

    please help me
    my GregorianCalendar date time format is like this. *08/01/29 02:25:59* . I try to insert my database(sql server 2000 )
    data type is datetime ,in my database table display like this *2029-08-01 02:25:59.000* .can you help me to insert correct date in my database table like ( . *08/01/29 02:25:59*)

    use [PreparedStatement |http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html] and setTimestamp:
    [http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html]

  • Convert Date Time field to Date & Time

    Post Author: Smita
    CA Forum: Crystal Reports
    I am new to Crystal XI  . I need to convert DateTime ("2007/04/04/04 09:49:05:00") format to Date and Time and then group a months data first by date and then by Time ( per Hour ).
    If someone can help ?
    Thanks

    Post Author: newcruser
    CA Forum: Crystal Reports
    Thanks
    This is easy...second group based on the second formula 'for each hour'.
    How you group by 15 minutes?.
    i want to group by every 15 minutes starting from 9 AM to 5 PM.
    First i need to create a time range and then use that time range to group?.  Or any other simple and best way to solve this?. Please help
    How to create time range from 9 AM to 5 PM?

  • How to convert a timestamp to date/time in CDS View?

    Dear experts,
    I would like to convert a timestamp to a date and time within a CDS view.
    This is what I tried to do:
    Cast (Cast(<timestamp> as abap.dec(8,0)) as abap.dats) as <date>
    I also thought about first converting the timestamp into a plain numerical character, then using substring and then casting it to abap.dats. Then its giving me an error that says: "dec can't be converted into numc".
    The above written code also throws an error - "function not allowed/ no cast within a cast".
    It is quite nasty not being able to write a cast within a cast - how else am i supposed to achieve a date here?
    I am looking forward to have your answers!
    Thanks and BR
    Anja

    Hi Anja,
    what you try to do is currently not possible, a feature not available in CDS views.
    Yet it surely is a feature, ABAP developers would like to see on the feature request list .
    Cheers,
      Jasmin

  • How do I preserve date/time when converting to DNG?

    When I convert a file to DNG format in Lightroom, it changes the capture time in the metadata to the current date and time, rather than copying the source data.  Is there a way in which I can tell it to leave my dates alone?

    You wrote: “I noticed my mistake. It seems that what the conversion process was changing was the ‘Metadata Date’ and ‘Date Time’ fields, but ‘Date Time ‘" and ‘Date Time Digitized’ were remaining the proper, original date. The cause of my confusion was that my Metadata panel view was hiding all but the ‘Date Time’ field, so it appeared as though it had overwritten my prior date information.”
    Date/times in photo metadata can be very confusing.  There are all these fields with confusing names imposed by legacy industry standards -- I challenge anyone except a metadata nerd like me to remember the meanings and differences of Date/Time, Date/Time Original, Date/Time Digitized, Capture Time, Metadata Date, Modify Date, ....  The worst are the two XMP fields Date Created and Create Date, which have two very different meanings!  And every app and operating system makes a half-hearted attempt to "simplify" it for the user by choosing different names.   
    LR makes no serious attempt to make this easy for the user, and over the first several versions, it's handling of date/times was pretty sloppy. (It still has outstanding bugs that don't crop up that often in practice.)  You would think that at least LR would provide roll-over tool tips on the field names in the Metadata panel explaining each field.

  • Power Query Language Functions to Convert a UTC Date/Time to MST

    I need to convert an implicit UTC date/time value to a MST date/time value.  I thought that I could achieve this by converting to an ISO 8601 compliant text representation of the MST date/time value (see below) and then back to a date/time value, but
    it's not working ...
    DateTime.FromText( DateTime.ToText( [MIN_REVSN_RLSE_DTTM] ,"yyyy-MM-ddThh:mm:ss-07")) 
    Is this a bug?  Or, is there a better way to do this?

    That's a fixed duration. Why not just [MIN_REVSN_RLSE_DTTM] + #duration(0, -7, 0, 0)?
    EDIT: For what it's worth, I don't think PQ ever does anything with DST anyway. This has both good points and bad points.

  • UTC Date Time and Normal Date Time

    Hi All,
    1. How UTC date time and Normal date time differs in siebel.
    2. If legacy data needed to be loaded into siebel, in siebel few fields are date time and UTC date time fields. what would happen if we load both normal date time and UTC date time without considering them techinically?
    3. UTC date time holds any specific format in physical database? If we want to load legacy data to UTC date time format what is the query to manipulate it?
    Thankyou
    Sean

    Sean,
    Please check document below, I believe it has most of the answers to the questions you have:
    http://download.oracle.com/docs/cd/E14004_01/books/GlobDep/GlobDepUTC.html
    Hope it helps,
    Wilson

  • Calculate the difference between two dates times in infopath form 2013

    Hi,
    I have an infopath 2013 form that contains three fields:
    2 date time and the 3rd contains the difference between the two in hours
    how I can make the difference between the two so that the display will be like this:
    Date Time1           08/21/2014           22:00 
    Date Time2           08/22/2014           1:00
    Diff Field                                             3:00

    Hi,
    Please refer to the following article which matches your requirement exactly.
    Calculate the difference between two date picker controls in InfoPath using rules and formulas - no code!
    Please mark it answered, if your problem resolved.

  • How to Convert a 'Time Stamp' field to Date......?

    Hello Gurus:
    I am using a Generic Extractor to pull some stats data.  One of the field is a Time Stamp field, which tells me the date and time of Transaction run.  I only need "Date".  I tried mapping the stamp field to Date info-object hoping for a automatic determination of the Date.... (like the Week, Month happens).   Well, I was wrong! 
    I only need the Date part.  How can I do this in BI?  is teher a specific SAP info-object for this that could be used..?  If not, what would be the routine.  The Date field that comes in is a SAP format... "u201C20,070,505.0000000u201D meaning the Transaction was run
    on 05/05/2007.  How can I get this convertion in BI transaformation??  is there a special Routine...?
    Also, we are trying to use "Time Stamp" based DELTA from a generic extraction.  The field used is a System date with time stamp.  Value looks like u201C20100,505,170,113.2918470u201D, u201C20100,505,170,113.4748820u201D  Every record pulled has a different value... probably giving me the time at which the record was created or generated.  Now, with DELTA specifid on this type of field, what will be the 'Current Status" value in Delta queue?  Is it based on the last record created and will that be the highest value?  For next Delta extraction, does it compare the extraction time stamp to this value and bring in those records that are greater than the 'Current Status" value?  I guess with that we can have as many deltas as we want..... right??  is there any particular setting I should be doing or worried/ careful about setting etc....?
    Thanks for the response in advance....
    Best..... SMaa

    Thanks Akshay,
    Your suggestion worked right away and was probably the easiest of all.  I tried the Function Module that Pravender suggested,
    but it did not return me the date back.... may be because it does not need the Micro Seconds in it....?  Routine as suggested by
    Satyam might work too, but did not go there....! 
    Thanks to all of you.
    Best..... SMaa

  • Convert field value from Double to Date Time.

    Hi Every one.  Its been a long time since i have last posted on this site but i need some help again please.
    I want to convert a field value (saved as format double) to a date time value. I dont seem to find a function
    that can do this can anyone give me some help please?
    A sample would be to convert 40249.6588697106 to the format of Date Time being 12/03/2010 15:48:46
    Any suggestion?
    Thanks

    You can define a date time variable and use CDateTime.
    dim d as DateTime
    d = CDateTime(40179.55833333)
    formula = d

Maybe you are looking for

  • Question about Illustrator CS5 making eps files in Mac OS 10.9.5

    Hello, I am using Illustrator CS5 on a Mac. I just updates to Mac OS 10.9.5 Now when I save an simple Illustrator vector file as an eps, and place it in another program (such as Quark), I get code instead of the preview image. I tried older vector ep

  • Problem with Interactive Report

    Hi, I am trying to create a report using five tables named 1.Servers:- server_id,name,service_id,type_id 2.Servertype:-type_id,name 3.CPUutilzation:-cpuutil_id,server_id,parameter_id,average,maximum,timestamp 4.MEMutilzation:-memutil_id,server_id,par

  • Faulty mobile Lenovo K900

    I bought lenovo k900 mobile last year 19oct2013.I want to say that I bought a headache. After 1 months there was battery problem. I went to Lenovo repair center and they repaired it. After 5 months the screen of mobile stopped working, I went to the

  • Mail.app fails after a few minutes

    I've gat a really annoying Mail.app behaviour. The setup: Mac OS X 10.4.10 server with Xserve RAID (broken mirror ...) Mac OS X 10.4.10 client user has home directory on the RAID auto-mounted per afp Mail.app V2.1 The app is run on the client, the ho

  • If the system won't start at all

    I have just purchased a MSI K8Neo 2 FX S939 motherboard second hand. I have installed a AMD 64 3200+ CPU and two 256M DDR333 memory modules and a NVIDIA 6600GT 256M DDR3 videocard on it. These components worked prior on another motherboard. Now the s