Incorrect syntax around datetime2 - sql

The following CREATE statement is meant for SQL Server:
CREATE TABLE tclientlink
(
link_id INT,
ext_client_id VARCHAR(255),
goald_address_id VARCHAR(255),
goald_client_id VARCHAR(255),
instance_id VARCHAR(255),
source_id VARCHAR(255),
timestamp DATETIME2
);
The INSERT statement
INSERT INTO TCLIENTLINK(link_id, ext_client_id, goald_address_id, goald_client_id, instance_id, source_id, timestamp)
VALUES (13582, "0000059811", "3037260", "0000059811", "1", "1", 2018-08-22 15:13:34);
But when I try to validate this using an online tool, I get the following error message:
You have an error in your SQL syntax; it seems the error is around: 'datetime2 )'
What change do I need to make to my DDL above?

double quote indicate column name, as a result if you use double quote sql server engine will search that column name , so in case of value you have to use single quote and also for datetime column value should be quoted. so working query is below
CREATE TABLE tclientlink
(
link_id INT,
ext_client_id VARCHAR(255),
goald_address_id VARCHAR(255),
goald_client_id VARCHAR(255),
instance_id VARCHAR(255),
source_id VARCHAR(255),
timestamp DATETIME2
);
INSERT INTO TCLIENTLINK(link_id, ext_client_id, goald_address_id, goald_client_id, instance_id, source_id, timestamp)
VALUES (13582, '0000059811', '3037260', '0000059811', '1', '1', '2018-08-22 15:13:34');

Your insert statement might be this.
use ' instead of " for string value, and the datetime2 need to use ' contain it.
INSERT INTO TCLIENTLINK(link_id, ext_client_id, goald_address_id, goald_client_id, instance_id, source_id, timestamp) VALUES(13582,'0000059811','3037260','0000059811','1','1', '2018-08-22 15:13:34');
sqlfiddle

Related

Presto Hive SQL Error: mismatched input 'PARTITIONED'. Expecting: 'COMMENT', 'WITH', <EOF>

I am trying to create a Hive table with partitions but getting the above error. What am I doing wrong?
CREATE TABLE IF NOT EXISTS schema.table_name
(
ID varchar(20),
name varchar(50)
)
PARTITIONED BY (part_dt varchar(8), system varchar(5));
The code works without the partitioning clause. Something gives up during partitioning.
Statement is working in hive. Pls find below screenshot.
Its possible that some of column names are reserved keywords and that is throwing error. if yes, you can use below SQL too.
CREATE TABLE IF NOT EXISTS schema.table_name
(
`ID` varchar(20),
`name` varchar(50)
)
PARTITIONED BY (`part_dt` varchar(8), `system` varchar(5));

using spaces in columns names in sql to create table

I am trying to create table by sqlyog
i want to use spaces in columns names but i still got errors
for example id number
i just can do it like this id_number
i searched in this website i found two ways
[id number] or "id number"
i tried it but i still have errors
this is the code
CREATE TABLE project(
ProjectID VARCHAR(10),
Project NAME VARCHAR(50),
Group_Name VARCHAR(20),
BeginDate VARCHAR(10),
EndDate VARCHAR(10)
);
and these are the errors that i got them
1 queries executed, 0 success, 1 errors, 0 warnings
Query: CREATE table project( ProjectID varchar(10), Project Name varchar(50), Group_Name varchar(20), BeginDate varchar(10), EndDate va...
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Name varchar(50),
Group_Name varchar(20),
BeginDate varchar(10),
EndDate varc' at line 3
i hope some one helping me
The SQL Standard defines double quote character " to delimit identifiers.
Speaking of MariaDB and MySQL this requires that the sql_mode was set to ANSI:
mysql> set sql_mode=ANSI;
Query OK, 0 rows affected (0.00 sec)
mysql> create table project("project name" varchar(50));
Query OK, 0 rows affected (0.02 sec)
Another option (as mentioned in the previous answer) is to use backticks. However this solution will not be portable.
You can create tables and columns with spaces in the name using backticks (`)
Egg:
CREATE TABLE `project project`
(
`ProjectID` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Project NAME` VARCHAR(255) NOT NULL
.
.
.
);
try like below
CREATE TABLE project(
ProjectID VARCHAR(10),
`Project NAME` VARCHAR(50),
Group_Name VARCHAR(20),
BeginDate VARCHAR(10),
EndDate VARCHAR(10)
);

SQL: how to specify a date format on creating a table and fill it

I want to save the date in format 'dd.mm.yyyy'. So I read there are different formats for a date in SQL (by the way I use Visual Studio and SQL Server).
I tried this code:
CREATE TABLE APP(
ID INT NOT NULL,
DT DATE FORMAT 'dd.mm.yyyy',
ADDRESS NVARCHAR (100) ,
PRIMARY KEY (ID)
);
But it returns the error:
Incorrect syntax near 'FORMAT'.
After that I want to use this code:
INSERT INTO APP (ID, DT)
VALUES ('1','22.12.2016')
You don't need to specify the format in the table definition as dates are stored in a binary format.
CREATE TABLE APP(
ID INT NOT NULL,
DT DATE,
ADDRESS NVARCHAR (100) ,
PRIMARY KEY (ID)
);
When you try to insert into that table however, the server will try to convert the string to a date before inserting it. This can be problematic as it is unable to tell if 12.11.2017 is the 12th of November or 11th of December. To figure this out it uses the localization settings of the user account that is performing the operation.
Often you will find that the account that is running the operation is set to USA format, month day then year (MDY), when what you want is day month year (DMY) format. One way to tell it what the sequence of the date's parts is to use the DATEFORMAT setting like this:
SET DATEFORMAT dmy;
INSERT INTO APP (ID, DT)
VALUES (1,'22.12.2016')
Another alternative is to cast the string to a date using the CONVERT function and tell it what the date format is. The formats have numeric codes like 104 for German format Like this:
INSERT INTO APP (ID, DT)
VALUES (2,CONVERT(date,'22.12.2016',104))
Dates are stored in an internal format. Formats only make sense for input and output.
In your case you want the date in a German format (104), so you can use:
select convert(varchar(255), dt, 104)
If you like, you can include the formatted date as a separate column:
CREATE TABLE APP (
ID INT NOT NULL,
DT DATE,
ADDRESS NVARCHAR(100),
DT_FORMATTED AS (convert(varchar(255), dt, 104)),
PRIMARY KEY (ID)
);
You can then refer to dt_formatted to get the string in the format you want.
Use this:
CREATE TABLE APP(
ID INT NOT NULL,
DT DATE ,
ADDRESS NVARCHAR (100) ,
PRIMARY KEY (ID)
);
Its default setting is yyyy-MM-dd.
No, it's not. There is no formatting information at all associated with the field.
The value is not formatted by the database. It's returned only as a point in time. Formatting that value into its textual representation is done by the application that is getting the data from the database.
So, there is nothing that you can do in the database to change how the date value is formatted. You have to change that where the data is displayed.
Dates are stored in an internal format.
Formats only make sense for input and output.
You can include the formatted date as a separate column:
SQL Server supports the date format. You have to use the below date format.
With century (yyyy) | Standard | Input/Output
103 | British/French | 103 = dd/mm/yyyy
CREATE TABLE [dbo].[Post]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR(MAX) NULL,
[RowNo] INT NULL,
[ColNo] INT NULL,
[Deadline] (CONVERT(VARCHAR(255), dt, 103)), -- Include the formatted date as a separate column
CONSTRAINT [PK_KtoCo]
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Use this.
CREATE TABLE:
CREATE TABLE EMP
(EID NUMBER(20),
ENAME VARCHAR2(20),
DT DATE,
SAL NUMBER(20));
INSERT INTO THE TABLE:
INSERT INTO EMP (EID,ENAME,DT,SAL) VALUES(01,'ABCDE','11.NOV.2011',10000);
O/P OF ABOVE TABLE:
SELECT * FROM EMP;
EID ENAME DT SAL
01 ABCDE 11-DEC-11 10000

SQL: Auto insert current date as string/varchar

I would like to set up a schema which will store the current date as a string fitting in a varchar column of size 10. However CURDATE() returns as a date type, is there any way I can convert this when creating a table to automatically convert this to a string?
For reference I am using MonetDB and declaring the column like below, can I cast CURDATE somehow when creating a table?
tdate varchar(10) default CURDATE() ,
This works fine
create table dateexample
(
id int identity(1,1),
empname varchar(100),
dateinserted varchar(10) DEFAULT (CONVERT(VARCHAR(10),GETDATE(),101))
)
insert into dateexample (empname)
values
('johny')
select * from dateexample
-- 1 johny 04/24/2014

What is wrong with this SQL statement

The error is:
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 'RA---SIN', 'DEC--SIN', 0.0,-90.0)' at line 1
INSERT INTO files_table (filename, folder, survey, telescope, author, observer, equinox, ctype1, ctype2, crval1, crval2) VALUES('H001_abcde_luther_chop.fits', 'C:\dev\data\FITS\surveys\', '', '','', -1.0, 'RA---SIN', 'DEC--SIN', 0.0,-90.0)
The statement that created the table was (the line breaks are just for ease of reading)
create table files_table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
filename varchar(255), folder varchar(255), survey varchar(255), telescope varchar(255),
author varchar(255), observer varchar(255), equinox double, ctype1 varchar(255), ctype2
varchar(255), crval1 double, crval2 double);
Is it because I use ' instead of " - this hasn't troubled me before
Is it due to the -- in RA---SIN and DEC--SIN
It is because of the backslash before the single quote. Escape your backslashes (like so \\) and it should be ok.
I think it's because you are missing a value
You have 11 columns named and only 10 values
This is because you're trying to insert RA--SIN into equinox column, which is of type double.
I believe you're missing an '', so the query would work like this:
INSERT INTO files_table (filename, folder, survey, telescope, author, observer, equinox, ctype1, ctype2, crval1, crval2) VALUES('H001_abcde_luther_chop.fits', 'C:\dev\data\FITS\surveys\', '', '','','' -1.0, 'RA---SIN', 'DEC--SIN', 0.0,-90.0)