I've an external table in Hive
Current:
col1 - string
col2 - string
col3 - string
col4 - float
col5 - int
I want to change the date type of col3 to date
Expected:
col1 - string
col2 - string
col3 - date
col4 - float
col5 - int
I tried regular sql command but not useful
alter table table_name modify col3 date;
Error:
Error while compiling statement: FAILED: ParseException line 1:32 cannot recognize input near 'modify' 'col3' 'date' in alter table statement
Requesting assistance here. Thanks in advance.
Correct command is:
alter table table_name change col3 col3 date;
The column change command will only modify Hive's metadata, and will
not modify data. Users should make sure the actual data layout of the
table/partition conforms with the metadata definition.
See syntax and manual here: Change Column Name/Type/Position/Comment
Related
I am trying to change type of columns from varchar to integer. When I run the following command
alter table audio_session
alter column module_type_cd type INT USING module_type_cd::integer,
alter column sound_type_cd type INT USING sound_type_cd::integer
I got the error:
ERROR: invalid input syntax for integer: "string"
SQL state: 22P02
The error does not make any sense. There is no "string" in the command at all. What did I do wrong?
The columns audio_session.module_type_cd or audio_session.sound_type_cd has at least one non-numeric value among all values of each row. So, it's impossible to convert to a completely numeric data type.
As an example :
create table Table1( col1 varchar(10), col2 varchar(10) );
insert into Table1 values('1','1');
insert into Table1 values('2','2');
insert into Table1 values('3','C3');
select CAST(coalesce(col1, '0') AS integer) as converted from Table1; -- this works
select CAST(coalesce(col2, '0') AS integer) as converted from Table1; -- but, this doesn't
SQL Fiddle Demo
There is no "string" in the command at all.
But must be in the data. The command you're going to use tries to cast all the columns values to integer. Check this:
select *
from audio_session
where module_type_cd = 'string' or sound_type_cd = 'string';
I have a flat file that I loaded into SQL Server using the import wizard. All columns are stored as nchar(50).
I'm now trying to convert the table to a new table with various types (char, float, int, etc).
There is one column that I'd like to convert to float but it contains N/A strings.
I checked that there weren't any other weird string using this:
SELECT col1, count(*)
from tab1
where ISNUMERIC(col1) <> 1
group by col1
Then I wrote a CASE statement to do the conversion:
SELECT CASE WHEN col1 = 'N/A' THEN NULL ELSE CAST(col1 AS FLOAT) END col1
INTO tab2
FROM tab1
But I'm getting this error message:
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
Any idea what's causing this?
If 2012+ Use try_convert()
SELECT col1, count(*)
from tab1
where try_convert(float,col1) is null
group by col1
You can use try_convert if you want to ignore invalid chars to float as below
Select try_convert(float, col1) as col1 from yourtable
Careful on using ISNUMERIC() for this. It can return some false positives. For example...
select isnumeric('$')
select isnumeric('1e4')
This should show you what is causing the error.
select * from table where column like '%[^0-9\.]%' escape '\'
You can use this in a where clause, or use TRY_CONVERT()
You aren't doing the type conversion from the String to the float. In your SQL statements you need to explicitly cast the original column type from nvarchar to float. Something like this might work:
SELECT
CASE
WHEN col1 = 'N/A' THEN NULL
ELSE CAST(col1 AS FLOAT)
END col1
INTO tab2
FROM tab1
I'm trying to update column data,i.e if column data contains '2u' or '3u' at end and replace with 'ui'. How do i achive this using the access sql query
sample table
col1 col2
562u yu3u
You might need to provide a bit more information but if I get what you're after you could use a SQL command like this:
UPDATE <tablename>
SET col1 = MID(col1, 1, LEN(col1) - 2) & 'ui'
WHERE col1 LIKE '*2u' or col1 LIKE '*3u'
You'll need to run the query for the second column separately:
UPDATE <tablename>
SET col2 = MID(col2, 1, LEN(col2) - 2) & 'ui'
WHERE col2 LIKE '*2u' or col2 LIKE '*3u'
I am inserting data to tables using dynamic SQL:
SET #Profiles = N'SELECT ''' + #var1 + ''' as col1, col2, col3, col4, col5, col6, col7
FROM ' + #TableName + 'tbl1'
INSERT INTO
table (col1, col2, col3, col4, col5, col6, col7)
EXEC (#Profiles)
Above query is in stored procedure, which is run by job.
I noticed when job is running the data with Japanese characters are inserted properly but when job is completed and I make select on inserted table it returns '?' instead of Japanese characters. I am using collation SQL_Latin1_General_CP1_CI_AS and my column data type is nvarchar. I also tried to change the collation but even with Japanese collation it returns '?'. Do you know how I can handle this?
EDIT 1
I forgot to add that the stored procedure resides in SSIS package. Maybe it can help.
I found what was wrong. I have a function at last step of the SSIS package which removes non-printable white characters and this function parameter was varchar instead of nvarchar. I changed it to nvarchar and it is ok now.
Here's my simplified table (SQL Server 2005):
table1: col1 int, col2 int, col3 cast(col1/col2 as int) [computed column]
for some reason the above doesn't work. i just want to save a WHOLE part of col1/col2, how do i do that?
example: col1 = 5, col2 = 3 ... col3 should be 1
One option would be to use the floor function:
FLOOR(col1/col2)
or
CONVERT(int, FLOOR(col1/col2)) -- Might be overkill