Solved

Convert column to int

Posted on 2016-11-08
13
37 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 46

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 46

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 24

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 46

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 46

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 46

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

911 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

22 Experts available now in Live!

Get 1:1 Help Now