Limitations of String Searches From MSAccess to SQl Server? - sql

I am doing a large string lookup on a small table.
When I do the following query in MsAccess:
SELECT isBit
FROM [SqlServerTableName]
WHERE mycolumnName = "[String Greater Than 128 characters]"
I get the message:
The identifier that starts with [String Greater Than 128 characters] is too long.
Maximum length is 128.
Followed by:
Unclosed quotation mark after the character string [String Greater Than 128 characters]
And:
Incorrect Syntax Near [String Greater Than 128 characters]
Now, is there a way around this? This table is very small and will stay very small(<1000 records) so I am not worried about string comparisons.

This is because you are using double quotes (") instead of single quotes (').
SQL Server is treating this as an identifier, and not as a string literal value.
You should change the query to be the following:
SELECT isBit
FROM [SqlServerTableName]
WHERE mycolumnName = '[String Greater Than 128 characters]'

Related

SQL Developer fills remaining space in char with spaces

I have a attribute with the data type char(256). I import the value via SQL Developer from a csv file
When the attribute gets a value with 10 characters, the remaining space gets filled with spaces.
I know that char allocates the space staticly, but does that also mean that I get a string in the format like "abc " ?
Since this make sql statements with equal operators difficult.
You are operating under a misconception; it has nothing to do with SQL Developer.
A CHAR data-type is a fixed-length string; if you do not provide a string of the full length then Oracle will right-pad the string with space (ASCII 32) characters until it has the correct length.
From the documentation:
CHAR Datatype
The CHAR datatype stores fixed-length character strings. When you create a table with a CHAR column, you must specify a string length (in bytes or characters) between 1 and 2000 bytes for the CHAR column width. The default is 1 byte. Oracle then guarantees that:
When you insert or update a row in the table, the value for the CHAR column has the fixed length.
If you give a shorter value, then the value is blank-padded to the fixed length.
If a value is too large, Oracle Database returns an error.
Oracle Database compares CHAR values using blank-padded comparison semantics.
To solve this, do not use CHAR for variable length strings and use VARCHAR2 instead.
VARCHAR2 and VARCHAR Datatypes
The VARCHAR2 datatype stores variable-length character strings. When you create a table with a VARCHAR2 column, you specify a maximum string length (in bytes or characters) between 1 and 4000 bytes for the VARCHAR2 column. For each row, Oracle Database stores each value in the column as a variable-length field unless a value exceeds the column's maximum length, in which case Oracle Database returns an error. Using VARCHAR2 and VARCHAR saves on space used by the table.
You may use varchar2 instead of char as datatype to avoid this.
Or you can trim your data in query by using rtrim(columnname) .

oracle sql query for selecting Even number columns

In oracle sql when I am trying to get the output for the below, it is throwing error.
select city,id from station where id % 2 = 0;
Error:
ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
The % operator for modulo is not supported in Oracle. You would need to use function mod():
select city,id from station where mod(id, 2) = 0;
Demo on DB Fiddle

ORA-00972 identifier is too long Oracle procedure

I have a Oracle procedure, its return query
SELECT CODE "Կադաստրային ծածկագիր", ENTAKAYANI "Ենթակայան"
FROM ELECTRIC_ENTAKAYAN_500
WHERE SDO_RELATE(GEOM,SDO_GEOM.SDO_BUFFER(
MDSYS.SDO_GEOMETRY(2001, 2400000,
MDSYS.SDO_POINT_TYPE(8451136.4,4451591.2,NULL),
NULL, NULL), 2, 0.005)
,'mask=ANYINTERACT')='TRUE'
AND rownum <= 10;
and problem this length("Կադաստրային ծածկագիր") = 20, Oracle max size length filed 30 symbol, but my string Armenian languages its > 30 symbol.
How to find an another solution?
If you are using Oracle 12cR2 you could use identifiers that are up to 128 bytes.
Database Object Naming Rules:
If COMPATIBLE is set to a value of 12.2 or higher, then names must be from 1 to 128 bytes long with these exceptions:
Names of databases are limited to 8 bytes.
Names of disk groups, pluggable databases (PDBs), rollback segments, tablespaces, and tablespace sets are limited to 30 bytes.
If an identifier includes multiple parts separated by periods, then each attribute can be up to 128 bytes long. Each period separator, as well as any surrounding double quotation marks, counts as one byte. For example, suppose you identify a column like this:
"schema"."table"."column"
The schema name can be 128 bytes, the table name can be 128 bytes, and the column name can be 128 bytes. Each of the quotation marks and periods is a single-byte character, so the total length of the identifier in this example can be up to 392 bytes.
Please keep in mind that byte != character.
SELECT /*csv*/ 1 AS "Կադաստրային ծածկագիր" FROM dual;
/*
"Կադաստրային ծածկագիր"
1
*/
And counting characters/bytes:
SELECT
length('ադաստրային ծածկագիր 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890') AS char#
,lengthb('ադաստրային ծածկագիր 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890') AS bytes#
FROM dual;
/*
CHAR# BYTES#
---------- ----------
120 138
*/

SQL(ite) where clause with a colon (:) character

I have a SQLite table column holding MAC addresses.
How do I write the SQL where clause for a string value including a colon ':' character? Surrounding it with quote characters doesn't work.
So far, I've been getting this error:
android.database.sqlite.SQLiteException: near ":66": syntax error (code 1): , while compiling: SELECT * FROM PhoneStatus WHERE phoneDeviceId=00:66:4B:B2:7B:F5
Thanks!
You would seem to need single quotes for the string constant:
SELECT *
FROM PhoneStatus
WHERE phoneDeviceId = '00:66:4B:B2:7B:F5'
If the query is delimited by single quotes, then double them up or escape them somehow.

Select truncated string from Postgres

I have some large varchar values in Postgres that I want to SELECT and move somewhere else. The place they are going to uses VARCHAR(4095) so I only need at most 4095 bytes (I think that's bytes) and some of these varchars are quite big, so a performance optimization would be to SELECT a truncated version of them.
How can I do that?
Something like:
SELECT TRUNCATED(my_val, 4095) ...
I don't think it's a character length though, it needs to be a byte length?
The n in varchar(n) is the number of characters, not bytes. The manual:
SQL defines two primary character types: character varying(n) and
character(n), where n is a positive integer. Both of these types can
store strings up to n characters (not bytes) in length.
Bold emphasis mine.
The simplest way to "truncate" a string would be with left():
SELECT left(my_val, 4095)
Or just cast:
SELECT my_val::varchar(4095)
The manual once more:
If one explicitly casts a value to character varying(n) or
character(n), then an over-length value will be truncated to n
characters without raising an error. (This too is required by the SQL standard.)