Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am new to sql sever i worte a query like this
create table studentinfo (id int,
xx int,
yy varchar(30),
zz int,
ww int,
uu datetime,
vv int,
ss varchar(30),
TT varchar(30),
kk int)

select xx,yy,zz,ww,uu,vv,ss,TT,'' as d1,'' as d2,''as d3,''as d4 from studentinfo where id='1'and kk<='0'
UNION
select XX,yy,zz,ww,''asd5,''as d6,'' as d7,''as d8,uu,vv,ss,TT from Studentinfo where id='1'and
kk >'0'

this is shows o/p like this :
xx yy zz ww d5 d6 d7 d8 d1 d2 d3 d4
1 jh 200 15 2013/01/1 2 ok fail 1900-01-01-00:00:00:00 0
1 jh 200 15 1900-01-01-00:00:00:00 0 2013/01/1 2 ok fail
1 jh 200 15 1900-01-01-00:00:00:00 0 2013/01/1 2 ok fail

i want to remove 1900-01-01-00:00:00:00 0 from that output .in table my table i am not inserted .but i was coming like that .hepl me how to remove that one
Posted

1 solution

In your union you have fields like this:
xx,yy,zz,ww,uu,... and xx yy zz ww d5,... respectively.
Now: uu is of type datetime. But for d5 you select '' (empty string). Because of the union the empty string is treated as zero, and as datetime respectively, thus you get the minimum value of datetime: 1900-01-01-00:00:00:00.
Is is working as expected.
You have two choices:
A) select NULL as d5 instead of '' as d5, and handle null value at presentation time (will be empty string by default in most cases).
B) instead of uu, use convert[^] or cast, like this for example: CONVERT(VARCHAR(24),uu), and you can keep the empty string, since both will be strings. Be aware, that in sql server below version 2012 there are only few built-in datetime formats you can choose from.
 
Share this answer
 
v2
Comments
Member 9846414 9-May-13 3:23am    
thanks for helping me
Zoltán Zörgő 9-May-13 3:38am    
If you find my answer helpful, feel free to accept it.
Member 9846414 13-May-13 3:26am    
thank u very much

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