SQL Server 2008 varchar and char not working - sql

I get an error saying varchar or char is not a recognized in SQL Server. However I use the same software, SQL Server 2008 in college, so I do not want to go for a different version of SQL Server. Anyway I can fix this?
Major Error 0x80040E14, Minor Error 26302
create table abc
(
id int,
name varchar(15)
)
Error is:
The specified data type is not valid. [ Data type (if known) = varchar
]

varchar(n) is not supported in SQL Server Compact. Here is the full list of types supported.
Following are the types that you can use.
nvarchar(n)
nchar(n)
ntext
So you might need to change to nvarchar(10), nvarchar(5) and nchar(1) etc..

Really ? , i tried it, also i tried in creating temp table, it seems both are ok.
create table abc (id int, name varchar(15))
create table #tmp (id int, name varchar(15))
http://sqlfiddle.com/#!3/66b44

Related

How to DECLARE TYPE myType AS TABLE?

I am trying to use table-valued parameter to pass a column to "IN" clause in SQL Server. To do so I need to declare a type of table.
The SQL expression:
DECLARE TYPE myType AS TABLE( myid varchar(50) NULL);
is giving the error:
'myType' is not a recognized CURSOR option.
Replacing DECLARE to CREATE is working good.
Using "CREATE" is requiring to drop this type in any next SQL calls like:
IF TYPE_ID('myType') IS NOT NULL DROP TYPE myType;
But I would like to use the type "myType" just within this SQL expression only.
How to declare a type as table without creating it permanently and without deleting it in all requests?
DECLARE requires the following sinthax:
DECLARE #LastName NVARCHAR(30), #FirstName NVARCHAR(20), #Country NCHAR(2);
With DECLARE, you can:
To assign a variable name(using #).
To assign a Variable Type
To assign a NULL default Variable value
So "DECLARE TYPE" is not a valid statement.
Have a look to temporary tables in sql.
You can create a temporary table in sql with the following code:
CREATE TABLE #TempTable
(
name VARCHAR(50),
age int,
gender VARCHAR (50)
)
INSERT INTO #TempTable
SELECT name, age, gender
FROM student
WHERE gender = 'Male'
With a temporary table, you can store a subset of data from a standard table for a certain period of time.
Temporary tables are stored inside “tempdb” which is a system database.

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

DateTime Conversion Error - Excel to SQL

I have two data sets on two different SQL servers. I've got dataset 1 and put it into Excel and am then going to put this into a temp table so I can query it against the data on server 2. Here is the SQL code that I have created:
Create table #JWTemp1 (AutoID varchar(10), IDNumber varchar(20), AltIDNumber varchar(20), AdmitDTTM datetime, AdmitDay varchar(15), AdmitWeekNo int)
Insert into #JWTemp1 Values('ID001','BCC445567','ABC445567','42510.7326388889','Friday','21')
Each time I try and run the code, I get the following error:
Conversion failed when converting date and/or time from character string.
I know this is a common error but I've tried all manner of soutions and got nowhere. Any suggestions?
You have to format the string. Not sure what DB are you using but here is the syntax for mySql.
DATE_FORMAT(colName, '%Y-%m-%d') DATEONLY
DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY
Unfortunately, non of the answers provided seemed to work. However, I solved the issue using the following logic:
Create table #JWTemp1 (AutoID varchar(10), IDNumber varchar(20), AltIDNumber varchar(20), AdmitDTTM datetime, AdmitDay varchar(15), AdmitWeekNo int)
Insert into #JWTemp1 Values('ID001','BCC445567','ABC445567','42510.7326388889','Friday','21')
Select
convert(datetime, Convert(float,AdmitDTTM))

Need resolution for this below code issue

I have Ran below query
DECLARE #temp TABLE(Id int identity(1,1), name varchar(20))
INSERT INTO #temp (name)
select name from sys.tables
The above query is working fine with out any issues in one machine. But i ran the another machine it through some error. Its very wired for me. I have attached the screen shot.
Both machines are having same sql server SQL server 2008 R2 Express(Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) Apr 2 2010 15:53:02 Copyright (c) Microsoft Corporation Express Edition with Advanced Services on Windows NT 5.1 (Build 2600: Service Pack 3) ) Edition.
This means one machine has table names that are all less than 20 characters, but the machine it doesn't work on has table names that are longer than 20 characters.
Change the size of your name column to nvarchar(255); you can go all the way up to 4000 if you still have trouble.
DECLARE #temp TABLE(Id int identity(1,1), name nvarchar(255))
INSERT INTO #temp (name)
select name from sys.tables
EDIT: based on #Raj's response and my subsequent research, I have modified the 'varchar' column to be 'nvarchar' because that is what table names can hold. For quick queries, I personally don't care if I use 255 instead of the actual potential length of a column's name (128). But per #Raj and the T-SQL documentation, the max column name length is 128.
http://technet.microsoft.com/en-us/library/ms188348.aspx
The definition for thename column in sys.tables is
sysname(nvarchar(128))
In the first machine, you probably do not have any table names that are longer than 20 characters, so it works. However, in the second machine, looks like you have table names longer that 20 characters, hence the error. Try changing your #temp definition to
DECLARE #temp TABLE
(
ID int identity(1,1),
name nvarchar(128)
)
String or Binary data would be truncated error you get when you're trying to insert string which is more than defined length, in your case it's 20 and string trying to fit in that column is greater than 20.
Resolution: Make it as nvarchar and increase the length.
DECLARE #temp TABLE(Id int identity(1,1), name nvarchar(255))
INSERT INTO #temp (name)
select name from sys.tables
As Raj states the datatype for a column name in SQL Server is SYSNAME, therefore just use this as it is both backward and forward compatible:
DECLARE #temp TABLE(Id int identity(1,1), name sysname)
INSERT INTO #temp (name)
select name from sys.tables
See What is SYSNAME data type in SQL Server?

mysql syntax error for timestamp

I have this piece of SQL that is being fed to Mysql.
CREATE TABLE pn_history(
member INT,
action INT,
with INT,
timestamp DATETIME,
details VARCHAR(256)
)
But is comes back as an error about the syntax.
#1064 - 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 'with INT,
timestamp DATETIME,
details VARCHAR(256)
)' at line 4
Why is this failing?
Both 'with' and 'timestamp' are reserved words in MySQL. So to get this to work, you'd need to escape each one:
CREATE TABLE pn_history(
member INT,
action INT,
`with` INT,
`timestamp` DATETIME,
details VARCHAR(256)
)
Really though, you need to consider changing the names of your columns identifiers.
Read more about MySQL Reserved Words.
EDIT: Actually, TIMESTAMP is not a reserved word. The documentation says:
MySQL allows some keywords to be used
as unquoted identifiers because many
people previously used them. Examples
are those in the following list:
ACTION
BIT
DATE
ENUM
NO
TEXT
`TIME
TIMESTAMP
So I guess that means peer pressure took TIMESTAMP off the reserved word list. Hah!
The problem is the name of the with column. Change the name into something like withValue.
CREATE TABLE pn_history(
member INT,
action INT,
withValue INT,
timestamp DATETIME,
details VARCHAR(256)
)
timestamp is a keyword (it is a data type in mysql) which may be causing you problems.
I would suggest using a different name, but if it must be named timestamp, try using backticks to quote it.