"date" as a column name - database

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

Related

Column name Arguments in the "With" Statement in SQL Server

From the Microsoft online document (https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver15), it mentioned that the "With" statement can have a column name as an argument, and then it says that:
"The list of column names is optional only if distinct names for all resulting columns are supplied in the query definition."
What is "if distinct names for all resulting columns are supplied in the query definition" actually means? I use the "With" statement very often, but I never specify column names in the argument.
I tried to go through the entire document but it appears nowhere have explained this in further detail.
Does anyone know under what situation I need to put specify the column name?
Thanks in advance!
Quite simply, the resultset of the query that defines the CTE must return a set of columns with distinct names. For example, the following will not work:
with cte as (select 1 as x, 2 as x)
select * from cte;
The resultset has 2 columns named "x". In such a case, you MUST supply the column names in the definition of the cte since the query produces a resultset with duplicate names. So you would need to use the form:
with cte(x, y) as (select 1 as x, 2 as x)
select * from cte;
As a general matter, it is a best practice for any resultset to NOT have duplicate column names.

Adding a calculated field to convert string value to date in T-SQL

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.

In SQL Server what's the difference between selecting a column as Year and [Year]?

Do we add '[]' just to differentiate the system defined keyword from our own ... ? Or is there any other reason behind using '[]'? Thanks!
The use of square brackets in t-sql is to allow you to use reserved words as object names or aliases.
However, I would recommend not using reserved words to begin with.
Here is my prefered method of avoiding the use of reserved words.
say you want to name your table my table only way to do this is to put it like so: [my table] . Same principle applies for other database objects.
create table [my table] (
column1 varchar(30), column2 varchar(50))

Search string which contains specail characters(!,#,#./,comma(,))

I have a column in DB where it may contain data with special characters.
for ex:a column,Name may have data as following
santosh's
santosh/s
santosh%s
How to search for these values in DB using like operator.
Select * from table where name like '%santosh%';
How to change the above query to search for apostrophe(santosh's)
for 1. apostrophe s you can use
Select * from table where column like '%santosh''s%'
for 2. /s you can give directly
Select * from table where column like '%santosh/s%'
for 3. %s you can use
Select * from table where column like '%santosh[%]s%'
hope this helps.....
Just use 2 apostrophe to escape the character
Select * from table where name like '%santosh''s%';

Setting alias name from a subquery in SQL

In my Select query I just want to to set the alias name of a column based on a sub-query (that is, a value in another table). Is this possible in SQL Server 2008?
Like:
SELECT tax_Amt AS (SELECT tax FROM Purchase.tblTax WHERE tax_ID=#tax_ID)
FROM Table
Any way to achieve the above query?
No, you cannot dynamically set an alias or column name in standard SQL.
You'd have to use dynamic SQL if you want: but note the alias applies to the column therefore all rows have the same alias. You can't vary the alias row by row
Personally, I'd have an extra column called "TaxType" or such because it sounds like you want to vary the name per row. I'd do that anyway even if all rows have the same alias so my client code expects "TaxType"
Try like this:
SELECT (SELECT tax FROM Purchase.tblTax WHERE tax_ID=#tax_ID) AS tax_Amt
FROM Table

Resources