Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What will be the result of the following statement?
SQL
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = '12/25/2005'
SET @TodaysDate = @TodaysDate + 5


//OPTION
A. 12/25/2010
B. 12/30/2005
C. 04/25/2006
D. An error is generated.


This qns asked me in interview . Ans is (D). But how can i check the out put of this.
Posted
Updated 14-Oct-12 19:59pm
v2

The query you wrote will give conversion error from varchar to int..
remove the single quotes from the date you mentioned if u want the varchar value

SQL
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = 12/25/2005
SET @TodaysDate = @TodaysDate + 5
 
Share this answer
 
Comments
Neetesh Agarwal 15-Oct-12 2:22am    
Thanks for Your Reply. But If I add in correct format like
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = '12/25/2005'
SET @TodaysDate = @TodaysDate + '00/03/2012'
Then its will compile successfully But i want to know how will i check the value of the variable TodaysDate
Execute the sql on sql server query analyser.
Answer is D because you can not add string value and an integer value.
 
Share this answer
 
Comments
Neetesh Agarwal 15-Oct-12 2:22am    
Thanks for Your Reply. But If I add in correct format like
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = '12/25/2005'
SET @TodaysDate = @TodaysDate + '00/03/2012'
Then its will compile successfully But i want to know how will i check the value of the variable TodaysDate
Hi,

open SQL sever then copy this code to query analyzer then execute,

Here you will get the error like.

Conversion failed when converting the varchar value '12/25/2005' to data type int.
 
Share this answer
 
v2
Comments
Neetesh Agarwal 15-Oct-12 2:22am    
Thanks for Your Reply. But If I add in correct format like
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = '12/25/2005'
SET @TodaysDate = @TodaysDate + '00/03/2012'
Then its will compile successfully But i want to know how will i check the value of the variable TodaysDate
Hi,

use print or select statement end of the code.
SQL
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = '12/25/2005'
SET @TodaysDate = @TodaysDate + '00/03/2012'
print @TodaysDate

Now you can see the result of the TodaysDate
 
Share this answer
 
v2
Comments
Neetesh Agarwal 15-Oct-12 2:57am    
Thanks a lot
Hi ,

You can see the result by selecting it...

SQL
DECLARE @TodaysDate VARCHAR(10)
SET @TodaysDate = '12/25/2005'
SET @TodaysDate = @TodaysDate + '00/03/2012'

SELECT @TodaysDate


Thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900