Loading...

Follow Madhivanan's T-SQL blog on Feedspot

Continue with Google
Continue with Facebook
or

Valid

I have created my own blog with custom url and moved all my existing post there.

Please visit https://exploresql.com/ hereafter.

Also please subscribe to my new blog if you want to get notified on the new posts

Thank you all.

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Sometimes you may need to find out the fill rate of each column to know the accuracy of given data. One of the things to know is how many columns do not have any values at all.

Let us create the following data set

use tempdb 
GO
create table test
(
	customer_id int, 
	customer_name varchar(100), 
	dob date, 
	address1 varchar(100),
	address2 varchar(100)
)

insert into test(customer_id,customer_name, dob)
select 10001,'Madhivanan','1990-01-01' union all
select 10002,'Murugan','1991-10-19' union all
select 10003,'Kandhan','1984-04-22'

Now see the result set


select * from test;


As you see, all the values of the columns address1 and address2 are NULL.

You can execute the following dynamic SQL to get the column names for which all values are NULL

declare @sql varchar(8000), @create varchar(1000),@insert varchar(100)

select 
	@sql='',
	@create='Create table #test (col_value sql_variant, col_name varchar(100)) ', 
	@insert=' insert into #test(col_value,col_name) '


select @sql=@sql+' '+@insert+
	' select min('+column_name+') as '+column_name+ ','''+column_name+
		''' from '+table_name 
	from information_schema.COLUMNS
where 
	TABLE_NAME='test'  and table_catalog='tempdb'

set @sql=@create+' '+ @sql+ ' select * from #test where col_value is null'
exec(@sql)

After execution, the result is

It listed out two columns address1 and address1 which have all values as NULL. This code can be modified to handle empty calues or any other values easily

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

There was a question on counting number of words in a sentence by my friend

One simple method is use while loop as shown below

Declare @s varchar(100)
set @s='  See      how many                        words this      has  '
set @s=ltrim(rtrim(@s))

while charindex('  ',@s)>0
Begin
	set @s=replace(@s,'  ',' ')
end

select len(@s)-len(replace(@s,' ',''))+1 as word_count

The result is 6

If the sentence has multiple consecutive spaces, replace them with single space until no more multiple spaces exist

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

When you use sql_variant datatype in sql server 2000, and add data to a table
using union all, it will work though datatype of data differs from each other


declare @test table(variant_column sql_variant)
insert into @test
select 'test' union all
select 345 union all
select getdate() union all
select '3245.23'
select variant_column from @test

But in sql server 2005, it will throw error for datatype mismatch.

The proper way of doing this is to explicitely cast any one of the values to be of sql_variant datatype

declare @test table(variant_column sql_variant)
insert into @test
select cast('test' as sql_variant) union all
select 345 union all
select getdate() union all
select '3245.23'
select variant_column from @test

This is one of the behavioural changes itroduced in sql server 2005 and is followed in all the succeeding versions
 

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Only one out of the following four SELECT queries will throw an error. What is it?

SELECT 5number
SELECT 5.number
SELECT 5[number]
SELECT 5..number
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview