May 28, 2013 at 8:57 am
Can you declare an operator?
DECLARE @MYTEST AS VARCHAR(3)
SET @MYTEST = '<>'
May 28, 2013 at 8:59 am
TJT (5/28/2013)
Can you declare an operator?DECLARE @MYTEST AS VARCHAR(3)
SET @MYTEST = '<>'
Well you can declare a varchar that has those characters. Not sure what you are wanting to do with it after that.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 28, 2013 at 9:01 am
Your script is definitely valid. But how are you planning to use it?
Whether this is really useful depends on what you do next .
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 28, 2013 at 9:06 am
I was hoping to use it in WHERE statement and this is just an example.
DECLARE @MYTEST AS VARCHAR(3)
SET @MYTEST = '<>'
SELECT CARS.Year
FROM CARS
WHERE CARS.Year @MYTEST '2011'
May 28, 2013 at 9:12 am
TJT (5/28/2013)
I was hoping to use it in WHERE statement and this is just an example.DECLARE @MYTEST AS VARCHAR(3)
SET @MYTEST = '<>'
SELECT CARS.Year
FROM CARS
WHERE CARS.Year @MYTEST '2011'
No that won't work like that. You could do this with dynamic sql but I have a feeling there is a lot more here than this simple query.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 28, 2013 at 9:18 am
TJT (5/28/2013)
I was hoping to use it in WHERE statement and this is just an example.DECLARE @MYTEST AS VARCHAR(3)
SET @MYTEST = '<>'
SELECT CARS.Year
FROM CARS
WHERE CARS.Year @MYTEST '2011'
Not like you could in .NET or C++ (or other programming languages) .
However, as mentioned by Sean, you can use it in dynamic SQL:
DECLARE @MYTEST AS VARCHAR(3)
DECLARE @sql AS NVARCHAR(4000)
SET @MYTEST = '<>'
SET @sql = '
SELECT CARS.Year
FROM CARS
WHERE CARS.Year ' + @MYTEST + '2011'
EXEC (@SQL)
May 28, 2013 at 5:00 pm
Hi
You might be able to create a function to handle it, eg:
CREATE FUNCTION dbo.myOpCarYear (@op varchar(2), @value varchar(4)) RETURNS TABLE AS
RETURN
(SELECT CARS.Year FROM CARS WHERE @op = '<>' and Year <> @value
UNION ALL
SELECT CARS.Year FROM CARS WHERE @op = '=' and Year = @value
UNION ALL
SELECT CARS.Year FROM CARS WHERE @op = '>' and Year > @value
UNION ALL
SELECT CARS.Year FROM CARS WHERE @op = '<' and Year < @value)
And use like this
SELECT Year FROM dbo.myOpCarYear('<>','2011')
Unfortunately I can't test this as my laptop fried it's hard drive and I haven't fully restored it yet.
May 28, 2013 at 9:27 pm
Simply question, Why? What is the business for doing this?
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply