View Full Version : SQL query help


Notsogood
03-09-2007, 02:29 AM
I have 2 separate queries which I want to add as IF-Then statements, is that possible at all?
The 2 queries are:
First
Select SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 38, 10) AS PrimaryNumber,SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)AS Type
FROM dbo.SQL_try
Where SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)<>'A' AND SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)<>'V'
Second
SELECT SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 28, 10) AS Primarynumber, SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)AS Type
FROM dbo.SQL_try
Where SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)='A' OR SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)='V'

Please can someone help me.
Thanks in advance

SQL_Hell
03-09-2007, 03:13 AM
Just to clarify.... you want an IF statement to run one the queries and an else to run the other?

If that's what you meant then use a sql server stored procedure, something like this....


IF (@ErrorSaveVariable <> 0)
BEGIN
Select SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 38, 10) AS PrimaryNumber,SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)AS Type
FROM dbo.SQL_try
Where SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)<>'A' AND SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)<>'V'
END
ELSE
BEGIN
SELECT SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 28, 10) AS Primarynumber, SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)AS Type
FROM dbo.SQL_try
Where SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)='A' OR SUBSTRING(CONVERT(varchar(250), dbo.SQL_try.filedata), 25, 1)='V'

END

Notsogood
03-09-2007, 04:33 AM
thanks. I created a store procedure but it is coming up with errors.I must be honest here and admit I don't understand SQL much at all.
These are the error msgs:
Must declare the variable '@ErrorSaveVariable'.
Incorrect syntax near the keyword 'ELSE'.
thanks

SQL_Hell
03-09-2007, 04:55 AM
Hi,


@ErrorSaveVariable' was just something I used as an example, I didn't mean use that in your code.

so what is going to be in your IF statement i.e. what is the IF condition?

Notsogood
03-09-2007, 06:12 AM
Basically I want capture the Primary numbers and put it in a table or text file.What is the best way of doing it?
Say if type is A then get this peice of string as Primary numbers
Say if type is L then get another peice of string as Primary numbers
I would like to get this as a list.
Thanks

SQL_Hell
03-09-2007, 07:51 AM
Ok where is type coming from? is it a value on a form?

What are you using for your front end?

Notsogood
03-12-2007, 12:55 AM
Ok I will explain from the beginning.there is no front end.SQL_try is a table and file data is a field name.It has a long string in it.I am trying to capture the nth letter as type.And if type is "A", then capture numbers from 'a' to 'c' as primary_number.If not then capture 'd' to e' as Primary number.
The data is in the long string itself.
Does it make sense at all?
Thanks