I have an excel sheet which has a time column. Its time column is currently in the data type 'Time' (6:00:00PM), however, I've tried with 'Custom' data type (6:00PM) as well.
I read this cell value using openXML library as follows:
row.XCells[9].GetValue()
The value I read is .75. This is the value I see when I change the data type to number. I want to convert this to a timespan in my C# backend. How do I do that?
var ts = TimeSpan.Parse(row.XCells[9].GetValue());
doesn't work.
Excel dates and times can be converted to C# DateTime using DateTime.FromOADate. Once you have the DateTime you can use the TimeOfDay property to get a TimeSpan (the DateTime will have a date of 30-Dec-1899 which is the OLE automation base date)
TimeSpan t = DateTime.FromOADate(row.XCells[9].GetValue()).TimeOfDay;
Console.WriteLine(t.ToString(#"hh\:mm\:ss")); // prints 18:00:00
Related
I have a column where timestamp is 5/23/2022 8:45:34 PM. I want to create a new column with same data as old column but in UTC format 'yyyy-MM-dd HH:mm:ss' and this new datetime format is 7 hours behind UTC (UTC-7)
I tried doing in azure data factory derived column using toTimestamp before it converts to UTC but it always fail.
toTimestamp(column_name,'yyyy-MM-dd HH:mm:SS')
but it did not work and the result always NULL.
Can anyone help this data conversion to UTC ?
The reason you are getting null values for the newly added columns is because the format you specified in the toTimestamp() function is incorrect. The following is the sample data that I used to reproduce this issue. The date column here is of type String.
While using Derived column in the dataflow to create a new date column of timestamp type, write toTimestamp(date, 'MM/dd/yyyy hh:mm:ss a', 'UTC') expression as the value for this new column. Here, date is the column you want to convert to new date column and MM/dd/yyyy hh:mm:ss a is the format of the values in date column (a represents the AM/PM). You can also pass time zone value like UTC, GMT etc. which is optional.
The following is what the result looks like, with a new column of timestamp type. You can use this resulting data to perform further conversions in dataflow.
The development is in Azure Data Factory -- Data Flow
I am getting an input file with various columns and one column with DateFormat ('MM/dd/yyyy'T'HH:mm:ss').
I am trying to convert the above DateFormat to toTimestamp('yyyy-MM-dd HH:mm:ss.SSS')
I have tried with the below format in Derived Column tab on the particular column needed in sink below is the Expression used to convert such case.
iifNull(toTimestamp(<string_column_name>,'MM/dd/yyyy\'T\'HH:mm:ss'), toTimestamp(<string_column_name>,'yyyy-MM-dd HH:mm:ss.SSS'))
For reference i am attaching the sample Date format got in the input file 01/26/2018 00:00:00.
Ref 4, should be converted to the format as 2018-01-26 00:00:00.
The format of date 01/26/2018 00:00:00 you provided is 'MM/dd/yyyy HH:mm:ss' which isn't contained in your expression. This leads to you got Null. If your column also has 'MM/dd/yyyy'T'HH:mm:ss' and 'yyyy-MM-dd HH:mm:ss.SSS' format, you can try this expression:
iifNull(toTimestamp(<string_column_name>,'MM/dd/yyyy\'T\'HH:mm:ss'), toTimestamp(<string_column_name>,'yyyy-MM-dd HH:mm:ss.SSS'),toTimestamp(<string_column_name>,'MM/dd/yyyy HH:mm:ss'))
Data preview:
I'm trying to convert a datetime column in format '2020/08/14 12:30:42' to a datetime column with format '14-August-2020 12:30:42' using the following function:
select
*,
date_format(starttimecet,'%d-%M-%Y %h:%i:%s) as test
from table
While this function in successful in doing that, the '14-August-2020 12:30:42' format of the test column no longer recognizes it as a datetime column. Is there a way to have a column in this format but still have it recognized as type datetime?
No, once you convert your bona fide datetime literal into a text string, it is no longer datetime. Your current query is actually along the lines of what you should be doing in general. Specifically, you should keep the original datetime column throughout your query as needed, and then display the text you want in the final outer select.
I have a date column data type as object and I want to convert it to datetime data type. when I am trying to parse date while loading or trying doing this after loading using --pandas.to_datetime-- its taking too long to convert it since rows are in 100s of millions. Could someone help changing date from object to date type more efficiently and faster
Using pandas, the only option to convert object to date-time is pd.to_datetime as it can't be done while reading the dataframe.
I guess you're using the defaults:
df['Date_Time'] = pd.to_datetime(df['Date_Time'])
It'll be much faster if you specify the format of date-time while using pandas.to_datetime conversion.
timefmt = "%Y-%m-%d %H:%M:%S"
df['Date_Time'] = pd.to_datetime(df['Date_Time'], format = timefmt, errors='coerce')
I have to import data from Excel file to SSIS but i am facing a problem in date column,in excel sheet date format is yyyy/mm/dd and when it gets upload in database it get change into yyyy/dd/mmm format.
How to fix this?
Use the SUBSTRING function in the derived column while importing the date,
(LEN(TRIM(SUBSTRING(ReceivedDateTime,1,8))) > 0 ? (DT_DBDATE)(SUBSTRING(ReceivedDateTime,1,4) + "-" + SUBSTRING(ReceivedDateTime,5,2) + "-" + SUBSTRING(ReceivedDateTime,7,2)) : (DT_DBDATE)NULL(DT_WSTR,5))
If the Data is there then use Substring function to extract the exact date that sets in the DB or if the date does not exist then insert NULL in the DB.
I see two options:
Data Conversion Transformation to convert to a text string in
the appropriate format. Using SSIS data types.
Add a script task that converts the data type. Using VB data types
First Create Table into Your Database Using below Command
CREATE TABLE [dbo].[Manual] (
[Name] nvarchar(255),
[Location] nvarchar(255),
[Date] datetime
)
SET DATEFORMAT YDM
By using DATEFORMAT YDM ,Date Will import in YYYY/DD/MM Format .Before runnung package modify the package and at the time of Column mapping select The Check Box "Delete Rows in Destination Table" .
Then Execute the Package . It Will work .