I have a table with the following properties:
U. TRADE_ID VARCHAR2
TRADE_DATE DATE
PRICE NUMBER(10.2)
Once I get the data from a file, the colums are as follow:
PSD231,03051982,50.1
My question is, are those the correct datatypes for the table? I'm getting the input from the file for the first sql column as a String. But I can't display it. I think I'm missing something in the sql part. How can I create a new sql row filled with data so I can test it? This is what I tried and works:
insert into myTable(TRADE_ID, TRADE_DATE, PRICE)
values('PSD231', date '2011-01-01', 10.4);
but I'm trying create a query with the date in the format:
01-01-2011, not like the above
"What if I wanted to insert the values like this: 10062019 in the format ddmmyyyy instead of how I have it in the query: 2011-01-01 "
Just put single quotes around the date, '10062019', and make sure it is in the correct date format for your region (ddmmyyyy in your case).
insert into myTable (TRADE_ID, TRADE_DATE, PRICE)
values ('PSD231', '20110101', 10.4);
To change date formats, use the CONVERT function e.g
SELECT CONVERT(varchar, getdate(), 1)
There is a nice list for the convert function here
Since you have defined the first field as a varchar type, it should be more like,
insert into myTable(TRADE_ID, TRADE_DATE, PRICE)
values('PSD231','2011-01-01', '10');
Values should be enclosed into single quotes when they are being inserted.
Related
This has been asked many times before and I tried quite a bit before posting this question. I have 2 columns on my SQL Server table where the dates are stored as CHAR(8) yyyymmdd format. When I try to insert this to another table with date column, I get:
Conversion failed when converting date and/or time from character string.
The typecasting that I used is:
,CAST(convert(char(10),qrda.BillFromDate,120) AS DATE) AS [BillStartDate]
,CAST(convert(char(10),qrda.BillToDate,120) AS DATE) AS [BillEndDate]
With the above code, I am trying to make it SQL Server readable date format and then typecasting it to DATE so that I can insert it to the destination table without any other transformations. Not sure where I am going wrong with this.
Since date are stored in the following format yyyyMMdd you can insert these values without any need to use CONVERT or CAST functions, since this format can be implicitly converted to DATE.
If the data contains invalid values such as 00000000, you can use TRY_CONVERT or TRY_PARSE function to convert these values to NULL
Example:
CREATE TABLE #TBLTEMP(datecolumn DATETIME)
INSERT INTO #TBLTEMP(datecolumn)
VALUES ('20180101')
INSERT INTO #TBLTEMP(datecolumn)
VALUES (TRY_PARSE('00000000' as DATETIME))
SELECT * FROM #TBLTEMP
Result:
From the example above, you can see that 20180101 was inserted succesfly without any casting, while TRY_PARSE function converted the invalid value 00000000 to NULL.
You can use the following syntax:
INSERT INTO TargetTable(DateColumn)
SELECT TRY_PARSE([CharColumn] as DATETIME
FROM SourceTable
References
Understanding SQL Server’s TRY_PARSE and TRY_CONVERT functions
TRY_PARSE (Transact-SQL)
Sorry for the trouble folks, but looks like this is bad production data where some values are literally '00000000'. I dont know how this flows into our system, but since the source table columns are CHAR(8), this is a valid value. Not so much for me.
I'm trying to insert some data into a Teradata database, this information has been originally exported from an Oracle instance, but I have a little problem with the dates, here's an example of the data:
CO_ID | CUSTOMER_NAME | JOIN_DATE
1022945 | John Carpenter | 07/03/2018 01:55:24 p.m.
And this is the create table statement:
CREATE TABLE transact (
co_id varchar(50),
user_name varchar(50),
join_date date);
Teradata is throwing error every time I execute the insert statement for example:
expected something between a string and a unicode character ...
How can I insert the information keeping the original format of the date, I have to modify the create table or there's any other trick?
thank you.
In Teradata a date is a date (without the time portion). For the insert you have to convert the input string into a valid date.
Somthing like
select cast('07/03/2018' as date format 'DD/MM/YYYY');
select cast(substr(input.join_date,1,10) as date format 'DD/MM/YYYY');
If you cast a string as date then the format clause is a description how the string parts are used to convert into a internal date format.
If you select from a date column the format clause is used to describe the wanted output format.
select cast( cast('07/03/2018' as date format 'DD/MM/YYYY') as date format 'YYYY-MM-DD')
If you add a format clause to your table definition join_date date 'DD/MM/YYYY', you define the default format for that column, which is used as the output format whenever no explicit format is given.
If you want to use the time portion too, your target column needs to be a timestamp
select cast( cast(regexp_replace('07/03/2018 01:55:24 p.m.','\.m\.', 'M')
as timestamp format 'MM/DD/YYBHH:MI:SS BT') as timestamp format 'YYYY-MM-DDBHH:MI:SS')
format phrase documentation
I have a CSV file Date column with the following values:
StartDate
---------------------------
01/02/2014 0:00
09/04/2013 0:00
I want to extract only the date part from the above column value. I created the table with data type as Varchar, because if I declare it as DATETIME then I am not able to perform a bulk insert.
Date part alone you can extract using LEFT function.
(provided all your data in the format as you specified above)
select LEFT(colname,10)
You can BULK INSERT into a table where you will keep the data for intermediary storage. Try something like:
SELECT id, LEFT(CONVERT(VARCHAR, dateCol, 103), 10) FROM temptbl
Take a look at the following MSDN page for date formats available to you with CONVERT():
http://msdn.microsoft.com/en-us/library/ms187928.aspx
get the date part by splitting by "space"
cols[col_num].split(" ")[0]
and then make insert statement.
Why the table has datatype varchar?? why not date?
I have a column named BillsOfMonth (nvarchar) in which i have only Month and year values in format Jan-2014, Feb-2014 , Mar-2014
I want to convert it to Date Format and order by Date.
i have tried
select rid
, name
, billsofmonth
from reports
order by
CONVERT (datetime, "1-" & billsofmonth)
Since "1-" & billsOfMonth which is Jan-2014 will become '1-Jan-2014'.
But convert will work only in Columns. How can i concatenate Constant String to column BillsOfMonth and convert it to date?
I can not make Function and T-Statement. I Can Only Use Query.
You should use single quotes instead of double quotes, and also use + not & to concatenate strings:
ORDER BY CONVERT(DATETIME, N'1-' + billsofmonth);
For what it's worth, there is no need to store this as NVARCHAR, VARCHAR would do just fine as there are no non-ascii characters in the format you are using. This article on choosing the wrong data type is a useful read. Although better still would be to store it as a date, making it the first of month, then put in a check constraint to ensure only the first day of the month is used. e.g
CREATE TABLE Reports
(
BillsOfMonth DATE NOT NULL,
CONSTRAINT CHK_Reports_BillsOfMonth CHECK (DATEPART(DAY, BillsOfMonth) = 1)
);
This provides much more flexibility with comparison, and ensuring data integrity i.e. I with your column I could enter 'xxxxxxx', which would go into the column fine, but when you came to run the convert to datetime an error would be thrown. The check constraint would stop any invalid entries, so if I ran:
INSERT Reports (BillsOfMonth) VALUES ('20140502');
I would get the error:
The INSERT statement conflicted with the CHECK constraint "CHK_Reports_BillsOfMonth". The conflict occurred in database "TestDB", table "dbo.Reports", column 'BillsOfMonth'.
Then if you will often need your date in the format MMM-yyyy you could add a computed column:
CREATE TABLE Reports
(
BillsOfMonth DATE NOT NULL,
BillsOfMonthText AS LEFT(DATENAME(MONTH, BillsOfMonth), 3) + '-' + DATENAME(YEAR, BillsOfMonth),
CONSTRAINT CHK_Reports_BillsOfMonth CHECK (DATEPART(DAY, BillsOfMonth) = 1)
);
Example on SQL Fiddle
What if you try like this
select rid,
name,
convert (datetime, (N'1-'+billsofmonth)) as NewBills
from reports
order by NewBills
I found what i am doing wrong. In Sql Server we use single inverted comma (') for constant expression not double inverted comma (").
So, It will be
SELECT [rid] ,[name], [billsofmonth] FROM [reports] order by CAST ( ('1-' + billsofmonth) as datetime)
I'm using Oracle 11g Schema
I want to select the month and day from a date concatenate it and put it into a column.
The syntax I have here makes sense but its throwing an error of..
INSERT INTO OB_SELECT_LST12_SPG WED
VALUES (((TO_CHAR(TO_DATE('RET_DATE', MM))||( TO_CHAR(TO_DATE('RET_DATE', DD)));
"SQL Error: ORA-00907: missing right parenthesis"
Any help is greatly appreciated.
Thanks.
Some points first...
Your table name has a space in in
Your not enough columns error is caused by you having more than one column in the table
You really shouldn't be doing this at all
If ret_date is a column don't encapsulate it in quotation marks (') as you won't be able to convert the string 'ret_date' into a date.
However, assuming ret_date is a string that looks like 13-06-2012
insert into ob_select_lst12_spg_wed (my_column)
values(to_char(to_date(:ret_date,'dd-mm-yyyy'),'mmdd'));
If ret_date is a date data-type then you can remove the inner conversion.
insert into ob_select_lst12_spg_wed (my_column)
values(to_char(:ret_date,'mmdd'));
to_char and to_date both make use of datetime format models, which have a number of options.
Please don't do this though. Always store dates as dates
From your comments ret_date is a date and the column wed is a character. You're getting the error bind variable not declared because you haven't specified the variable. I'm going to assume that ret_date is in another table as it's a date, in which case lose the values key-word and insert directly from that table:
insert into ob_select_lst12_spg (wed)
select to_char(ret_date,'mmdd')
from the_other_table
This shouldn't be required though, you can always convert a date into whatever you want on exit from the database. If you don't store it as a date in the database then it's easy for errors and incorrect values to creep in. I personally would change the column wed to a date, in which case your query becomes the very simple:
insert into ob_select_list12_spg (wed)
select ret_date
from the_other_table
You can then use the correct datetime format model for your needs when selecting from the database.
Sample query to get date, month from date column:
Select ((To_Char(to_date('01-FEB-73','dd-mon-yy'), 'MM'))
||( To_Char(to_date('01-JAN-73','dd-mon-yy'), 'dd')))
from dual;
if RET_DATE is date datatype then use
insert into table_name (columname)
Select ((To_Char(RET_DATE, 'MM'))
||( To_Char(RET_DATE, 'dd')))
from dual;
You are missing extra parentheses. Number of opened ( was 8 and closed ) was 5
INSERT INTO OB_SELECT_LST12_SPG(WED)
VALUES ((TO_CHAR(TO_DATE(RET_DATE, MM)))||( TO_CHAR(TO_DATE(RET_DATE, DD))));
Update
Make sure other columns are not required (i.e. Allow NULLs)