Solved

Convert column to int

Posted on 2016-11-08
13
28 Views
Last Modified: 2016-11-08
Hello,

Is there a way i can convert column1 to int
SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX ([' +  @ColumnName1 + ']'  + '  ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'  

Open in new window


to get query like :

select  max (cast(ref as int)) as ref from table1 order by ref

Open in new window



Cheers
0
Comment
Question by:RIAS
  • 6
  • 5
13 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41878629
I'm not sure if I understood but if you want to add the CAST function the just add the keyword:
SELECT  CASE 
                       WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
			ELSE MAX (CAST[' +  @ColumnName1 + '] AS INT) END Output FROM  [dbo].[' + @tableName + ']'  

Open in new window

Btw, I'm assuming this is a dynamic SQL otherwise you can't do this.
0
 

Author Comment

by:RIAS
ID: 41878638
Hello Vitor,

This a stored procedure ,your query generated syntax error .

The stored procedure is as bellow

	  DECLARE @SQL VARCHAR(1000) = null


SET @SQL = '
	
		SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX ( [' +  @ColumnName1 + ']'  + '  ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'  

			  
EXECUTE(@SQL)
		

Open in new window

0
 

Author Comment

by:RIAS
ID: 41878640
The problem with the original Sp query is that it does not give correct max (ref)  ,need to vconvert it to integer.

Cheers
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41878644
This a stored procedure ,your query generated syntax error .
Can you post the error?
0
 

Author Comment

by:RIAS
ID: 41878647
	  DECLARE @SQL VARCHAR(1000) = null


SET @SQL = '
	
		SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX (CAST[' +  @ColumnName1 + '] AS INT)'  + '  ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'  

			  
EXECUTE(@SQL)
		

Open in new window

0
 

Author Comment

by:RIAS
ID: 41878651
Error :

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'Ref'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'Output'.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 18

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 50 total points
ID: 41878652
Try

DECLARE @SQL VARCHAR(MAX) = ''
SET @SQL = '
	
		SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX (CAST[' + @ColumnName1 + '] AS INT)'  + '  ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'  


EXECUTE(@SQL)

Open in new window

1
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41878654
Looks like you had an extra parentheses closing:
SET @SQL = 'SELECT CASE 
		WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
		ELSE MAX (CAST[' +  @ColumnName1 + '] AS INT)
		END Output
FROM  [dbo].[' + @tableName + ']'  

Open in new window

0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 450 total points
ID: 41878655
Sorry, no extra closing but need one to be opened after CAST:
SET @SQL = 'SELECT CASE 
		WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
		ELSE MAX (CAST( [' +  @ColumnName1 + '] AS INT))
		END Output
FROM  [dbo].[' + @tableName + ']'  

Open in new window

1
 

Author Closing Comment

by:RIAS
ID: 41878657
Cheers Vitor, thanks Pawan as well!
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41878665
RIAS, why are you giving points to a solution that doesn't work?
The intention is for future people with similar issue to come here and check immediately the comments that really helped you, so working solutions.
EE's policy doesn't reward efforts (unfortunally).
1
 

Author Comment

by:RIAS
ID: 41878666
The solution was partially correct, but if it for fully working then will do so in future.

Cheers
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now