Click here to Skip to main content
12,634,107 members (23,430 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL Server
insert into datef
( id , date1 , date2 , name1 )
values
( 1 ,  getdate() , select max(date1)+ 1 from  datef  else 'insert 1st Jan <Current year>' , 'name1')


How can i use the above statment in normal query

OP's additional information moved from non-solution below
I am getting error in this statement while executing pls sort it out
select max(date1)+ 1 from datef else 'insert 1st Jan '
Posted 17-Nov-12 1:07am
lalitkr1.8K
Updated 17-Nov-12 2:40am
Nelek111K
v3
Comments
OriginalGriff 17-Nov-12 6:28am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
That is a normal query, so what are you trying to do that you haven't explained?
Use the "Improve question" widget to edit your question and provide better information.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If date1 is NULL then MAX(date1) is always NULL. You need to use COALESCE function to get non-nullable value. For example:
DECLARE @curdate AS DATETIME

SET @curdate = GETDATE()

SELECT MAX(COALESCE(date1, @curdate)+ 1
FROM YOUR_TABLE_NAME


Depending on MS SQL Server use date and time function[^] corresponding to your needs.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Declare @date As datetime


Select @date=MAX(date1) From datef

If(@date Is Null) Set @date=Convert(datetime, '01-Jan-'+DateName(yyyy,GetDate())) 
Else Set @date=DateAdd(d,1,@date)

insert into datef
( id , date1 , date2 , name1 )
values
( 1 ,  getdate() , @date , 'name1')

  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100