Convert integer to string in Presto - sql

I am trying to convert an integer (e.g. 20211008) to a string format (2021-10-08) to use as a primary key in a separate sub-query.
I have tried the following:
primary_key = cast(PARSE_DATETIME(cast(integer as VARCHAR),'yyyyMMdd') as date)
but receive an error: =' cannot be applied to varchar, date"

Related

postgresql changing column type from array to integer throwing casting error

I am changing the postgresql column data type from integer[] to integer, while executing below query,
alter table contact_type alter column is_delete set data type integer USING is_delete::integer
i am getting below error
ERROR: cannot cast type integer[] to integer
LINE 1: ...umn is_delete set data type integer USING is_delete::integer
^
SQL state: 42846
Character: 86
but when tried to change datatype from varchar[] to char, below query works fine
alter table contact_type alter column ct_type set data type varchar
i have referred this link so link but it is not working for converting array to normal data type..
Edit :- it is empty table without any data...
You need to pick the array element that you want to use. You can't convert e.g. 42 integers to a single one.
E.g. if you want to use the first element of the array:
alter table contact_type
alter column is_delete
set data type integer USING is_delete[1];
But a column named is_delete should probably be a boolean rather than an integer.

Dynamic Table and Column Names In hive platform

CREATE TABLE concat(cast(weekday(getdate()) as STRING),'_',cast(datepart(getdate(),'dd') as STRING ))(
id STRING,
date_time DATETIME,
cnt int
)
basically I want to create table and assign its name using query "concat(cast(weekday(getdate()) as STRING),'_',cast(datepart(getdate(),'dd') as STRING ))", but unable to do this it throws error.

U-SQL Column Type Convertion

I have created a U-SQL query, which gets the input file from the DataLake Store and converts the values. The final output is stored in DataLake Store.
DECLARE #in string = "system/dbotable{*}.tsv";
DECLARE #out string ="system/temp.tsv";
#searchlog =
EXTRACT
Id int,
Address string,
number int
FROM #in
USING Extractors.Tsv();
#transactions =
SELECT
*,
ROW_NUMBER()
OVER(PARTITION BY Id ORDER BY Id DESC) AS RowNumber
FROM #searchlog;
#result =
SELECT
Id ,
Address,
number
FROM #transactions
WHERE RowNumber == 1;
OUTPUT #result
TO #out
USING Outputters.Tsv();
And it is showing the following error,
Execution failed with error '1_SV1_Extract Error : '{"diagnosticCode":195887132,"severity":"Error","component":"RUNTIME","source":"User","errorId":"E_RUNTIME_USER_EXTRACT_COLUMN_CONVERSION_INVALID_ERROR","message":"Invalid character when attempting to convert column data.","description":"HEX: \"2243616E696E6522\" Invalid character when converting input record.\nPosition: line 1, column index: 1, column name: \"Id\".","resolution":"Check the input for errors or use \"silent\" switch to ignore over(under)-sized rows in the input.\nConsider that ignoring \"invalid\" rows may influence job results and that types have to be nullable for conversion errors to be ignored.","helpLink":""
It seems like the Id column is not always of type Integer.
I would extract the Id column as string first and then in a second step, try to convert it to Int, using a user defined function as shown in here: https://msdn.microsoft.com/en-us/library/azure/mt621309.aspx (example based on DateTime).
The other option, would be to use silent:true in your extractor, so you automatically ignore rows which fail the conversion.

Conversion failed when converting the nvarchar value '%' to data type int

I have an Object Data Source which pulls information from the database where if we don'd have a value for this particular parameter then we want to get all records. So I have a LIKE statement and I'm using a wildcard to return all entries, but this is giving me the error message
System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the nvarchar value '%' to data type int.
The table structure for the relevant column is:
[columnName] VARCHAR(50)
The SQL is something similar to:
SELECT [columns] from [table] where [column] LIKE #param
Then in VB I add the parameter:
Dim sessionParam As New Parameter
sessionParam.Name = "param"
sessionParam.DefaultValue = "%"
sessionParam.Type = TypeCode.String
SqlDataSource1.SelectParameters.Add(sessionParam)
So far I've tried casting the parameter value, casting the column, using dbType for the parameter instead of type, but nothing seems to work and I just get the same error. I suspect the column is reading as an int as this column is a mix of values so some are numbers, some are text, therefore SQL is making the 'educated guess' that that value needs to be an int
Conversion failed when converting the nvarchar value '%' to data type int
Is pretty clear that you have a datatype issue. And the wildcard of '%' is certainly not a integer. You are probably having the issue because the column in where [column] LIKE #param is probably an integer. So even though you identify the parameter as string it is still trying to do an integer to string comparison.
So you are comparing like WHERE Integer LIKE String and that throws a datatype conversion error because SQL will automatically try to convert your string to an integer.
To solve if you want to search for a number as a string which doesn't seem like a good idea you would do something like:
WHERE CAST([column] AS VARCHAR(10)) LIKE #param.
Instead of using LIKE if the parameter is NULL, try this instead.
SELECT [columns] from [table] where #param is null or [column] = #param
If you pass in a NULL parameter everything is returned. If it isn't null, then only where the column matches the parameter will be returned.

Change column datatype from Text to Integer in PostgreSQL [duplicate]

This question already has answers here:
Rails Migrations: tried to change the type of column from string to integer
(6 answers)
Closed 8 years ago.
I am using the following query to change the data type of a column from text to integer but getting error:
alter table a.attend alter column terminal TYPE INTEGER ;
ERROR: column "terminal" cannot be cast automatically to type integer
create table test(id varchar );
insert into test values('1');
insert into test values('11');
insert into test values('12');
select * from test
--Result--
id
character varying
--------------------------
1
11
12
You can see from the above table that I have used the data type – character varying for id
column. But it was a mistake because I am always giving integers as id. So using varchar here is a bad practice. So let’s try to change the column type to integer.
ALTER TABLE test ALTER COLUMN id TYPE integer;
But it returns:
ERROR: column “id” cannot be cast automatically to type integer SQL
state: 42804 Hint: Specify a USING expression to perform the
conversion
That means we can’t simply change the data type because data is already there in the column. Since the data is of type character varying Postgres can't expect it as integer though we entered integers only. So now, as Postgres suggested we can use the USING expression to cast our data into integers.
ALTER TABLE test ALTER COLUMN id TYPE integer USING (id::integer);
It Works.
So you should use
alter table a.attend alter column terminal TYPE INTEGER USING (terminal::integer) ;