Applying semicolon to the values in a table in SQL - sql

Currently I am working on an SQL script. The result of the script is a table with three columns.I want to apply a semicolon to the left side of all the values.
Select distinct
emp_id, -->numeric value
emp_dob, -- >numeric value
emp_ssid -- >alphnumeric value
.
.
How can I do it. I am quite new to SQL

If you are using Oracle, I think you want ||:
Select distinct emp_id || ';',
emp_dob || ';' -- >numeric value
emp_ssid, || ';' -- >alphnumeric value
Your syntax strongly suggests SQL Server, but the question is tagged Oracle.

Because you wanted to append a semicolon to the end of the value, your result column cannot be converted to a numeric value. You need to cast it to nvarchar and then append.
Please try
CAST(emp_id AS NVARCHAR(20)) + ';'

Use CONVERT to turn a numeric column into string. The string concatenation operator in Sybase is the non-standard +.
select
';' + convert(varchar, emp_id),
';' + convert(varchar, emp_dob),
';' + emp_ssid
from ...

Related

SQL Server reducing the length of the string to 8000 characters

I am trying to insert data in a table with column datatype as NTEXT. Ideally it should store more than 8000 characters, but the in my case it is reducing it to 8000 characters.
I am making the Insert Query at runtime in Procedure. Below is the sample query that procedure is making.
INSERT INTO TMPRESULTS SELECT ('A' + ',' + 'B' + ',' + 'C')
A,B,C, etc. are sample data and actual data will be identified at runtime with actual content crossing 8000 characters. Also the variable used to store the value are defined as 'NVARCHAR(MAX)'
However, when I try following query it does insert more than 8000 character in the table
INSERT INTO TMPRESULTS SELECT ('ABCdddd................')
I presume while I am trying to concat the data with '+' sign, sql server is reducing the length to 8000. I can't use CONCAT as data will be more than 256 columns/arguments.
Any idea, why it is doing so? Also, if someone can help with some alternate solution as I will have to make insert query at runtime.
This is documented in + (String Concatenation) (Transact-SQL) - Remarks:
If the result of the concatenation of strings exceeds the limit of
8,000 bytes, the result is truncated. However, if at least one of the
strings concatenated is a large value type, truncation does not occur.
For a varchar 8,000 bytes would be 8,000 characters, and for a nvarchar 4,000.
All your literal strings in the query INSERT INTO TMPRESULTS SELECT ('A' + ',' + 'B' + ',' + 'C') are non large value types (In fact, they are all a varchar(1)). If you CONVERT/CAST one of them to a varchar(MAX) this would solve the problem:
INSERT INTO TMPRESULTS
SELECT (CONVERT(varchar(MAX),'A') + ',' + 'B' + ',' + 'C');
if you want an nvarchar, make sure you declare your literal strings as a nvarchar too:
INSERT INTO TMPRESULTS
SELECT (CONVERT(nvarchar(MAX),N'A') + N',' + N'B' + N',' + N'C');
In SQL Server 2017 onwards, there is CONCAT_WS function to perform concatenation easily. You can also read about CONCAT
So, instead of this:
INSERT INTO TMPRESULTS SELECT ('A' + ',' + 'B' + ',' + 'C')
We can have below:
INSERT INTO TMPRESULTS SELECT CONCAT_WS(CAST(N',' AS NVARCHAR(MAX)),'A','B','C'))
I have put sample below from SQL Server 2017 for reference:
CREATE TABLE #tempValue(BigValue NVARCHAR(MAX))
INSERT INTO #tempValue
SELECT CONCAT_WS(CAST(N',' AS NVARCHAR(MAX)),REPLICATE('A',4000),REPLICATE('B',4000),REPLICATE('C',4000))
SELECT LEN(BigValue) FROM #tempValue -- 12002
Also, CONCAT_WS is better for below reasons:
If CONCAT_WS receives arguments with all NULL values, it will return
an empty string of type varchar(1).
CONCAT_WS ignores null values during concatenation, and does not add
the separator between null values.

Concate Primary Keys in SQL

I want to concate Primary Keys of multiple tables in SQL directly. I used below query to concate three primary keys with a hyphen between them but the SQL skipped the hyphen and sum up the primary keys and result in a single value.
SELECT CID + '-' + RID + '-'+ CGID As [IdCombination] ...
where CID , RID and CGID are the Primary Keys of three SQL Tables.
How it skipped the string part in query ?
Any help would be highly appreciated.
Updated
For Example : The Values of CID , RID and CGID are 3 , 4, 3 respectively. It should be 3-4-3 but the result is 10.
What is happening? Remember that + means both addition and string concatenation. It so happens that - can be interpreted as a number (like -0), so SQL Server prefers to interpret the + as addition.
Normally, when you do this type of operation, the separation character cannot be interpreted as a number, and you just get an error. I am amused that you don't get an error in this case.
One method is to explicitly cast the values as strings:
SELECT CAST(CID as VARCHAR(255)) + '-' + CAST(RID + as VARCHAR(255)) '-'+ CAST(CGID as VARCHAR(255)) As [IdCombination]
In SQL Server 2012+, you can do this more simply using CONCAT():
SELECT CONCAT(CID, '-', RID, '-', 'CGID) As [IdCombination]
CONCAT() knows that everything should be a string.
Try this
SELECT 5 + '-' + 8
The output is 13. I must admit, that I did not expect this...
And now try this
SELECT CAST('-' AS INT)
The result is 0. As your select starts with an INT, SQL Server tries to do a summa of int values. As the single hyphen is casteable to int implicitly, this returns the summa of your values...
The solution, as pointed out by others is either a cast of your column values to a string type or the usage of CONCAT
I would need to see output, but I am assuming, some ID is stored as int and it is being counted, so you should use
SELECT Cast(CID as Varchar(50)) + '-' + Cast(RID as Varchar(50)) + '-'+ Cast(CGID as Varchar(50)) As [IdCombination]

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 '

Add keyword before and after cast() in oracle

I would like to add a keyword before and after each field value in Oracle.
For example if I am getting 123 as my ID, I would like to make it
Test123Test
Here is my query:
SELECT
CAST("ID" as varchar(10))
FROM
TABLENAME;
I have tried add + "Test" but it is giving me error.
Use || instead of + to concatenate strings in Oracle.
SELECT 'test' || CAST(ID as varchar(10)) || 'test'
FROM TABLENAME
Note that I removed the " around ID too, since you most likely won't need them and they can cause problems when it unintended strictly matches column names.
I have tried add + "Test" but it is giving me error.
Perhaps, + is used as concatenation in SQL Server. In Oracle, you could use the CONCAT function or the || operator.
The concat function is restricted to only two strings. You can have a look the concat function in the documentation http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions026.htm.
Let's see an example using the operator -
SELECT 'test' || to_char(id) || 'test' new_id
FROM TABLENAME

SQL wont let me replace a quote in SELECT statement

I am trying to remove double quotes " from a column in my SQL export and I get an error, after researching the proper way... this is one of the ways I have tried....
SELECT
'293453' as custnum,
REPLACE(Orders.Order_Comments, '"', '') as FULFILL1,
OrderDetails.OrderID as OrderID2, etc.
The resulting error is:
Your SQL is invalid: Argument data type text is invalid for argument 1 of replace function.
Your Orders.Order_Comments columns is of type text. You can't use the REPLACE() function with that data type. It only works with char/varchar/nchar/nvarchar.
To fix this, the best thing to do is ALTER the table to use a varchar(max) column. The text type is depcrecated, anyway. But, if that's not an option, you'll have to cast it in the query:
REPLACE(CAST(Orders.Order_Comments as varchar(max)), '"', '')
Note that this is potentially very slow.
Take a look at this answer: SQL Server find and replace in TEXT field
The text data type is not valid for use with replace, so you need to cast it as VARCHAR(MAX)
REPLACE(CAST(Orders.Order_Comments AS VARCHAR(MAX)), '"', '')
Try this:
select REPLACE(isnull(Orders.Order_Comments,''), '"', '') as order_comments
from orders