Trouble converting CSV date from non standard format SQL - sql

I have dates in the format 01jan2020 (without a space or any separator) and need to convert this to a date type in SQL Server 2016 Management Studio.
The data was loaded from a .CSV file into a table (call it TestData, column is Fill_Date).
To join on a separate table to pull back data for another process, I need the TestData column Fill_Date to be in the correct format (MM-DD-YYYY) for my query to run correctly.
Fill_Date is currently in table TestData as datatype varchar(50).
I want to either see if it is possible to convert it with TestData table or directly insert the result into a 2nd table that is formatted.
Thanks (NEWB)

I ended up solving by converting the data while dropping into a temp table, deleting old value, and then inserting from that table back into the TestData table.
CONVERT(VARCHAR,CONVERT(date,[fill_date]),101) AS fill_date

Related

updating one table in database 1 to a table in database 2 receiving converting data error

I have two tables in different databases. The table to update is jomast in the database where the SQL code lives. The second table is in Scheduling_data DBO as testtable.
Additionally, I know that once I get the data from the source table into the correct format that I will need to use wildcards as the incoming data is stated as, e.g. 32, while the receiving table has the data as a varchar10 showing as, e.g. 00031-0000.
So I can do one of two things, either cast the change in the code, listed below, as the link or create an additional column in the source testtable and write code to change the incoming column to another column changing the format from; 31 to 00031-000.
Here is my code that is erroring out with a 8114 message not able to convert varchar to float.
update jomast
set frel_dt = T2.releasedate
from Scheduling_Data.dbo.testtable as T2
where cast (jomast.fjobno as varchar(20)) = T2.job
I realized that my cast was in the wrong location. Moved it to the T2 data and it worked. Would like help on the other part of this post though.

Data type for date in PostgreSQL

I've exported a CSV file from Excel which has date in format ddmmyyyy hmm. I'm using the COPY function to import to a table in PostgreSQL.
Since I only want to retain the date part I tried the date data type:
CREATE TABLE Public."ride_details"(ride_id int,created_at date)
COPY Public."ride_details" FROM '/tmp/ride_details.csv' DELIMITER ',' csv HEADER;
But that resulted in:
ERROR: date/time field value out of range: "26/07/19 5:48"
HINT: Perhaps you need a different "datestyle" setting.
CONTEXT: COPY ride_details, line 2, column created_at: "26/07/19 5:48"
SQL state: 22008
Do I need to specify a different data type or how to make this work?
COPY is rather unforgiving with invalid input. (This way it can be fast an reliable.)
It may be enough to set a matching datestyle setting:
SET datestyle = 'ISO,DMY'; -- DMY being the relevant part
... and retry. (Sets the setting for your session only.) Related:
Importing .csv with timestamp column (dd.mm.yyyy hh.mm.ss) using psql \copy
Info in your question is not entirely clear, you may have to do more:
copy to a temporary "staging" table with a text column, and INSERT to the actual target table from there using to_date() - with a custom pattern specifying your non-standard date format:
CREATE TABLE public.ride_details(ride_id int,created_at date); -- target table
CREATE TABLE pg_temp.step1(ride_id int, created_at text); -- temporary staging table
COPY TO pg_temp.step1 ...;
INSERT INTO public.ride_details(ride_id, created_at)
SELECT ride_id, to_date(created_at, 'DD/MM/YY') -- or whatever
FROM pg_temp.step1;
to_date() ignores dangling characters after the given pattern, so we do not have to deal with your odd hmm specification (hh?).
I went with the YY format displayed in the error msg, not the yyyy you claim at the top. Either way, the input must be in consistent format, or you need to do more, yet ...
All in a single DB session, since that's the scope of temp tables. The temp table is not persisted and dies automatically at the end of the session. I use it for performance reasons.
Else you need a plain table as stepping stone, which is persisted across sessions and can be deleted after having served its purpose.
Related:
How to ignore errors with psql \copy meta-command
How to update selected rows with values from a CSV file in Postgres?

Alter Column type in Teradata

I have an export job from Datameer going through to HIVE. The issue is that we were told that HIVE converts Date columns to string. I am feeding the data from HIVE to Tableau and the issue is that the Date column being converted to string is completely throwing off my data.
I am looking to convert/alter my existing column "Posting_Date" from String to Date... HIVE is based off a Teradata interface so I am trying to find a command which will let me convert this column back to Date format..
I tried the following:
ALTER table Database.Table1
ADD posting_date date(4)

Convert datatype of existing data from Char to DateTime with SQL

I am relatively new to SQL Server and I am trying to update the datatype of about 3000 records from a Char to Datetime so I can use the DATEDIFF function. I created a view that achieves the conversion but what I think I need to do is alter the data in the origin table.
SELECT
CONVERT(datetime, CONVERT(char(8), TRANS_ACCOUNTINGDATE_ALLCAMPAIGNS_2010_ALLPROCESSINGACCOUNTS_ALL))
FROM Accounting;
What I think I need to do is an alter table and iterate over each row performing the conversion. Trying to change the data type using the GUI is not working for me.
Any help would be appreciated.
Thanks
The datatype is an attribute of the COLUMN, not just of the data inside the column. You can't put datetime data into a char field - that's the purpose of data types!
You need to add a new field and run an UPDATE statement to populate it with your converted data. Then you can drop the original field and rename your new one to the original name.

Convert a string to a date in Access

I'm migrating data between tables in Access 2003. In the old table, the date was stored as a text field in the format YYYYMMDD.
I want to store the field as a datetime in the new table. I've tried using CDate() in my SQL statement, but it just displays as #Error in the results.
What am I doing wrong?
e.g. cdate(format("20091231", "####/##/##"))
So, in your case it will be
SELECT cdate(format(mystringFieldThatIsInYYYYMMDDFormat, "####/##/##"))
FROM myData