How to use apostrophe in SQL [duplicate] - sql

This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 6 years ago.
This value is in a column in a table:
'962091','962092','962093'
I try to use this in a where. First I declare a variable:
DECLARE #KPLnr varchar(100)
SET #KPLnr = CONVERT(nvarchar(max), dbo.UF_GetOption('FastecKPL')) /* here I get the values in */
If I select, I get the correct values of #KPLnr: '962091', '962092','962093', but if I try to use it in a where statement, it seems like the value is set wrong.
I get 0 results, but if I set it manually with:
WHERE c.kpl IN ('962091', '962092','962093')
I got 414 results.
So why is WHERE c.kpl IN ('962091', '962092', '962093') not equal to
WHERE c.kpl IN (#KPLnr) in my code?

When an apostrophe is stored in a text column, you need to escape it by adding an extra apostrophe:
WHERE c.kpl IN ('962091'', ''962092'',''962093')

Related

SQL Server - Insert value in string at dynamic position [duplicate]

This question already has answers here:
Replace first occurrence of substring in a string in SQL
(4 answers)
Closed 8 months ago.
I have a table with multiple entries in the "Test1" column
These are for example:
123ABCsignal2342
23ABsignal234signal
ABBSDsignal2signal
I want to update the entries like this:
123ABC.signal2342
23AB.signal234signal
ABBSD.signal2signal
The position for inserting the dot (.) is always in a different place and is signed by the expression "signal". Even if "signal" occurs several times, the point should be inserted only once at the beginning.
Declare #strs varchar(50) = '23ABsignal234signal'
SELECT STUFF(#strs
, CHARINDEX('signal', #strs)
, LEN('signal')
, '.signal')
OR
As Suggested Squirrel
Declare #strs varchar(50) = '23ABsignal234signal'
SELECT STUFF(#strs, CHARINDEX('signal', #strs), 0, '.')

How to get only part of word from column and remove everything before and after it using PostgreSQL [duplicate]

This question already has answers here:
PostgreSQL substring get string between brackets
(2 answers)
Closed 1 year ago.
I have the following details column, with varying parameters. How can I get only joblib values? "The Place of joblib is not always the same, so I may bot be able to use substring count"
date:01/12/2014--**--joblib:[snbsd]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2014--**--joblib:[jinxthin]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2014--**--joblib:[snbserv]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2016--**--joblib:[sql12server]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2015--**--joblib:[stfmbinserx]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2011--**--joblib:[ftplikes]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
Desired result:
snbsd
jinxthin
snbserv
sql12server
stfmbinserx
ftplikes
Here You go:
substring(substring(var1, position('joblib:' in var1)+8), 1, position(']' in substring(var1, position('joblib:' in var1)+8))-1)
replace var1 with column name containing Your string
With below You can try it out directly on PostgreSQL:
WITH myconstants (var1) as (
values ('date:01/12/2014--**--joblib:[snbsd]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto')
)
SELECT substring(substring(var1, position('joblib:' in var1)+8), 1, position(']' in substring(var1, position('joblib:' in var1)+8))-1)
FROM myconstants

SQL Select statement how to return emoji [duplicate]

This question already has answers here:
SQL Query Where Column = '' returning Emoji characters 🎃 and 🍰
(4 answers)
Closed 3 years ago.
I currently have a query where I want to select cake:
SELECT '🍰'
But upon executing query, it gives me an output of '??'
How to output the real cake?
set as unicode using the N'' notation
SELECT N'🍰'

Getting an error when trying to remove comma from a freetext column in SQL [duplicate]

This question already has answers here:
alternatives to REPLACE on a text or ntext datatype
(2 answers)
Closed 5 years ago.
USE DBname
UPDATE notestable
SET NotesColumn= REPLACE(NotesColumn,',','')
WHERE NotesColumn LIKE '%,%'
when i try execute this code, i'm getting the below error.
Msg 8116, Level 16, State 1, Line 2
Argument data type text is invalid for argument 1 of replace function.
i managed to do similar code a week ago and removed all inverted commas from this column already but there is clearly something i'm missing out. (I changed the names purposely since we work for a government entity)
I'm using the code from this post but its still not working: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/12c2e9bf-a5c2-484a-a7b3-021890c61963/how-to-remove-comma-fro-sql-string?forum=transactsql
You cannot run a REPLACE on a text data type. Convert it to [n]varchar(max) first.
USE DBname
UPDATE notestable
SET NotesColumn= REPLACE(CONVERT(nvarchar(max), NotesColumn),',','')
WHERE NotesColumn LIKE '%,%'
Try it maybe problem depends on text datatype, convert it to varchar(max)
USE DBname
UPDATE notestable
SET NotesColumn= REPLACE(CONVERT(VARCHAR(MAX), fieldName),',','')
WHERE NotesColumn LIKE '%,%'
USE DBname
UPDATE notestable
SET NotesColumn= CAST(REPLACE(CAST(NotesColumn AS nvarchar(MAX)),',','')AS ntext)
WHERE NotesColumn LIKE '%,%'

SQL Detecting if only integers in varchar [duplicate]

This question already has answers here:
How to get only numeric column values?
(5 answers)
Closed 8 years ago.
I'm hoping that there is some basic string function which will do this that I just haven't found yet.
Specifically for scenarios where length of the string is unknown:
Is there a succinct method of telling if a string variable only contains [0-9] values?
For example, the following method works but require additional mention of all possible characters that would appear other than numerals.
SELECT VAR
FROM #TABLE1
WHERE VAR NOT LIKE '%[A-Z]%'
and
SELECT VAR
FROM #TABLE1
WHERE VAR LIKE '[0-9][0-9][0-9][0-9][0-9]...'
doesn't work because the length of the VAR is required for this filter.
Thanks in advance
SELECT VAR FROM #TABLE1
WHERE VAR NOT LIKE '%[^0-9]%'