How can I filter my data down to a given Year or Month in a PSQL v13 Database - psql

I get a syntax error in PSQL Control Center on an Actian 13 database when using Date_Part and I'm out of ideas on how to proceed.
There are two fields in the table I am querying 'Date' and 'CreateDate'. I get the same error when I involve either field. I have tried both 'month' and 'year' and have confirmed the field in the database is a Date field.
I have tried the following with no luck.
select date_part('month',CreateDate) from gl_trx3 where CreateDate = '2021-10-13'
I am using this specific date for example because there are records with that date saved.
The error message I get is below.
[LNA][PSQL][SQL Engine]Error in expression: date_part('month',CreateDate)
Anyone know what I am missing?

In the list of Time and Date Functions for Pervasive SQL13 the function date_part does not exist.
Using MONTH(dateexp) returns the month as an integer in the range of 1 to 12.

Related

Changing timezone for a default timestamp column in Snowflake

I have a table defined in snowflake as follows;
CREATE OR REPLACE TABLE DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES(
PREDICTED_PROBABILITY FLOAT,
TIME_PREDICTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
As expected default timezone is America/Los Angeles. I am trying to change it to UTC, but only for this column, not at account/session level.Here is the code I wrote;
ALTER TABLE DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES ALTER TIME_PREDICTED SET timezone= 'UTC';
But it is giving an error,
SQL compilation error: syntax error line 1 at position 86 unexpected 'timezone'.
Can I please get help on how to do the change at column level?thanks in advance.
I recommend reviewing this documentation regarding timestamp data types:
https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#timestamp
However, assuming that your TIMESTAMP_TYPE_MAPPING is still the default of TIMESTAMP_NTZ, then you've now got a bunch of data that is set to America/Los Angeles without a timezone offset in the values, and if you're not going to change any of your account timezone settings, then you either need to leave it that way and just change the timezone as you select the data using the CONVERT_TIMEZONE function, or you should change your table definition to a data type that includes a timezone offset. You could also update the column as it is by converting it with the same CONVERT_TIMEZONE function, but then future data would still be inserted using America/Los Angeles timezone.
My recommendation is to use TIMESTAMP_TZ as your column type and modify the current data accordingly.

How to insert data from text file where date column has date as "00-00-0000"

I have a text file and it contains one column as "NPA_DATE", in which few rows contain date as "00-00-000".
I am unable to load data where date is in "00-00-0000" format. I am getting the error as "Record 1: Rejected - Error on table MIS_PNPA, column NPA_DATE. ORA-01847: day of month must be between 1 and last day of month". However I want to load the data with NPA_DATE as "00-00-0000" along with other data. All other rows are getting uploaded in database. I use toad for oracle 9.0.1.8
You should add the SQLPlus tag to your original post as this is really a SQLPlus question.
Anyway alter your control file to this to set the date to NULL if it comes in as '00-00-0000' in the data file:
...
npa_date date "DD-MM-YYYY" NULLIF (npa_date="00-00-0000")
...

issue to query data filtered by Date from fusion table

I'm trying to write a small javascript code to query data filtered by date.
If I write the following sentence in my browser, I can get data :
"http://www.google.com/fusiontables/gvizdata?tq=SELECT Date, Poids FROM 3049883"
but if I write the same thing, except I want only data after a certain date :
"http://www.google.com/fusiontables/gvizdata?tq=SELECT Date, Poids FROM 3049883 WHERE Date > 2/29/12"
From the SQL-like API, https://developers.google.com/fusiontables/docs/developers_reference#Select, it should work
I get an error witch is "'internal_error', message:'Could not parse query'"
-Date is a DATETIME format in my fusion table.
-I've tried different format, but I can not get data.
What am I doing wrong?
Thank you very much for your help.
The Date value must be quoted and the format is MM/dd/yy so you must pad single digits with leading zeros.
I had success with:
select Date,Poids from 3049883 where Date >= '02/29/12'
Note: I did not test with gvizdata, just with the FT JSONP API

ruby/rails - Converting Entire Database Field (from string to date)

I have made a huge mistake.
Initially I created my model with a field called start_date and made it a string to keep track of event dates.
Now I'm realizing it would be nice to have this field as a date type so I could do calculations like find events where start_date is between today and 1 month from now.
This issue is I already have 500 records so starting over would suck....
The format of the start_date field is in a rails compatible type " 2011-02-21 22:00:00 " but its just a string...
Is there anything I can do?
Create a migration to add a start_date_2 column of the type you want
Model.find(:all).each { |i| i.update_attributes(:start_date_2, Date.new(i.start_date)) }
Create a migration to delete start_date and to rename start_date_2 to start_date
This should work, out of the top of my head.
You could try just doing an EXPORT on the table (making sure to only export data, do not include CREATE and/or DROP table commands).
Create a migration to change the datatype
TRUNCATE the table
IMPORT the data
Since the column is now a date field, it should parse the input of a string just fine, considering that's what you provide it anyway
Perhaps, you can do away with the risk of changing column type if there is live data. The parse methods can save you. From Ruby-doc:
parse(str='-4712-01-01', comp=true, sg=ITALY)
Create a new Date object by parsing from a String, without specifying the format.
str is a String holding a date representation. comp specifies whether to interpret 2-digit years as 19XX (>= 69) or 20XX (< 69); the default is not to. The method will attempt to parse a date from the String using various heuristics; see _parse in date/format.rb for more details. If parsing fails, an ArgumentError will be raised.
Here and here are some more examples / explanations. Hope this helps.

I'm getting "Invalid month in date" trying to run this?

I'm trying to run the following db command against Informix:
delete from table1
where u_id in (select u_id
from table2
where c_id in (select c_id
from ptable
where name = 'Smith'
and dob = '29-08-1946'));
I pass this in as a string to the db.ExecuteNonQuery method in the MS Data Application block and I get the above error?
To get the date format '29-08-1946' to work, you need your DBDATE environment variable set to a value such as "DMY4-" (or "DMY4/"). These are standard variations for the UK (I used them for years; I now use "Y4MD-" exclusively, which matches both ISO 8601:2004 (Date formats) and ISO 9075 (SQL), except when debugging someone else's environment). There are other environment variables that can affect date formatting - quite a lot of them, in fact - but DBDATE takes priority over the others, so it is the big sledgehammer that fixes the problem.
One of the problems is that your notation using a plain string is not portable between US and UK (and ISO) settings of DBDATE. If you have a choice, the neutral constructor for dates is the MDY() function:
WHERE dob = MDY(8,29,1946)
This works regardless of the setting of DBDATE. You can probably use TO_DATE() too:
SELECT TO_DATE('29-08-1946', '%d-%m-%Y') FROM dual;
This generated '1946-08-29 00:00:00.00000' for me - the function generates a DATETIME YEAR TO FRACTION(5) value, but those convert reliably to DATE values in Informix.
You can also use the DATE() function or an explicit cast to DATE (either CAST('29-08-1946' AS DATE) or '29-08-1946'::DATE), but both of those are subject to the whims of the locale of the users.
Your date field is improperly formatted. Since there is no 29th month in the year 1946 that is what is causing the error.
I'd try just swapping the month and day. 08-29-1946.
The way the day and month parts of a date string are read in can depend on your computer's culture settings.
It is always safer to pass date strings to a database in the form 'dd-MMM-yyyy' (i.e. '29-aug-1946')
It's even safer to pass them as YYYY-MM-DD, the dd-MMM-yyyy in that example will fail on a server with a (for example) French locale.

Resources