Postgres DB Column Type Conversion - ruby-on-rails

Trying to convert a string column to date. Column currently holds values, such as '20170130', and '20161130'.
When I try:
change_column :tickets, :string_date, :date
This gives me an error:
PG::DatatypeMismatch: ERROR: column "string_date" cannot be cast automatically to type date.
HINT: Specify a USING expression to perform the conversion.
So, I try:
change_column :tickets, :string_date, 'date USING CAST(string_date AS date)'
Still no luck, with the following error:
PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date
Anyone know what's going on?? Thought converting from string to date would be easier...
thanks in advance

strange - I would expect to see the value, where you fail to cast date, eg:
t=# create table a2(string_date text);
CREATE TABLE
t=# insert into a2 values ('20170130'),('20161130');
INSERT 0 2
t=# insert into a2 select 'bad value';
INSERT 0 1
t=# alter table a2 alter COLUMN string_date type date using string_date::date;
ERROR: invalid input syntax for type date: "bad value"
maybe its an empty string?..
anyway - find bad value and fix it, then try to convert again:
t=# select string_date from a2 where string_date !~ '\d{8}';
string_date
-------------
bad value
(1 row)
t=# begin;
BEGIN
t=# delete from a2 where string_date !~ '\d{8}';
DELETE 1
t=# alter table a2 alter COLUMN string_date type date using string_date::date;
ALTER TABLE
t=# end;
COMMIT

Related

adds leading zero to a column value Etext template

How to format the column so that it adds leading zero to a column value.
for example , in the etext template i want to add this logic. using which function can i do this ?
that is if job_code is 2900 then it should come as 002900. I tried adding "0000000" under the format column, but it didnt work.
You could format the column in Data Model sql so it comes to the template already formated..
Select LPAD(COLUMN_NAME, 6, '0') as ALIAS...
Here is an example of LPAD function: ERROR: Function SUBSTR requires a character expression as argument 1. and adding zeroes in front of data

Query Selector, Where Clause comparing dates

I am trying to get the selective data from a Sheet.
I am getting Column U in comparision with Column P
Column U = Unique Data
Column P = Date and Time Associated with it.
Column U Column P
564865 2020-06-13 5:52:00
I would like to get the data of the day before current day while truncating time of Column P.
i am trying to use this query, but i am getting some error.
=QUERY('Shopify Unfulfilled'!1:2615,"Select U WHERE "&LEFT('Shopify Unfulfilled'!P:P,10)& "=" &TODAY()-1&"")
ERROR:
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " <ID> "Created "" at line 1, column 16. Was expecting one of: "(" ... "(" ...
Thank you.
See if this works
=QUERY('Shopify Unfulfilled'!1:2615,"Select U WHERE todate (P) = date '"&TEXT(today() -1, "yyyy-MM-dd")&"'", 1)

how to change datatype from integer to character in psql?

ı need to change the datatype of a column .
ALTER TABLE students
ALTER COLUMN "Dname" character varying (30) ;
I tried this but it doesn't work.
You should use this statement instead.
ALTER TABLE students
MODIFY Dname varchar2(30);
Best,
Yassine

Parse keyword="value"

I'm looking for a HIVE UDF that will parse an array of data into a tabular form.
If there's nothing in HIVE, a PIG example would be appreciated.
The input is in this format:
date timestamp key1="val1" key2="val2" key3="val3" year month day
date timestamp key1="val4" key2="val5" key3="val6" year month day
Would like the results to be a table where the column names are the keywords and the results are the values. Such as:
results:
column_name key1 key2 key3
results val1 val2 val3
val4 val5 val6
As of my understanding about your issue,i am pointing one solution.
first create a table in hive.
create table example1(dates string,timestamps string,key1 map<string,string>,key2 map<string,string>,key3 map<string,string>,year int, month string,day string) row format delimited fields terminated by ' ' map keys terminated by '=';
create another table like
create table example2(key1 string,key2 string,key3 string)
insert the data into second table from first table
insert into table example2 select key1["key1"],key2["key2"],key3["key3"] from example1;
output:
select * from example2;
"val1" "val2" "val3"
"val4" "val5" "val6"
in this, i am not concentrating on data types.

Formatting: Changing Column Headers for SqlPlus Query

I'm having trouble with two formatting issues that I would really appreciate help with:
1) The Days Open column is displaying the number of days properly, but the column name is being overwritten by my conversion command, and
2) I need the Order Date (OOpenDate) to be displayed in the "MM/DD/YYYY" format
Code:
column O_Num heading 'Order|Number' format a6
column OOpenDate heading 'Order|Date' format a10
column (sysdate-OrderOpenDate) heading 'Days|Open' format a4
select O_Num, OOpenDate, to_char(sysdate-OOpenDate, '999')
from Orders
where Status = 'Open';
What its currently displaying:
Order Order
Number Date TO_C
------ --------- ------
100 03-DEC-13 14
What I want it to display as:
Order Order Days
Number Date Open
------ --------- ------
100 12/03/2013 14
Thank you in advance!
The simplest approach would be to alias the "Days Open" column and apply the format mask to the alias
column days_open heading 'Days|Open' format a4;
select O_Num, OOpenDate, to_char(sysdate-OOpenDate, '999') days_open
from Orders
where Status = 'Open';
Otherwise, the column name in your column command would need to exactly match the expression in your SELECT statement (including the to_char, any spaces, etc.) That's possible but it generally makes things rather hard to maintain.

Resources