SQL Server Alter Table Query (Incorrect Syntax) - sql

I'm stumped, and I know this is probably something very simple. I am trying to add two columns to an existing table.
I am receiving the following syntax error:
Incorrect syntax near 'PublishedDate' Expecting '(', or SELECT.
Here is my SQL:
ALTER TABLE my_table ADD (PublishedDate DATETIME, UnpublishedDate DATETIME)

Try without the parentheses:
ALTER TABLE my_table
ADD PublishedDate DATETIME, UnpublishedDate DATETIME
Here is a sqlfiddle with a demo for you to try.

Related

Alter column length in SQL

ALTER TABLE tableName
MODIFY COLUMN columnName VARCHAR (256);
Error :
Incorrect syntax near 'VARCHAR'
The syntax for modifying a column definition is depending on used database. For Microsoft SQL Server for example, you would need to replace MODIFY COLUMN with ALTER COLUMN.
For better help you should specify your database type.

SQL - how to create a new table?

I am self-studying how to write SQL. I am following the documentation here.
SQL Error [1064] [42000]: You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near ')' at line 1
How can I fix this?
Can you add what SQL you have tried? Otherwise, this is how you create a new table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
)

SQL AS statement without column name shortcut

what does the AS in a statement such as below do?
SELECT column AS
FROM table
Since there is no shortcut, does it even do anything?
You did not tag your actual RDBMS, the following is valid for SQL-Server, but you'll be able to transfer this to your system:
--A dummy table as declared table variable
DECLARE #DummyTbl TABLE(SomeColumn INT);
INSERT INTO #DummyTbl VALUES(1),(2),(3);
--Variation of namings
SELECT SomeColumn --The orginal name
,SomeColumn AS OtherName --A classical alias
,SomeColumn test --The alias does NOT need the 'AS'
,SomeColumn [AS] --You may name your output 'AS'
FROM #DummyTbl;
But your syntax is - at least with SQL Server - not valid. AS is - in this context - a reserved word.

Syntax error while converting varchar to numeric in sql

I want to convert a varchar to numeric, I looked aroud and found SELECT CAST. I tried to use it like this this:
SELECT CAST(`Elo` AS INT) FROM Table `Rankings`;
However, I get a syntax error. Is there anything wrong?
Remove the Table keyword from your query.
SELECT CAST(`Elo` AS INT) FROM `Rankings`;

What's wrong with this Postresql ALTER TABLE?

I have a database where there is a column of varchar that I wish to convert to a timestamp. I'd like to do something like this, but keep getting a syntax error, please can someone advise?
ALTER TABLE MY_TABLE
ALTER COLUMN MY_COLUMN TYPE timestamp
USING to_timestamp(MY_COLUMN::double precision);
MY_COLUMN is of type VARCHAR(255) NOT NULL
Error reads:
Syntax error in SQL statement "ALTER TABLE MY_TABLE
ALTER COLUMN MY_COLUMN TYPE TIMESTAMP
USING[*] TO_TIMESTAMP(MY_COLUMN::DOUBLE PRECISION) "; SQL statement:
ALTER TABLE MY_TABLE
ALTER COLUMN MY_COLUMN TYPE timestamp
USING to_timestamp(MY_COLUMN::double precision) [42000-176] 42000/42000
It would appear that my Postgresql running within Grails doesn't support to_timestamp. As pointed out by #a_horse_with_no_name in the comments of the question, the code works. Thanks for pointing out sqlfiddle.com, I hadn't realised such a resource existed.