user5921883
user5921883

Reputation:

How to get value inside quotes in SQL

I want to separate a fixed expression in SQL in order to get the string value inside ' '. For example in the following SQL:

declare @value varchar(60)
set @value = 'a.[country] LIKE ''US'''

I would like to store separately the information US, is there a way to do it? Thank you in advance.

Upvotes: 1

Views: 1436

Answers (2)

Vivek
Vivek

Reputation: 174

You can try this

declare @value varchar(60)
set @value = 'a.[country] LIKE ''US'''

select left(Right(@value,3),2)
--OR this
select substring(Right(@value,3), 1, 2) 

Upvotes: 2

SSD
SSD

Reputation: 1391

The question seems incomplete, but based on the question I suggest to use some Delimiter with the dynamic string and use combination of LEFT, CHARINDEX and SUBSTRING in SQL to fetch values out of SQL.

Example

Following is not the best example, but just explaining how to use it: e.g

declare @value varchar(60)
set @value = 'a.[country] LIKE §''US'''
SELECT LEFT(SUBSTRING(@value,
                      CHARINDEX('§', @value) + 1, 100),
                      CHARINDEX('§', @value) - 1)

Output

'US'

There are already some example about this on StackOverflow.

If you haven't checked out yet other StackOverflow posts, you can refer following:

How to extract this specific substring in SQL Server?

Upvotes: 0

Related Questions