SQL Server - convert not working on varchar data type

Asked By Jag
07-Apr-10 08:48 PM
I am trying to do a comparison on when a report last ran using getdate()-180 which is working, but when I try to convert the field in the database nothing changes.

SELECT

 

ObjName as "Report Name"
,SI_RECURRING as "Recurring Status"
,LastModifyTime as "Last Run"
,ScheduleStatus as "Schedule Status"
,convert(varchar, getdate()-360/2, 101) as "Cut Off Date"
FROM CMS_InfoObjects3
WHERE convert(VARCHAR, LastModifyTime,114) > convert(varchar, getdate()-360/2, 114)
AND ObjName like '%.rpt'
GROUP BY ObjName, LastModifyTime, ScheduleStatus, SI_RECURRING
order byObjName, LastModifyTime desc;

convert(VARCHAR, LastModifyTime,114) does not seem to be converting to the same format as the getdate convert.

 

  Venkat K replied to Jag
07-Apr-10 09:23 PM
The 114 option of CONVERT Function will convert the souce expression or DateTime to below mentioned format:

14 114 - hh:mi:ss:mmm(24h)

SELECT

 

CONVERT (NVARCHAR,GETDATE(),114) -- This will convert the datetime to time 24 hr format.
SELECT CONVERT (NVARCHAR,'06:49:02.810',114)  -- This will give the same result what value we have given in expression option.

If yourLastModifyTime column has a time value then the output will be the same result as the column has.

Thanks

 

  Jag replied to Venkat K
07-Apr-10 10:17 PM
Hi Venkat the field name indicates that it is a date time, but it is not it is data type varchar(30) the data looks like.

2008 04 01 04 52 38 398 which I am assuming is yyyy mm dd hh mm dd ttt

and no matter how I try to format (convert) the field I get no response it stays the same.

I am using SQL server 8.0 and the collation is SQL_Latin1_General_CP1_CI_AS

Ideally it would be nice if I could get it to format to 101 or dd/mm/yyyy

thanks for your assistance.
  Web Star replied to Jag
07-Apr-10 11:31 PM
u don't need to date compairing after convert it into varchar , event varchar comparing chatacter not date
u just change your where condtion as follows this will work perfectly
WHERE LastModifyTime > (getdate()-360/2)
  Jag replied to Web Star
07-Apr-10 11:40 PM
Sorry Web Star

 

 

 

SELECT</FONT></FONT><FONT size=2> ObjName </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>as</FONT></FONT><FONT size=2> "Report 
Name"<BR></FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT size=2>SI_RECURRING </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>as</FONT></FONT><FONT size=2> "Recurring 
Status"<BR></FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT size=2>LastModifyTime </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>as</FONT></FONT><FONT size=2> "Last Modified 
Date"<BR></FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT size=2>ScheduleStatus </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>as</FONT></FONT><FONT size=2> "Schedule 
Status"<BR></FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT color=#ff00ff size=2><FONT color=#ff00ff 
size=2>getdate</FONT></FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>()-</FONT></FONT><FONT size=2>180 </FONT><FONT color=#0000ff size=2><FONT 
color=#0000ff size=2>as</FONT></FONT><FONT size=2> "Cut Off 
Date"<BR></FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>FROM</FONT></FONT><FONT size=2> CMS_InfoObjects3<BR></FONT><FONT 
color=#0000ff size=2><FONT color=#0000ff size=2>WHERE</FONT></FONT><FONT size=2> 
LastModifyTime </FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>></FONT></FONT><FONT size=2> </FONT><FONT color=#808080 size=2><FONT 
color=#808080 size=2>(</FONT></FONT><FONT color=#ff00ff size=2><FONT 
color=#ff00ff size=2>getdate</FONT></FONT><FONT color=#808080 size=2><FONT 
color=#808080 size=2>()-</FONT></FONT><FONT size=2>180</FONT><FONT color=#808080 
size=2><FONT color=#808080 size=2>)<BR></FONT></FONT><FONT color=#808080 
size=2><FONT color=#808080 size=2>AND</FONT></FONT><FONT size=2> ObjName 
</FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>like</FONT></FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2><FONT 
color=#ff0000 size=2>'%.rpt'<BR></FONT></FONT><FONT color=#0000ff size=2><FONT 
color=#0000ff size=2>GROUP</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>BY</FONT></FONT><FONT size=2> 
ObjName</FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT size=2> LastModifyTime</FONT><FONT color=#808080 
size=2><FONT color=#808080 size=2>,</FONT></FONT><FONT size=2> 
ScheduleStatus</FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT size=2> SI_RECURRING<BR></FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>order</FONT></FONT><FONT size=2> </FONT><FONT 
color=#0000ff size=2><FONT color=#0000ff size=2>by</FONT></FONT><FONT size=2> 
ObjName</FONT><FONT color=#808080 size=2><FONT color=#808080 
size=2>,</FONT></FONT><FONT size=2> LastModifyTime </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>desc</FONT></FONT><FONT color=#808080 
size=2><FONT color=#808080 size=2>;</FONT></FONT>


Gives me Syntax error converting datetime from character string.

 

  Jag replied to Jag
07-Apr-10 11:45 PM

Something went wrong with my reply....

SELECT

 

ObjName as "Report Name"
,SI_RECURRING as "Recurring Status"
,LastModifyTime as "Last Modified Date"
,ScheduleStatus as "Schedule Status"
,getdate()-180 as "Cut Off Date"
FROM CMS_InfoObjects3
WHERE LastModifyTime > (getdate()-180)
AND ObjName like '%.rpt'
GROUP BY ObjName, LastModifyTime, ScheduleStatus, SI_RECURRING
order by ObjName, LastModifyTime desc;

This results in an error:  Syntax error converting datetime from character string.

 

  Web Star replied to Jag
08-Apr-10 12:33 AM
Means in your table date are stored in varchar datatype column, it column is datatime than above query works if varchar than u need to convert it into datetime not in varchar as
WHERE Convert(Datetime, LastModifyTime) > (getdate()-180)
hope this helps
  Jag replied to Web Star
08-Apr-10 12:44 AM
Still getting this error

Syntax error converting datetime from character string.

I am not sure if there is a problem with the actual data having gaps between the characters.

yyyy mm dd hh mm ss ttt

normally the getdate is formatted yyyy-mm-dd hh:mm:ss,ttt

Would this be causing an issue with the convert process as nothing I am trying is working.

:-(

Create New Account
help
SSIS package SQL Server hi friends, SSIS package create in Sql server 2008 odbc mysql to ado.net sql server how to create and job scheduled in sql server 2008 please give the solution, thanks. Please do some reading on MSDN or several SQL
Column of type [int]: can I tell whether all its digits are the sa SQL Server . . using SQL Thanks. SQL Server Programming Discussions SQL Server 2005 (1) SQL Server 2000 (1) SQL Server (1) User defined function (1) CREATE TABLE (1) Declare (1
Exporting SQL Server to Access SQL Server Is there a tutorial on the easiest way to export an SQL Server database's tables into and Access database (all tables without having to do them one at a time) in a package that runs daily? Thanks. SQL Server DTS Discussions SQL Server (1) Database (1) Windows (1) Wang (1) Msdnmg (1) Day (1
SQL Server 2005: Job resheduling SQL Server Hi Let's assume there is a Sql Server database with contains a stored procedure. Sql Server Agent has a job, which runs this stored procedure. The shedule for this job is
Change data in a table in a seperate database on a seperate server SQL Server I am very new at SQL Server, and want to make a trigger on a table that will update / write data to another table that is in a different database on a different server. Is this possible? If so, how would you do it? SQL Server Discussions SQL Server 2008 (1) SQL Server (1) Stored procedure (1) Describe (1) Trigger (1