Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting Error

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-14 : 04:02:10
Hi,

I try to run this but failed on converting.
May I know how can I solved this?

declare @test varchar(10)
set @test = 'Convert(date,ModifiedOn) >= 2012-02-13'
select top 10 ModifiedOn from Details
where ModifiedOn = @test

Error:
Conversion failed when converting date and/or time from character string

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-02-14 : 04:28:58
Increase the length

declare @test varchar(50)

What you are trying to do here?



Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-14 : 04:32:23
It isn't possible this way without using dynamic sql.
But Senthil is right. Tell us what you want to do so we maybe can show a better way.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-14 : 08:12:31
I would like to update the table to run certain date.
If is -1 it refers to yesterday date.
How if I would like to run from 1st Feb 2012 to yesterday date.

DECLARE @day varchar(100)
SET @day = '-1'
UPDATE tablename
SET columnname = 'SELECT * FROM tablename WHERE ModifiedDate = @day'
where ....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-14 : 09:45:06
do you mean this?


DECLARE @day int
SET @day = -1

UPDATE t
SET t.columnname1 = value1,t.columnname2= value2,...
from tablename t
where t.ModifiedDate >= DATEADD(dd,DATEDIFF(dd,0,GETDATE())+@Day,0)
and t.ModifiedDate < DATEADD(dd,DATEDIFF(dd,0,GETDATE())+@Day+1,0)

this will work for any value of @day which will regarded as offset from current day

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -