How to INSERT to a column name in Snowflake
CREATE TABLE TEST
(
TO DATE,
FROM DATE
)
I am getting the error:
SQL compilation error: syntax error line 3 at position 0 unexpected 'TO'. syntax error line 4 at position 0 unexpected 'FROM'
The statement is
CREATE TABLE TEST ( "TO" DATE, "FROM" DATE );
However, I would recommend a column name like TO_DATE or FROM_DATE instead of using the keyword FROM. If you SELECT from the table, you also need to use "" as a consequence.
Use double Quotes:
CREATE TABLE TEST ( "TO" DATE, "FROM" DATE );
Related
I have a column in my table that is of float type. The table was automatically generated when I imported the spreadsheet (Excel) data to my database. Thus there is a column I wish to change from float to varchar, but when I try to do this, I get an error:
'tblInvoices' table
Unable to create index 'IX_tblInvoices'.
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.tblInvoices' and the index name 'IX_tblInvoices'.
The duplicate key value is (1.00001e+006). The statement has been terminated.
It is a unique column, and set that way (not set as the primary key for reasons). I have already run queries to search for and delete duplicate fields but there are none. The query I ran as follows:
WITH CTE AS
(
SELECT
Invoice,
RN = ROW_NUMBER()OVER(PARTITION BY Invoice ORDER BY Invoice)
FROM
dbo.tblInvoices
)
DELETE FROM CTE
WHERE RN > 1
So the value within the Invoice column is 1000010 and when I run the following query a single row is found.
SELECT *
FROM [TradeReceivables_APR_IFRS9].[dbo].[tblInvoices]
WHERE Invoice = 1.00001e+006
Note that I have searched for the value in the error, 1.00001e+006, and not 1000010.
So my question is why does the DBMS do this? Why does it change the value like that? When I remove the column, it does it with another column and so on and so on (about 40 000 rows in total). How can I change the column from float to varchar without changing the data and getting errors?
Any help will be greatly appreciated!
It seems that the field is an integer so you can Cast it to BIGINT before cast to VARCHAR
Declare #Invoice as float = 1.00001e+006
print cast(#Invoice as varchar) -->> Result : 1.00001e+006
print cast(cast(#Invoice as bigint) as varchar) -->> Result : 1000010
I'm trying to add a calculated field to an existing table in SSMS, which will convert a string in the format YYYYMMDD to a date format, but I am getting errors regarding the string field not being valid.
I require the calculated field as I have '00000000' values (i.e. NULL) in the string field so can't use this in date calculations.
The code I'm using is :
ALTER TABLE [TEM].[AssignmentRates]
ADD [Date_Expired] DATE NULL
(SELECT CONVERT([date], CASE WHEN [Expiry_Date]='00000000' THEN NULL ELSE [Expiry_Date] END))
where [Expiry_Date] is the string column I'm trying to convert, and [Date_Expired] is the name of the calculated column I'm trying to add.
I get this error:
Invalid column name 'Expiry_Date'
against both instances of that field name, and can't work out why. If I run the query as a stand alone SELECT it returns the required results.
Using table aliases or the full database, table and column name for it don't appear to work either.
It's probably something incredibly obvious, but I haven't been able to work out what it is.
The error on expiry_date seems quite clear -- that is not the name of a column in the table. But you can simplify the logic:
ALTER TABLE TEM.AssignmentRates ADD Date_Expired AS
(TRY_CONVERT(date, Expiry_Date));
Actually, the nested SELECT may have caused an issue. That would not normally be used for a computed column.
Looks like a syntax issue
alter table [TEM].[AssignmentRates]
ADD [Date_Expired]
as
(
case Expiry_Date when '00000000' then null else cast(Expiry_Date as date) end
)
Your syntax was invalid for a computed column. What you had was actually adding a regular column (successfully) but then the attempting to run a select statement which was causing those column-name errors as it didn't have a from clause for context.
ALTER TABLE TEM.AssignmentRates /* don't do this */
ADD Date_Expired DATE NULL /* implied end of statement */
(SELECT CONVERT(date, CASE WHEN Expiry_Date = '00000000' THEN NULL ELSE Expiry_Date END));
Had you used the proper syntax, you'd at least get an informative error:
ALTER TABLE TEM.AssignmentRates /* error! */
ADD Date_Expired AS
(SELECT CONVERT(date, CASE WHEN Expiry_Date = '00000000' THEN NULL ELSE Expiry_Date END);
/*ERROR: Subqueries are not allowed in this context. Only scalar expressions are allowed.*/
But really you didn't need a subquery in the first place:
ALTER TABLE TEM.AssignmentRates /* success */
ADD Date_Expired AS
CONVERT(date, CASE WHEN Expiry_Date = '00000000' THEN NULL ELSE Expiry_Date END);
Gordon has a point about just using try_convert() though.
I have created table as:
create table ot.eligible(
id number,
name varchar2(255),
join_date date,
left_date date
);
The problem I am getting is I am unable to insert the data which has date column:
insert into ot.eligible(ID,NAME,JOIN_DATE,LEFT_DATE) values(1,'ashwin',to_date(01/12/2017,'MM/DD/yyyy'),to_date(01/2/2018,'mm/dd/yyyy'));
before i tried without using to_Date and numeric found error was there so,I added to_date but,at this time I got error as:
ORA-01858: a non-numeric character was found where a numeric was expected
My sysdate is:
10/17/2019 8:42:29 PM
when I hitted select sysdate from dual;
You are missing single quotes in dates -
insert into ot.eligible(ID,
NAME,
JOIN_DATE,
LEFT_DATE)
values(1,
'ashwin',
to_date('01/12/2017','MM/DD/yyyy'),
to_date('01/02/2018','mm/dd/yyyy'))
I'm trying to create an sql statement which involves adding up a total of a cost field between two dates and where a customer id is to a certain value. My statement currently looks like this:
SELECT SUM(COST) AS TotalCost
FROM ORDERS
WHERE ( DATE BETWEEN '01/01/2012' AND '09/25/2015' )
AND WHERE CUSTOMERID = '23'
However, I get an error when I run this which says:
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "WHERE" at
line 1, column 98.
I know that it is a syntax error in my statement but I'm not sure how it should be written to prevent this from occurring
Leave out the second WHERE
WHERE (DATE BETWEEN '01/01/2012' AND '09/25/2015') AND CUSTOMERID='23'
This version should work; extra where clause, and I'm assuming your customerid is an integer, not a string.
SELECT SUM(COST) AS TotalCost
FROM ORDERS
WHERE (
DATE BETWEEN convert(datetime, '01/01/2012') AND convert(datetime, '09/25/2015')
)
AND CUSTOMERID = 23
I have a table called calendars.
One of its columns is named 'date'
When I want to select the date column it gives error ORA-01747 namely invalid table.column.
select date from calendars
I guess this happens because 'date' is a reserved word for pl/sql. The problem is it's not even possible to change the column name :
alter table calendars rename column date to date_d
Result is: ORA-00904 error: invalid identifier.
What do you advice?
Thanks.
Have you tried
select calendars.date from calendars; /* or you could alias "calendars" if you don't want to type so much */
If that doesn't work or help, have you tried dropping the column (and maybe try referencing it with the table name prefix: calendars.date)?
I also found this post: How do I escape a reserved word in Oracle?
It seems that Oracle will be case-sensitive if you use double quotes so
select "date" from calendars;
is not the same as
select "Date" from calendars;
Try escaping the reserved word with double quotes.
select "date" from calendars
date is a reserved keyword and hence cannot be used like
SELECT date from some table
there can be multiple solutions for the problem
The date column needs to be enclosed within the brackets like
SELECT [date] FROM tableName
Enclose the reserved keyword in backticks
SELECT 'date' from tableName
Use alias
SELECT tableName.date from tableName