SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


variables and Escape quotes


variables and Escape quotes

Author
Message
TJT
TJT
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 254
I am trying to create a variable like the following in a WHERE clause

DECLARE @Test AS VARCHAR(90)
SET @Test = '2003 OR Cars.year = 2004 OR Cars.year = 2004'

SELECT Cars.year
FROM CARS
WHERE Cars.year = @Test


Is this possible to set a variable like this? I need to escape the "OR Cars.year =" part it seems
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5010 Visits: 5478
TJT (5/15/2013)
I am trying to create a variable like the following in a WHERE clause

DECLARE @Test AS VARCHAR(90)
SET @Test = '2003 OR Cars.year = 2004 OR Cars.year = 2004'

SELECT Cars.year
FROM CARS
WHERE Cars.year = @Test


Is this possible to set a variable like this? I need to escape the "OR Cars.year =" part it seems


No, it's invalid T-SQL construction.
You should use the following:


SELECT Cars.year
FROM CARS
WHERE Cars.year IN (2003,2004,2005)



or you can build the whole statement as dynamic SQL and execute it, however I don't think that is really applicable for your case.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)

Group: General Forum Members
Points: 837 Visits: 1721
I would use Jeff Moden's DelimitedSplit8K function to do this:

First create some sample data:



IF OBJECT_ID('tempdb..#Cars') IS NOT NULL
DROP TABLE #Cars

CREATE TABLE #Cars (
[ID] INT IDENTITY(1,1) NOT NULL,
[CarBrand] VARCHAR(10) NULL,
[CarYear] INT NULL,
PRIMARY KEY (ID))

INSERT INTO #Cars
SELECT 'Chevrolet',2003 UNION ALL
SELECT 'Ford',2005 UNION ALL
SELECT 'Toyota',2006 UNION ALL
SELECT 'Ford',2012 UNION ALL
SELECT 'Saturn',2008 UNION ALL
SELECT 'Honda',2005 UNION ALL
SELECT 'Chevrolet',2004

SELECT * FROM #Cars




This is how to filter using DelimitedSplit8k:



DECLARE @Test AS VARCHAR(90)
SET @Test = '2003,2004,2005'

SELECT
c.CarBrand
,c.CarYear
FROM
#Cars c
INNER JOIN
dbo.DelimitedSplit8k(@Test,',') as dsk
ON c.CarYear = dsk.Item




Output



CarBrand CarYear
Chevrolet 2003
Chevrolet 2004
Ford 2005
Honda 2005



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search