Postgres Format When Creating a Table - sql

My work is testing out Postgres. I usually write in SQL using SAS with Oracle and Teradata syntax. Our test database has a really sloppy table in which every column was created as a character 255. I have a very simple thing I'm trying to do but it's not working. I want to create a new table and reformat from 255 to 10. I also want to remove all the trailing blanks. Also, "IS NULL" is not working. Even when there is nothing visible for a value.
PROC SQL;
CONNECT TO POSTGRES(&connectstuff);
EXECUTE(CREATE TABLE common.UNQ_NUM_LIST AS
SELECT DISTINCT UNIQUE_NUM,
BTRIM(PAT_ACCT) AS PAT_ACCT
FROM ACCT_DATA.ACCNTS
) by postgres;
DISCONNECT FROM POSTGRES;
QUIT;
I want to create PAT_ACCT as a character 10 format but not sure how. Can I indicate a new format when creating a table? Everything I've tried didn't work. Even the BTRIM doesn't actually seem to get rid of the trailing spaces on that value either. And again, null values aren't being recognized with "IS NULL". I feel like this should be very simple!

To influence the data type, cast the result column in the query appropriately:
CAST (btrim(pat_acct) AS character(10)) AS pat_acct
There is, however, no way to set a column NOT NULL that way.
I recommend that you execute two statements: one that creates the table the way you want it, and another one like
INSERT INTO ...
SELECT ... FROM ...

Related

Column cannot be found or is not specified for query - Progress SQL Interface

I get the following error, I haven't had this issue before and was wondering if lvscat is an alias for something. From what I read that is a common issue, but even if it is that I am still unsure of how to fix it. This is the full Error
[DataDirect][OpenEdge JDBC Driver][OpenEdge] Column "LVSCAT" cannot be found or is not specified for query.
Here is the query:
INSERT INTO PUB.lvsbk (BookingNo, LvsCat)
VALUES (1007265, 'G')
Mapping with SQL Interface:
It is possible that the column was defined as "lvsCat". That means that the case is important.
Unfortunately, you have to use the double quotes to reference it:
INSERT INTO PUB.lvsbk (BookingNo, "LvsCat")
VALUES (1007265, 'G') ;
If this is the case, you might want to recreate the table without escaping the name of identifiers.
I am unsure what tool you are using to demonstrate the definition of your table, but you can view the actual definition of your table with:
select * from sysprogress.syscolumns where tbl = 'lvsbk';
Alternatively a simple:
select top 1 * from pub.lvsbk;
May provide enough evidence as to what your column is actually called.
I found the issue, it was because the field simply didn't exist.

INSERT Statement in SQL Server Strips Characters, but using nchar(xxx) works - why?

I have to store some strange characters in my SQL Server DB which are used by an Epson Receipt Printer code page.
Using an INSERT statement, all are stored correctly except one - [SCI] (nchar(154)). I realise that this is a control character that isn't representable in a string, but the character is replaced by a '?' in the stored DB string, suggesting that it is being parsed (unsuccessfully) somewhere.
The collation of the database is LATIN1_GENERAL_CI_AS so it should be able to cope with it.
So, for example, if I run this INSERT:
INSERT INTO Table(col1) VALUES ('abc[SCI]123')
Where [SCI] is the character, a resulting SELECT query will return 'abc?123'.
However, if I use NCHAR(154), by directly inserting or by using a REPLACE command such as:
UPDATE Table SET col1 = REPLACE(col1, '?', NCHAR(154))
The character is stored correctly.
My question is, why? And how can I store it directly from an INSERT statement? The latter is preferable as I am writing from an existing application that produces the INSERT statement that I don't really want to have to change.
Thank you in advance for any information that may be useful.
When you write a literal string in SQL is is created as a VARCHAR unless you prefix is with N. This means if you include any Unicode characters, they will be removed. Instead write your INSERT statement like this:
INSERT INTO Table(col1) VALUES (N'abc[SCI]123')

Access97: INSERT INTO fails without a table or a query

I need to execute a simple INSERT INTO query on an old Access97 database.
I'm starting with a very short example - which doesn't work:
INSERT INTO [MY-TABLE]( [Field1] )
VALUES ( "blabla" )
MY-TABLE is the actual name of the table and Field1 is a String field.
I get the error:
Query input must contain at least one table or query
Because I need to insert literal value, I don't want to use a query here (i.e. a SELECT FROM)
Reading also the docs (https://msdn.microsoft.com/en-us/library/bb208861(v=office.12).aspx) I don't see where my SQL is wrong.
UPDATE
Here a couple of screen shot of the actual table and fields:
here the SQL code:
Anyway...
SOLVED!!
It works even with double quotes.
The problem was the push button I was using to check: using the "View" button leads to the error above. Instead I must use the "exclamation mark".
I need to execute the query using the "exlamation mark" button instead of the view one. This is because my query has not a result set to view - hence the error I saw.
By the way I confirm the syntax is accepted both with single or double quotes.

table with "." in its name

I was trying to use sqlFetch. The fetch works perfectly when I change the name of my table to have underlines instead of periods. So if I use the command
sqlFetch(conn, "HelloWorld_40")
It works fine. Unfortunately, my friends are all using the real name of the table
sqlFetch(conn, "HelloWorld.40")
But then it crashes and it tells me that
Error in sqlColumns(conn, "HelloWorld.40") :
'HelloWorld.40': table not found on channel
I'm guessing the period "." is illegal name for a table. But I don't want my friends to change it because it's a lot of people who would be affected. Is there a way I can call the table, or do I have to secretly go to their database, change the name while I use it and then change it back to a period (risking that I will forget, someone will read, blah blah).
Thanks.
put the table name in square brackets:
[HelloWorld.40]
It is a problem with sqlFetch which parse table name. Unfortunately it did not handle table quotes, so it's search for table 40 in schema HelloWorld. You need to directly call sqlQuery (with quoted table name, brackets for MS SQL Server):
sqlQuery(dbhandle, "SELECT * FROM [HelloWorld.40]")
Side note: you should specify which database you are using.
The best delimiter is double quotes -- that should work in most underlying databases:
"HelloWorld.40"
In MySQL, you can also use back ticks (`):
`HelloWorld.40`
In SQL Server, Access, and I think Sybase, you can also use square braces:
[HelloWorld.40]

Check whether field exists in SQLite without fetching them all

I am writing a database abstraction layer that also abstracts some of the different query types. One of them is called "field_exists" - its purpose should be pretty self-explanatory.
And I want to implement that for SQLite.
The problem I am having is that I need to use one query that either returns a row confirming that the field exists or none if it doesn't. Thus, I cannot use the PRAGMA approach.
So, what query can I use to check whether a field exists in SQLite, that fulfills the above criteria?
EDIT: I should add that the query needs to be able to run in PHP code (using PDO).
Also, the query should look something like this (which only works with MySQL):
SHOW COLUMNS FROM table LIKE 'field'
Trying to select a field that doesn't exist will return an exception, then you can catch it and return nothing.
Use the .schema TABLENAME command. It will tell you the command that was issued to create the table. For more info chekcout the SQLite command shell documentation.
If you don't have access to the sqlite command line, you can always query the sqlite_master table. Let's say you want to know the command used to create the table MyTable. You'd issue this:
select sql from sqlite_master where name='MyTable';
This then gives you the sql command that was used to create the table. Then just grep through that output and see if the column you're looking for is in the command used to create the table.
UPDATE 2:
Actually better than the sql I posted above, you can use this:
PRAGMA table_info(*table_name*)
This will show you all the columns in a given table along with their types and other info.