Solved

Convert column to int

Posted on 2016-11-08
13
52 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 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

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 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar 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 48

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 48

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 48

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

791 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