Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert column to int

Posted on 2016-11-08
13
Medium Priority
?
68 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
13 Comments
 
LVL 52

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 52

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
 
LVL 32

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 200 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 52

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 52

Accepted Solution

by:
Vitor Montalvão earned 1800 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 52

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

609 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