Convert String to Date in ADF Pipeline - u-sql

I am very new to Usql scripts and need to convert my Date field which is coming from CSV file as string in my blob storage.
My Blob is linked to DB table which contains a Date field. DB is not accepting String '02/02/2016' into DB date field.
In DataFactory Pipeline, how can I convert String '02/02/2016' to Date in DB (Blob -> Azure SQL DB).

Assuming you are trying to convert this value within your U-SQL script, you can do so using the C# DateTime.ParseExact function once you have EXTRACTed your data from the CSV as a string:
#data =
EXTRACT YourDateValue string
USING Extractors.Text();
#format =
SELECT DateTime.ParseExact(YourDateValue
,"dd/MM/yyyy"
,CultureInfo.InvariantCulture
) AS YourDateValue
FROM #data;
If possible however, you would be better served changing the format of the Date values that are stored in your CSV files.

Related

How should I format my string to pass it to my database via TIBCO BW6?

I'm new to TIBCO BW6. This is my scenario. I have a .csv file, and one of my columns is a string in this format: '31/08/2021 15:18:00'
I created a process that reads my file and inserts a new row into my database. I have a problem with the date.
In my palette JDBC Update, the date is a timestamp. When I match the input, the right pattern for matching my string in datetime is pattern yyyy-mm-dd hh:mm:ss.
To do this kind of thing the best solution is generally to use the SQL database formatting functions in your SQL query.
Something like this (for ORACLE) :
insert into table_name
(my_date_field)
values (TO_DATE('2022/01/23 11:25:44', 'yyyy-mm-dd hh24:mi:ss'));
In BusinessWorks the query would look like this and you would have to map the value of the field corresponding to the '?' with your actual timestamp :
insert into table_name
(my_date_field)
values (TO_DATE(?, 'yyyy/mm/dd hh24:mi:ss'));

Sqlite get the date part of a DateTime and return as a DateTime, not a string

In Sqlite I want to extract the date and time portions of a DateTime field separately in a view and return them also as a datetime, not strings. I've tried Cast, Date(), datetime(), but they all return strings.
I've read the SQLite documentation and understand how there is not an actual Date data type. Yet a Table field defined as DateTime is able to be parsed as a Date by an Excel query, but calculations on that field are not. I'm trying to do all data prep in the database view.
My data has the following field taken directly from the table definition:
LastModifiedDate datetime
I want the date (without time) to have the same DateTime data type as LastModifiedDate, not Text, because I use this view in many spreadsheets. I can apply Excel Date functions and formatting to LastModifiedDate field directly as returned from the ODBC query to Excel, and want to do the same to the Date-only part. I don't want to have to put a string-to-date conversion in every spreadsheet when I know it can get the date natively from Sqlite in LastModifiedDate.
SELECT LastModifiedDate,
date(LastModifiedDate) as Datepart,
cast(LastModifiedDate as numeric) as Date2
FROM Transactions
LastModifiedDate Datepart Date2
2019-07-28 18:22:38.9165394 2019-07-28 2019
LastModifiedDate in the above query is interpreted in Excel as a date to which date formats and date functions can be applied with no further processing required. Datepart above is returned as Text to Excel, and I can't apply date functions and formats without further pre-processing in Excel. I would like Datepart to be interpreted a date in Excel just as LastModifiedDate is.
I'm looking at the ch-werner.de sqliteodbc-0.9998. It will return an ODBC TIMESTAMP type only if the column decltype starts with timestamp or datetime. It returns ODBC TIME only for decltypes starting with time and ODBC DATE only for decltypes starting with date.
sqlite3 provides this decltype only for result table columns that are direct database column references. So if your SELECT statement has some expression that is more than a plain column reference, the decltype is lost. sqlite3 works like this at least up to version 3.39.0. It is documented.
The CAST expression converts the value of given expression to a storage classes by the determined affinity of the given declared type, but does not assign decltype to the result.
If you want to see the decltypes for query columns, you can use the sqlite3 cli and give it command .stats 2. Then it'll output the column declared types for each statement it executes.
If the decltype is found, the sqliteodbc-0.9998 will always parse string values into ODBC types. If DSN Option JDConv is enabled, it'll also parse floating point julianday values (whether provided as float or a string of a float) into ODBC types and when writing it'll write floating point into database.
If you can afford to change the schema, you can add a generated virtual column. This is cheap in storage, because data is not affected, but it costs when you query the column. This column can calculate other column into the values and decltypes you need for ODBC.
ALTER TABLE data ADD COLUMN
Datepart date AS (date(LastModifiedDate))
Then to get the Datepart, you simply query the column.
SELECT Datepart FROM data

How to Implement Timestamp in oracle?

create table abc_test(
id number,
rv timestamp
);
insert into abc_test(id,rv) values (1, CURRENT_TIMESTAMP);
Is there any better way to do it?
Can the timestamp be stored in the database in some binary format like "01x0000000000046565".
In Microsoft sql, this can easily be done as below :
create table xyz_test(id1 timestamp, name varchar(50))
insert into xyz_test (Name) values('person1')
select * from xyz_test
The above query will show some binary form for the timestamp column and "person1" in the name column
How to get the same result in the oracle?
It is indeed stored in a binary format -- the tool that you are using translates it when it is retrieved.
Display of dates and timestamps is dependent on tool configuration, and on session settings etc.. You can read the timestamp in whatever format you like -- consult the documentation for To_Char(datetime).

How to change date format of a datetime object?

currently, i have a datetime object
DateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider);
which successfully converts it into a datetime (from a string) to become for example :
7/6/2012 9:30:00 AM
How do i convert this to become 2012/07/06 09:30:00 (24hr format)? So that i can insert it into the database using C#??
PS: I'm using Sybase SQL Anywhere 12, and from what I've read, they neeed the format to be in year/months/day and the time to be in 24hr format right? Please correct me if I'm wrong.
The DateTime itself does not have a format. The date and time are stored internally as a number. Usually the classes of the database provider take care of converting a DateTime to the correct format.
If Sybase will only accept the date formatted as a string you will need to use the DateTime.ToString method and format it with the correct format string.
How are you building your insert command? Are you using database parameters or just building a string containing the insert statement?
SQL Anywhere 12 has a default date format of YYYY-MM-DD HH:NN:SS.SSS
This can be configured/changed with the timestamp_format database option however:
timestamp_format option
The setting can be permanently changed through SQL like:
SET OPTION PUBLIC.timestamp_format = '<format here>';
Or temporarily changed (per connection basis) like:
SET TEMPORARY OPTION timestamp_format = '<format here>';
Of course, if you already have a datetime object in your code, you should be able to pass the value into a parameterized query. It doesn't have to be passed as a string.

How to update DATETIME field with existing data from same table

I have an sqlite database which currently holds an integer field called Year which currently only stores the year. In future versions I want to store a full date and time.
I updated my table to include a FullDate field using alter table.
> ALTER TABLE Files ADD COLUMN UploadDate DATETIME DEFAULT 0;
Next, I want to migrate all the existing year fields to the new field. So I'm looking for something like:
> UPDATE Files SET UploadDate = (DATETIME('%Y-%m-%d', Year, 1, 1));
Unfortunately this doesn't seem to work as the result is empty. I also tried the date and strftime functions but they either result in incorrect data or empty data.
What's the proper way to update a DATETIME field with existing data in the same table?
The DATE and DATETIME functions don't have a format parameter.
For more: http://sqlite.org/lang_datefunc.html
The main catch is that SQLite does not have any date or time types, so that you might as well populate your field with:
UPDATE Files SET UploadDate = Year || '-01-01';
And that will do the exact same thing. Dates are not stored as typed, but can be evaluated as such against the date and time functions.

Resources