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

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, '.')

Related

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'🍰'

How to use apostrophe in SQL [duplicate]

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')

Is it possible to reference a variable via string manipulation? [duplicate]

This question already has answers here:
T-SQL: How to use parameters in dynamic SQL?
(4 answers)
how to pass variables this in dynamic query in sql
(1 answer)
Closed 7 years ago.
I have a series of datetime variables that need to be referenced, and the key to which variable needs referencing is reached by using substring on another field. I can create the name of the variable as a string using string manipulation, something like this:
'#' + SUBSTRING(field, CHARINDEX(',', field) + 2, LEN(field) - CHARINDEX(',', field) + 1) + 'start'
Is there a way to get SQL to recognize that this is the name of one of my variables? Some kind of function to convert the string?

How to Convert text values to integer values [duplicate]

This question already has answers here:
How do I cast a string to integer and have 0 in case of error in the cast with PostgreSQL?
(14 answers)
Closed 7 years ago.
I have a problem to convert column with text values into integer values.
In table address i have column 'postcode' type TEXT. I have created a new column name 'postcode_int' type integer.
In column 'postcode' some people wrote for example '330...' or '00234'.
How can i check if the value is an integer.And if a value is an Integer then how to convert this values from type TEXT into type Integer and set them in column 'postcode_int' so that later i can do 'between '11111' and '99999'.
i tried to do something like this:
UPDATE adresse
SET postcode_int= case
when pg_typeof( REGEXP_REPLACE( COALESCE(trim( LEADING '0' FROM postcode), '0'),
'[^0-9]*' ,'0')::integer
) = integer
then postcode_int
else 0
end;
With regards
Andrey
I believe the SQL you need is:
UPDATE adresse
SET postcode_int = CAST(postcode AS INT)
This is based on your example.
If this answer is not clear please ask for a revision.