Inserting Special Characters in SQL insert query - sql

The sql statement I am trying to execute is
INSERT INTO Scanners (Character) VALUES ('~')
The characters I would like to insert are:
{,}, [, ^ , & , ~, ` , : , ;.
But it says syntax error. Any help would be appreciated.

You can try to use [].
INSERT INTO [Scanners] ([Character]) VALUES ('~')

You can use the Chr function to insert special characters. So for example, to insert '~', you could do the following:
INSERT INTO Scanners (Character) VALUES ('' || Chr(126) || '') -- 126 is ascii for ~
Note, I'm not in front of my machine with environment set up to test this.

Related

How to insert regex with special characters like “&,?.#:;” in oracle database?

I have a regex value that I want to insert in oracle database table column but I have some problems that the value doesn't inserted correctly in the database
this is the value that I want to insert :
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)?]', 1);
the result of this insert is :
valid_value
+--------------------------------------+
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)]
I lose the "?" character can some one help me on this how to insert regex value in a table column.
I start the script using a batch file
set DB_CREATE_ROOT="%~dp0"
set SQL_INIT_CONF_DIR=%DB_CREATE_ROOT%\Scripts\configuration\
for /r %SQL_INIT_CONF_DIR% %%F in (*init.sql) do (
ECHO %DATE% %TIME% "%%F" >> %LOG_NAME%
sqlplus -L Test_APP/welkom#//localhost:1521/xe #"%%F" >> %LOG_NAME% 2>&1
)
thanks in advance
You can use CHR function for each of the special characters if you find difficulties with direct inserts.
Thanks
For the records, none of the characters mentioned have a special meaning inside SQL strings thus they don't need any special treatment:
SQL> SELECT '&,?.#:;' AS are_we_special
2 FROM DUAL;
ARE_WE_
-------
&,?.#:;
(online demo)
... being & the only possible exception (“SET DEFINE OFF” in Oracle Database) and only in the context of SQL*Plus and SQL Developer—in which case you'd be getting a Enter value for xxxxx prompt.
Whatever problem the OP had, CHR() is just a workaround for his specific undisclosed issue.
This may help you
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)'?']', 1);
Try this:
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]', 1);
See demo:
In SQLPLUS:
SQL> /
COL1
---------------------------------------------
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]
Now question is which client you are using. In SQLPLUS, its working as shown above.

Replace ; with blank in SQL

I have a table like this:
DECLARE #T TABLE
(note VARCHAR (50))
INSERT #T
SELECT 'Amplifier'
UNION ALL SELECT ';'
UNION ALL SELECT 'Regulator'
How can I replace the semicolon (';') with blank ('').
Expected Output:
Amplifier
'' -- here semicolon replace with blank
Regulator
If you want to replace ALL semicolons from any outputted cell you can use REPLACE like this:
SELECT REPLACE(note,';','') AS [note] FROM #T
Fetching from the given table, use a CASE statement:
SELECT CASE WHEN note = ';' THEN '' ELSE note END AS note FROM #T;
replace() would replace all occurrences of the character. Doesn't seem like you'd want that. This expression only replaces exact matches of the whole string.
It looks like you need to REPLACE all your semicolons:
DECLARE #T TABLE
(note VARCHAR (50))
INSERT INTO #T
SELECT REPLACE(SourceColumn, ';', '')
FROM SourceTable
SQL Server 2017 introduced the function TRANSLATE where you can specifiy a list of characters to be replaced
SELECT TRANSLATE('MAX(0,MIN(h-36,8))', '(,-)', ' ') -->'MAX 0 MIN h 36 8 '

Alter All Column Values using TRIM in SQL

I want to modify (Remove all white spaces) all the values of Column
"Name" .
How do I use TRIM and modify all column values of Name .
Example :
Before : " rishi.ranka " After : "rishi.ranka"
Thank you very much in Advance.
Use a combination of LTRIM (Left Trim) & RTRIM (Right Trim):
UPDATE [TableName]
SET [Name]=LTRIM(RTRIM([Name]))
If the datatype of name column is varchar then don't need to use rtrim function the right side spaces will be automatically trim. use only LTRIM only.
update tablename
set name = ltrim(name)
where <condition>;
Run this see the how it trims the right spaces automatically.
DECLARE #mytb table
(
name varchar(20)
);
INSERT INTO #mytb VALUES (' stackoverflow ');
SELECT len(name) from #mytb;
SELECT ltrim(name),len(ltrim(name)) from #mytb;

Are SQL strings null terminated?

I was learning how to use len() function. When I found out the length of a cell having 12 characters, it gave me result 12. Now I was thinking that arent the SQL strings null terminated(As if they would have been then len() should have returned 13 not 12)?
Please help me out.
Thanks
Well, first - the len function does not depend on null termination, programming languages not using null termination ALSO have a len function an it works.
Thus, a len function in SQL will give you the length of the string AS THE SERVER STORES IT - what do you care how that works?
Actually it will likely not be null terminated as this would make it hard to split a string over multiple database pages. And even if - this would be seriously implementation dependent (and you don't say which product you mean - the SQL language says nothing about how the server internally stores strings).
So, at the end your question is totally irrelevant. All that is relevant is that the len function implementation is compatible with the internal storage.
In SQL Server, LEN will ignore trailing spaces (http://msdn.microsoft.com/en-us/library/ms187403.aspx) - here's a modified example from that link:
PRINT 'Testing with ANSI_PADDING ON'
SET ANSI_PADDING ON ;
GO
CREATE TABLE t1
(
charcol CHAR(16) NULL
,varcharcol VARCHAR(16) NULL
,varbinarycol VARBINARY(8)
) ;
GO
INSERT INTO t1
VALUES ('No blanks', 'No blanks', 0x00ee) ;
INSERT INTO t1
VALUES ('Trailing blank ', 'Trailing blank ', 0x00ee00) ;
SELECT 'CHAR' = '>' + charcol + '<'
,'VARCHAR' = '>' + varcharcol + '<'
,varbinarycol
,LEN(charcol)
,LEN(varcharcol)
,DATALENGTH(charcol)
,DATALENGTH(varcharcol)
FROM t1 ;
GO
PRINT 'Testing with ANSI_PADDING OFF' ;
SET ANSI_PADDING OFF ;
GO
CREATE TABLE t2
(
charcol CHAR(16) NULL
,varcharcol VARCHAR(16) NULL
,varbinarycol VARBINARY(8)
) ;
GO
INSERT INTO t2
VALUES ('No blanks', 'No blanks', 0x00ee) ;
INSERT INTO t2
VALUES ('Trailing blank ', 'Trailing blank ', 0x00ee00) ;
SELECT 'CHAR' = '>' + charcol + '<'
,'VARCHAR' = '>' + varcharcol + '<'
,varbinarycol
,LEN(charcol)
,LEN(varcharcol)
,DATALENGTH(charcol)
,DATALENGTH(varcharcol)
FROM t2 ;
GO
DROP TABLE t1
DROP TABLE t2
If we're talking pure SQL, there's no NUL terminator for you to worry about. If we're talking interfacing to SQL from other languages (e.g. C), then the answer depends on the language in question.
There are a couple of relevant points worth remembering:
There are two character types in SQL: CHAR(N) and VARCHAR(N). The former is always the same length (N) and padded with spaces; the latter is variable-length (up to N chars).
In Transact-SQL, LEN returns the length on the string excluding trailing spaces.
In some SQL strings are zero- terminated.
On some SQL they have a leading length byte/ word.
This of course does not matter for the len() function.
But it does matter if you want to insert a \0 into the string.
Usually varchar has a leading length byte.
But char is \0 terminated.

Insert a trailing space into a SQL Server VARCHAR column

I'm trying to insert trailing spaces into a VARCHAR(50) column and the SQL insert seems to be cutting them off. Here's my code:
create table #temp (field varchar(10));
insert into #temp select ' ';
select LEN(field) from #temp;
Unfortunately, this returns a length of zero, meaning the ' ' was inserted as a ''. I need a blank space to be inserted for this column - any ideas?
Use DATALENGTH, not LEN, because LEN doesn't process spaces.
Take this zero length string, for example:
SELECT LEN(' ') AS len,
DATALENGTH(' ') AS datalength
Results:
len datalength
-----------------
0 1
From http://msdn.microsoft.com/en-us/library/ms190329.aspx:
LEN Returns the number of characters of the specified string expression, excluding trailing blanks.
You better be aware also that SQL Server follows ANSI/ISO SQL-92 padding the character strings used in comparisons so that their lengths match before comparing them. So, you may want to use LIKE predicate for comparisons [1]
[1]
How SQL Server Compares Strings with Trailing Spaces
http://support.microsoft.com/kb/316626