I have a long string variable (e.g. 123456WALMARTabcdefghi 67815622 \\). I have a table with two columns, AccountNum and Account Name.
Account Name contains 'Walmart' as one of the values. The AccountNum for that row is 78910.
What SQL command would return the AccountNum of 78910?
Edit: clarified that the AccountNum is not found in the long string variable.
This is the table that I'm trying to retrieve the AccountNum from.
| Account Num | Account Name |
|---|---|
| 78910 | Walmart |
| 111213 | Costco |
CREATE TABLE [dbo].[CUSTOMER]
(
[Account Num] [nvarchar](50) NOT NULL,
[Account Name] [nvarchar](20) NOT NULL,
[RECID] [bigint] NOT NULL
)
ON [PRIMARY]
The expected output would be 78910 from the AccountNum column. I have tried various string commands (LIKE, CHARINDEX), but I'm not sure where to go from here.
CodePudding user response:
DECLARE @WANTED AS VARCHAR(50)
SET @WANTED = "Walmart"
IF CHARINDEX(@WANTED,@yourVariable) > 0
begin
SELECT AccountNum FROM table WHERE AccountName = @WANTED
end
else
select 'Not Find' As Result
