string-concatenation


sybase - simple concatenation


I am trying to create a utility stored proc, that can take a parameter and query a handy table for retrieving some quick info. For this I need to add '%' character before and after the param passed so as to use it in the query with like, as shown below.
Working code - if hardcoded
select some_field from TABLE where nm_field like ('%'+'abc'+'%')
But when I try to use the same with a variable, I get lesser results as compared to above query
declare #nm char(100)
select #nm = 'abc'
select some_field from TABLE where nm_field like ('%'+#abc+'%')
On some investigation, trial and error, the following select shows that '%' is added only before the param, and not at both sides.
declare #name char(20), #other char(20)
select #name = 'abc'
select #other = ('%' + #name + '%')
select #other
Expected Output
%abc%
But actual output
%abc
I am using Sybase ASE-12.5.4
Can you please help me out with what is going wrong here?
Try to change #nm type to varchar as below
declare #nm varchar(100)
select #nm = 'abc'
select some_field from TABLE where nm_field like ('%'+#abc+'%')
Char type has constant size and db fill a gap using spaces
declare #nm char(100)
select #nm = 'abc'
below select
select #nm
will return abc and 97 spaces.
It seems, I have found the correct answer. It's the usage of char that is leading to this behaviour, since it is of fixed size. varchar isn't such and hence the concatenation works as expected.
With char, we need to add trim so that concatenation happens as expected
declare #str char(100)
select #str ='abc'
select ('%'+ltrim(rtrim(#str))+'%')
Output
%abc%

Related Links

ConcatRelated Function Returns All Values
In java properties file itself can we concatenate two or more than two variables together?
“No message” when writing string with console.log()
Moqui:We can use create an view with concatenating two or more columns as single column. simple can we do concatenatio in moqui view creqtion.
Error in ConcatRelated
String Concatenating functions in Progress 4GL?
sybase - simple concatenation
How do you concatenate strings in a Puppet .pp file?

Categories

HOME
service-worker
deep-learning
static
pascal
grafana
opendj
emscripten
maxscript
zip
angularfire2
extjs4.2
octave
why3
line
libc
radgridview
renjin
flatpak
parameter-passing
job-scheduling
sencha-touch
lilypond
ksh
resourcebundle
beta
google-api-client
tiki-wiki
onload
definitelytyped
pic32
fat
jasmine-node
calculation
livelink
catch-unit-test
papaparse
pdw-file-browser
wurfl
graphhopper
reactivekit
sessionstorage
xdocreport
password-protection
jvmti
odoo-website
attributeerror
importerror
snapchat
jide
getchar
tinymce-3
x-ray
scalding
mouseleave
materialized-path-pattern
webjars
dotnetnuke-7
lean
freepbx
libav
findfirst
exim4
character-replacement
java-money
file-move
intel-c++
pax-exam
visual-sourcesafe-2005
ember-cli-rails
zxspectrum
mmwormhole
java-security
code-documentation
cross-join
exceldatareader
infosphere-spl
ejb-2.x
pseudo-class
iso-prolog
psr-4
wxformbuilder
alternate
dot42
cfcache
breakout
rolify
neos-server
kaleidoscope
argouml
patricia-trie
fragmenttransaction
boost-signals
inkcanvas
mapping-by-code
database-create
usertype
koken
ikimagebrowserview
clrstoredprocedure
appledoc
squishit
load-time
eclipse-marketplace
folding
zend-rest
request-headers
purepdf
cuda.net
sqlobject
firefox-3
technical-debt
soappy
contentpresenter

Resources

Encrypt Message