Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Cast/Convert....?

Author  Topic 

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 09:21:48
Hi I have a table:

Create table
( max_value nvarchar(50))

Now I want to input all the max values within a particular column to this table.

i.e insert into table
select Max(column) from Table1

my code runs it in a loop so it will put all max columns of a table into this field.

My problem is I can have varying column types. i.e. Column with varchar, column with numeric, column with money.

What can i assign the column Max_Value in my table to handle all of these different datatypes??

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-22 : 09:24:15
Don't be afraid to tell us what you really want to achieve.

Looping?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-22 : 09:24:57
sql_variant?

Or have one column for each datatype you want to store the MAX value for

Kristen
Go to Top of Page

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 09:48:07
Apologies, My problem is that the Max_value varchar(400) cannot hold a money type column.

error: Msg 260, Level 16, State 1, Line 2
Disallowed implicit conversion from data type money to data type nvarchar, table 'DNF_db.dbo.Table_Audit', column 'Min_value'. Use the CONVERT function to run this query.

Script:


declare @SqlString varchar(8000),
@Column_name varchar(150),
@Counter numeric,
@Max numeric,
@Total numeric

--Create Audit Table if not exists
If Not Exists (Select NAME From Sysobjects WHERE NAME = 'Table_Audit')
Begin
Create Table Table_Audit
(
Table_Name varchar(200),
Total_Count numeric,
Column_Name varchar(200),
Blank_Quantity int,
Percentage_Blank float,
Distinct_Count int,
Percentage_Distinct float,
Min_Length int,
Max_Length int,
Min_value nvarchar(400),
Max_Value nvarchar(400),
Nulls int,
Date_run datetime
)
end

--truncate table...
truncate table Table_Audit

set @Total = (select count(*) from Supplier_Table)
set @Max = (select count(*) from information_schema.columns where table_name = 'Supplier_Table')

set @Counter = 1

while @Counter <= @Max
Begin
set @Column_name = (select Column_name from information_schema.columns where table_name = 'Supplier_Table' and Ordinal_position = @Counter)
set @Sqlstring = '
select ''Supplier_Table''as Table_Name,
count(*) as Total_Count,
''' +@Column_Name+ '''as Column_Name,
sum(case when cast([' +@Column_Name+ '] as varchar) is not null and cast([' +@Column_Name+ '] as varchar) <> '''' then 0 else 1 end) as Blank,
left(cast(sum(case when cast([' +@Column_Name+ '] as varchar) is not null and cast([' +@Column_Name+ '] as varchar) <> '''' then 0 else 1 end) as numeric)
/ ' + cast(@Total as varchar) +' * 100,5) as Percentage_Blank,
count(distinct [' +@Column_Name+ ']) as [Distinct],
left(cast(count(distinct [' +@Column_Name+ ']) as numeric)/' + cast(@Total as varchar) +' * 100,5) as Percentage_Distinct,
min(len(cast([' +@Column_Name+ '] as varchar))) as Min_length,
max(len(cast([' +@Column_Name+ '] as varchar))) as Max_Length,
min([' +@Column_Name+ ']) as Min_Value,
max([' +@Column_Name+ ']) as Max_Value,
sum(case when [' +@Column_Name+ '] is null then 1 else 0 end) as [Nulls],
getdate() as Date_Run
from Supplier_Table'
--print @Sqlstring
insert into Table_Audit
exec (@Sqlstring)
set @Counter = @Counter + 1
End

Go to Top of Page

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 09:49:36
Apologies regarding the formatting of my above script. How can I make this display with the tabs and spaces as my script in sql??

Oh thats a question which can be answered later!

Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-22 : 09:57:43
Put [ code ] and [ /code ] first and last of code. Of course the tags should be without spaces.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-22 : 09:59:30
"How can I make this display with the tabs and spaces as my script in sql??"

Use the "Edit Reply" icon (looks like Paper + Pencil) on that post and put

[code]
...
[/code]

around it

Kristen
Go to Top of Page

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 10:10:58
Thanks I managed to solve the cast problem, by using:

cast(min([' +@Column_Name+ ']) as varchar) as Min_Value,
cast(max([' +@Column_Name+ ']) as varchar) as Max_Value,
Go to Top of Page
   

- Advertisement -