Showing posts with label SQL 2008. Show all posts
Showing posts with label SQL 2008. Show all posts

Thursday, January 1, 2015

Update SQL Table with joins

Following is the query to update sql table with joins

UPDATE a
SET a.sortorder = b.sort_order
FROM picture AS a
INNER JOIN (select row_number() over (order by id) as sort_order,id,projectid from picture
where projectid = 61
) AS b
       ON a.id = b.id
where a.projectid = 61


select * from picture order by id desc


SQL REPLACE() : Argument data type ntext is invalid for argument 1 of replace function.

SQL Server Replace function does not work with text and ntext data types.
Following query contains replace function for ntext type column.

update description_table set description = replace(description,'../G/','http://styletint.in/G/') where description like '%../G/%'

this query is will return following error:

Argument data type ntext is invalid for argument 1 of replace function.

To resolve this, we can cast ntext type to nvarchar(max) and then update column with this value: Following query will work without any error:

update description_table set description = replace(CAST(description AS NVARCHAR(MAX)),'../G/','http://styletint.in/G/') where description like '%../G/%'


Wednesday, January 16, 2013

SQL Split function


CREATE FUNCTION dbo.Split(@String VARCHAR(8000), @Delimiter CHAR(1)) 
RETURNS @temptable TABLE (items VARCHAR(8000)) 
AS 
BEGIN DECLARE @idx INT 
DECLARE @slice VARCHAR(8000) 
SELECT @idx = 1 
IF LEN(@String)<1 font="font" is="is" nbsp="nbsp" null="null" or="or" return="return" tring="tring">
WHILE @idx!= 0 
BEGIN 
SET @idx = CHARINDEX(@Delimiter,@String) 
IF @idx!=0 
SET @slice = LEFT(@String,@idx - 1) 
ELSE 
SET @slice = @String IF(LEN(@slice)>0) 
INSERT INTO @temptable(Items) VALUES(@slice) 
SET @String = RIGHT(@String,LEN(@String) - @idx) 
IF LEN(@String) = 0 
BREAK 
END 
RETURN 
END 

Saturday, February 25, 2012

SQL Server user can access all databases issue

Following are the steps to resolve this issue.


1. Go to user's properties:






2. Go to user mappings:
    2.1. check the default database for login.
    2.2. check db_owner in database role membership. 













3. click default schema button
    3.1. type dbo and click on.



4. click server roles and confirm uncheck of sysadmin permissions.