SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Casting DateTime
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kjamal
Starting Member

1 Posts

Posted - 07/16/2013 :  09:32:08  Show Profile  Reply with Quote
I have a very old database that has configured Date as a numeric value instead of a DateTime value making it really hard to filter. So in my query I'm trying to cast it into a DateTime value but I'm receiving some errors. Is it possible for someone to offer some modifications to my existing query for it to run error free?

My Query:
SELECT
CAST (DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS [Date]) 'Date',
LEFT(CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS [Time]),8) 'Time',
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (Select CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgID,
Parms
FROM JnlDataSection
WHERE CAST(substring(convert(varchar(50), [Date]), 0, 5) + '-' +
substring(convert(varchar(50), [Date]), 5, 2) + '-' +
substring(convert(varchar(50), [Date]), 7, 2) AS DATETIME)) =
(Date, DATEADD(day,-1, getdate())) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449'
Order By Date DESC;

Msg 4145, Level 15, State 1, Line 21
An expression of non-boolean type specified in a context where a condition is expected, near ')'.

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 07/16/2013 :  10:04:34  Show Profile  Reply with Quote
quote:
Originally posted by kjamal

I have a very old database that has configured Date as a numeric value instead of a DateTime value making it really hard to filter. So in my query I'm trying to cast it into a DateTime value but I'm receiving some errors. Is it possible for someone to offer some modifications to my existing query for it to run error free?

My Query:
SELECT
CAST (DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS [Date]) 'Date',
LEFT(CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS [Time]),8) 'Time',
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (Select CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgID,
Parms
FROM JnlDataSection
WHERE CAST(substring(convert(varchar(50), [Date]), 0, 5) + '-' +
substring(convert(varchar(50), [Date]), 5, 2) + '-' +
substring(convert(varchar(50), [Date]), 7, 2) AS DATETIME)) =
(Date, DATEADD(day,-1, getdate())) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449'
Order By Date DESC;

Msg 4145, Level 15, State 1, Line 21
An expression of non-boolean type specified in a context where a condition is expected, near ')'.

You have brackets mis-aligned. The following will eliminate the syntax errors, but that doesn't mean the logic is correct, or that this is what you should be using.

If you can post a sample of the numeric representation of the data and what you want it to be converted to, someone will be able to offer a simpler way of doing that conversion:
SELECT 
CAST (DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS [Date]) 'Date',
LEFT(CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS [Time]),8) 'Time',
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (
	Select CONVERT(VARCHAR(18),[Date]) [Date], 
		[Object],
		MsgID,
		Parms
		FROM JnlDataSection
	WHERE 
		CAST(substring(convert(varchar(50), [Date]), 0, 5) + '-' +
		substring(convert(varchar(50), [Date]), 5, 2) + '-' +
		substring(convert(varchar(50), [Date]), 7, 2) AS DATETIME) =
		CONVERT(Date, DATEADD(day,-1, getdate()))) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449' 
Order By Date DESC;
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 07/16/2013 :  12:42:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Please give an example of Date column.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000