Adds

Thursday, 26 November 2015

Update Name to Title Case in SQL Server using SQL Query




-- User Define Function
Create Function dbo.[UDF_TITLE_CASE]
(
      @String VARCHAR(MAX)  -- Variable for string
)
RETURNS varchar(MAX)
BEGIN
     Declare @Xml XML
     Declare @ProperCase Varchar(Max)
     Declare @delimiter Varchar(5)
     Set @delimiter=' '
     SET @Xml = cast(('<A>'+replace(@String,@delimiter,'</A><A>')+'</A>') AS XML)

     ;With CTE AS (SELECT A.value('.', 'varchar(max)') as [Column]
      FROM @Xml.nodes('A') AS FN(A) )
      Select @ProperCase =Stuff((Select ' ' + UPPER(LEFT([Column],1))
      + LOWER(SUBSTRING([Column], 2 ,LEN([Column]))) from CTE
      for xml path('') ),1,1,'')

RETURN (@ProperCase)
END
GO
-- For Example :
SELECT dbo.[UDF_PROPER_CASE]([StudentInfo_Name]) as [Student Name]
from [dbo].[Tbl_StudentInformation]
GO

SELECT [StudentInfo_Name] as [Student Name]
from [dbo].[Tbl_StudentInformation]
GO


update [dbo].[Tbl_StudentInformation]
set [StudentInfo_Name]=dbo.[UDF_PROPER_CASE]([StudentInfo_Name])

Wednesday, 25 November 2015

Create a user & grant that user to a specific Database



--Step 1: (create a new user)
create LOGIN hello WITH PASSWORD='foo', CHECK_POLICY = OFF;


-- Step 2:(deny view to any database)
USE master;
GO
DENY VIEW ANY DATABASE TO hello;


 -- step 3 (then authorized the user for that specific database , you have to use the  master by doing use master as below)
USE master;
GO
ALTER AUTHORIZATION ON DATABASE::khsk_db TO hello;
GO