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


extracting column values when a part of the column value is known


extracting column values when a part of the column value is known

Author
Message
t.mounika01
t.mounika01
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 51
hi
How can we extract a column values from database if we know a part of that column value.
For ex: if I have values for a column called statements like this:
(I am good Gal)
(you are a good boy) and so on..
and I want to extract all the statements that have a part called 'good'

what is the best way to query this?
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93611 Visits: 20654
Quick thought, use the LIKE operator
Cool

USE tempdb;
GO

DECLARE @TEST TABLE
(
TEST_ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED
,TEST_STRING VARCHAR(50) NOT NULL
);

INSERT INTO @TEST(TEST_STRING)
VALUES
('I am a bad Gal')
,('I am a good Gal')
,('This is an old horse')
,('This is a good horse')
,('I am a good boy')
,('I am a naugthy boy');

SELECT
*
FROM @TEST T
WHERE T.TEST_STRING LIKE '%good%';



Results
TEST_ID     TEST_STRING
----------- ---------------------
2 I am a good Gal
4 This is a good horse
5 I am a good boy

t.mounika01
t.mounika01
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 51
What to do if we want to replace the word 'good' with 'bad' in all those sentences?
I wrote it like this:
REPLACE(columnname,'good','bad')

Is there any other way to do this?
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)

Group: General Forum Members
Points: 156539 Visits: 11650
Using the REPLACE function is the simplest way to do it. Assuming there aren't any unusual requirements, that's exactly the way I'd do it.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
t.mounika01
t.mounika01
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 51
Thank you all for the help :-)
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95272 Visits: 21195
Note that you might replace strings you don't want them to be replaced.


INSERT INTO @TEST(TEST_STRING)
VALUES
('I am a bad Gal')
,('I am a good Gal')
,('Oh my goodness! There''s no more goodwill')
,('He''s a patient with Osgood–Schlatter disease');

SELECT
*, REPLACE(TEST_STRING, 'good', 'bad')
FROM @TEST T
WHERE T.TEST_STRING LIKE '%good%';




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93611 Visits: 20654
Luis Cazares (8/22/2014)
Note that you might replace strings you don't want them to be replaced.


INSERT INTO @TEST(TEST_STRING)
VALUES
('I am a bad Gal')
,('I am a good Gal')
,('Oh my goodness! There''s no more goodwill')
,('He''s a patient with Osgood–Schlatter disease');

SELECT
*, REPLACE(TEST_STRING, 'good', 'bad')
FROM @TEST T
WHERE T.TEST_STRING LIKE '%good%';



DECLARE @STR VARCHAR(50) = 'That an absolute @£$%'
SELECT REPLACE(REPLACE(REPLACE(REPLACE(@STR,'e @','ly'),'£',' a '),'$','brilliant'),'%',' point!')


Cool
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