Setting a varchar column length with a variable or a subquery - sql

I have a table that is created by an import of an Excel file. As is the case, all text fields are imported as nvarchar(255). I'd rather do the bulk import then manipulate the table afterwards. I know SSIS allows me to set data types and sizes via data mapping, but that doesn't seem to work consistently for me. Maybe I'm not holding my mouth right...
Anyway, I want to change the varchar length definitions to the max length of the data in each column. Rather than run a statement to check the max length as a literal...
select max(len(rtrim(FIELD))) from TABLE$
...I want to do it in code. So I hit on this idea:
declare #Var int
set #Var = (select max(len(rtrim(FIELD))) from TABLE$)
alter table dbo.TABLE$ alter column FIELD varchar(#Var)
Lines one and two work fine, but it gives me an error when executing the third line:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '#Var'.
So I tried this, thinking it would be a more compact solution...
alter table dbo.TABLE$
alter column FIELD varchar(select max(len(rtrim(FIELD))) from TABLE$)
...but it wasn't. I got these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
So my question is kind of a two-parter. First, why won't these methods work, and second, what would work--short of finding the mas length of each column, then setting the varchar length with a literal?
Thanks in advance.

Dynamic SQL would work for what you're trying to do. Whether it's the right tool for the job or the right thing to do is another story. Give this a shot:
DECLARE #cmd NVARCHAR(4000)
SET #cmd = 'ALTER TABLE dbo.Table$ ALTER COLUMN FIELD VARCHAR(' + CAST((SELECT MAX(LEN(RTRIM(FIELD))) FROM dbo.TABLE$) AS NVARCHAR(10)) + ')'
EXEC(#cmd)
That query parses but I didn't try to run it. Be careful using Dynamic SQL though. It can get you into trouble if you start using it everywhere. Further reading:
http://sqlmag.com/database-performance-tuning/don-t-fear-dynamic-sql
http://www.sommarskog.se/dynamic_sql.html

Related

Msg 8114, Level 16, State 5, Line 31 Error converting data type varchar to bigint

I'm trying to convert a varchar column to bigint and keep getting an error.
I've tried to alter the column using every exact or approximate data types and it will not convert. I've trimmed and cleaned the data and it still has issues.
This is the code that I have been using.
USE [database1]
GO
ALTER TABLE [dbo].[table1]
ALTER COLUMN [column1] bigint
GO
These are the results of the code above
Msg 8114, Level 16, State 5, Line 31
Error converting data type varchar to bigint.
The statement has been terminated.
Any help is appreciated, thank you.
You would appear to have invalid values. So, update them away:
update t
column1 = convert(varchar(255), try_convert(bigint, column1));
Then try changing the column type.
Or, just add a computed column:
alter table t add column1_bi as (try_convert(bigint, column1));
You can see what the bad values are using:
select column1
from t
where try_convert(bigint, column1) is null and column1 is not null;

Adding columns and data to table

I am trying to run the following queries. When I run them separately the code executes correctly. When I run them together I get the error message:
Msg 207, Level 16, State 1, Line 11
Invalid column name 'TotOP'.
Msg 207, Level 16, State 1, Line 12
Invalid column name 'TotPK'.
It's as if the first query has not run at all? - Any ideas
As a work around I know that I can generate all of the columns at the beginning and only partially populate the table leaving these final two columns empty until I run the second query however I am curious to know why these statements can be run separately but not together.
Query 1:
ALTER TABLE [fcgen].[RPD1yeardata]
ADD [TotOP] DECIMAL (7,3) NULL,
[TotPK] DECIMAL (7,3) NULL;
Query 2:
UPDATE [fcgen].[RPD1yeardata]
SET [TotOP] = [1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[39]+[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48],
[TotPK] = [15]+[16]+[17]+[18]+[19]+[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]
FROM [fcgen].[RPD1yeardata]
just use GO after 1s query
ALTER TABLE [fcgen].[RPD1yeardata]
ADD [TotOP] DECIMAL (7,3) NULL,
[TotPK] DECIMAL (7,3) NULL;
Go
--Query 2:
UPDATE [fcgen].[RPD1yeardata]
SET [TotOP] = [1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[39]+[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48],
[TotPK] = [15]+[16]+[17]+[18]+[19]+[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]
FROM [fcgen].[RPD1yeardata];

How to rename a MSSQL Database that has name "Database"?

I have a MSSQL Database named "Database". Now when I am trying to rename it using query shown below,
USE master;
GO
ALTER DATABASE Database
Modify Name = Database01
GO
It gives me this error message:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Database'.
But this query works fine for other database. What I am doing wrong?
If you "quote" the table name it should work. The default quote characters are square brackets [], so:
USE master;
GO
ALTER DATABASE [Database]
Modify Name = Database01
GO
Instead of using the the Long code you Can just use the System built-in Stored Procedure -sp_renamedb'oldDBName','NewDBName'
when you have to use reserved keywords for table name,database name,column name always put [] (big brackets )
such as
select * from [Table] in place of select * from table
select [column] from [table] in place of select column from table
but its very bad idea to put reserved keyword as a name for your objects.

Create table, and import data from csv or txt file

I have a CSV file that contains stock quotes. I am new when it comes to SQL, but I have done a lot of research and come up with a code that I thought should work. But it doesn't. I get errors all the way....
USE ShakeOut
GO
CREATE TABLE CSVTest1
(Ticker varchar(10),
dateval smalldatetime),
timevale time(),
Openval varchar(10),
Highval varchar(10),
Lowval varchar(10),
Closeval varchar(10),
Volume varchar(10),
)
GO
BULK
INSERT CSVTest1
FROM 'c:\TEST.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest1
GO
--Drop the table to clean up database.
DROP TABLE CSVTest1
GO
My CSV file has timevalue as 03:15:00 PM, and I'm not sure how to set that up in the table. The other values I think are aproxmately right, here's a sample of my csv file:
5/1/2009,9:30:00 AM,18.21,18.45,18.21,18.32,32163
5/1/2009,9:35:00 AM,18.33,18.34,18.27,18.29,36951
5/1/2009,9:40:00 AM,18.29,18.38,18.25,18.37,53198
5/1/2009,9:45:00 AM,18.38,18.4,18.28,18.285,49491
And here is my error messages in SQL Management Studio:
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near ','. Msg 208,
Level 16, State 82, Line 3 Invalid object name 'CSVTest1'. Msg 208,
Level 16, State 1, Line 3 Invalid object name 'CSVTest1'. Msg 3701,
Level 11, State 5, Line 3 Cannot drop the table 'CSVTest1', because it
does not exist or you do not have permission.
I would really appreciate help here, my head is about to explode after all these hours without any progression. I've tried MySQL too, didn't work there either.
As I'm new, I might need it explained to the details.
It appears you have an extraneous comma in the CREATE TABLE statement. There is a comma following the final column prior to the closing paren. Perhaps it is valid in some implementations, but you might try removing it. Change it to:
Volume varchar(10)
Ah - and it appears there is an extraneous closing parent in the second column definition. Change it to:
dateval smalldatetime,
And the time column:
timevale time,
Ultimately, it appears you should probably just try to get the CREATE TABLE statement syntax correct, then start adding the other parts.
There is no need for a comma after the last column definition: Volume varchar(10),.
I assume timevale should be timeval.
time() should just be time.
Also, I'm probably being picky but you have capitalised the first letter of all the column names except the first two - won't cause an error but probably better to have a consistent naming convention. I would capitalise the 'v' in val and write the whole word too.
The CSV data needs revising too - you need to specify EVERY column, even if it is null. See my example data (the new lines at the end of each row are for illustration purposes only).
1234567890,2012-08-25,22:15,anytext,ornum,for,varchar,columns <-new line
abcd123456,2010-05-20,00:01,anything,in,these,varchar,columns <-new line
abcd123456,2010-05-20,00:01,anything,in,,,columns <-new line
This works:
CREATE TABLE CSVTest1 (
Ticker varchar(10) NULL,
DateValue smalldatetime NULL,
TimeValue time NULL,
OpenValue varchar(10) NULL,
HighValue varchar(10) NULL,
LowValue varchar(10) NULL,
CloseValue varchar(10) NULL,
Volume varchar(10) NULL)
GO
BULK INSERT CSVTest1
FROM 'C:\TEST.txt'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
GO
Your CSV file needs to have a new line for each record you want to insert, as specified by the ROWTERMINATOR = '\n' and a comma between each field as specified by FIELDTERMINATOR = ','.
EDIT:
By the way if you are using SQL Server Management Studio (SSMS) you can create the table through the user interface and then:
Right click on the table
Script Table as
CREATE To
New Query Editor Window

Error when using openrowset for particular record

INSERT INTO OPENROWSET('MSDASQL', 'Driver=PostgreSQL Unicode;uid=postgres;Server=localhost;port=5432;database=data;pwd=xxx',
'select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from tesing WHERE SanctionId = ''-1'' ')
select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from testing where SanctionId=1103
While executing the above query, I am getting following error:
Msg 8152, Level 16, State 10, Line 1
String or binary data would be truncated.
The statement has been terminated.
Can anyone help me to resolve this?
You will have to check the source data against the target column definitions.
This happens when you try to insert, say, 100 characters into a varchar(50) column