Avatar of emi_sastra
emi_sastra

asked on 

SQL Variable Problem

Hi All,

I have below script :

DECLARE @POSITIONS VARCHAR(100) = '''SLF'', ''SRF'''

select @POSITIONS as POSITIONS
 
 SELECT 
 A.PositionCode
 FROM TMPOSITION A
 WHERE A.PositionCode IN ('SLF', 'SRF')

  SELECT 
 A.PositionCode
 FROM TMPOSITION A
 WHERE A.PositionCode IN (@POSITIONS)

Open in new window


The result :

POSITIONS
----------------------------------------------------------------------------------------------------
'SLF', 'SRF'

(1 row(s) affected)

PositionCode
------------
SLF 
SRF 

(2 row(s) affected)

PositionCode
------------

(0 row(s) affected)

Open in new window


What is wrong with @Positions ?

Thank you.
Microsoft SQL Server

Avatar of undefined
Last Comment
emi_sastra
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of emi_sastra
emi_sastra

ASKER

Hi Stefan,

It does not work with your code.

DECLARE @POSITIONS VARCHAR(100) =  '/SLF/SRF/';

select @POSITIONS as POSITIONS
 
 SELECT 
 A.PositionCode
 FROM TMPOSITION A
 WHERE A.PositionCode IN ('SLF', 'SRF')

  SELECT 
 A.PositionCode
 FROM TMPOSITION A
 WHERE CHARINDEX('/' + A.PositionCode + '/', @POSITIONS) > 0;

Open in new window


Thank you.
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of emi_sastra
emi_sastra

ASKER

Hi Eric,

Using RTRIM works.

Thank you very much for your help.
Avatar of emi_sastra
emi_sastra

ASKER

Hi Stefan and Eric,

Thank you very much for your help.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo