Tuesday, 6 September 2011

Update Specific Field Value in SQL Server

How to Update Some Specific Value IN Sql Server

UPDATE tblLocation SET LocationName = REPLACE(LocationName,'hall','Hall');
it can easily understand by Below example
lets my table name is Party
id  PartyName                       active
1    Rohit                                   1
2    Dealer Rohit                              1
3    Owner                                1
4    Builder Rohit                              1
-----------------------------
in above we want to update partyname field value where ROHIT replace to Riya then query is

UPDATE Party  SET PartyName= REPLACE(PartyName,'Rohit','Riya');
---------------------------------------------------------------------------------------
2..Another Query
How to Update First Word Is Always Capital  in SQL Server..
To Acompolish this we create a function and call this like below

 CREATE FUNCTION [dbo].[properCase](@string varchar(8000)) RETURNS varchar(8000) AS
BEGIN 
    SET @string = LOWER(@string)
    DECLARE @i INT
    SET @i = ASCII('a')
    WHILE @i <= ASCII('z')
    BEGIN
        SET @string = REPLACE( @string, ' ' + CHAR(@i), ' ' + CHAR(@i-32))
        SET @i = @i + 1
    END
    SET @string = CHAR(ASCII(LEFT(@string, 1))-32) + RIGHT(@string, LEN(@string)-1)
    RETURN @string
END

 And Call this function like this  in ur Query

 UPDATE urTable SET name=[dbo].[properCase](name)

this set first letter of ur word is capital and other is Small , According to Ur Requirement

Hop This will help u
Rohit Srivastava

No comments:

Post a Comment