3

Basically I'd like to get this command to work:

$sql = "SELECT (EntryDate + TotalTime) as DataLine FROM TimeSheet WHERE EmployeeID='AA01'";

EntryDate is in the database as a text, but TotalTime is a Number. I need to cast TotalTime as a text, because I've noticed if I combine two differing types of values, it just blanks out the output.

I know I'm supposed to use CAST(TotalTime as XXX) but I'm not sure what I can legally cast it to (char/nchar doesn't seem to work, neither does string nor text). I always get an error of the form...

Syntax error (missing operator) in query expression '(EntryDate + CAST(TotalTime as string) as DataLine FROM TimeSheet WHERE EmployeeID='AA01''

Could I get some help? Thank you!

EDIT I would like to note this isn't intended to add together the values of EntryDate and TotalTime together to produce a single value. I simply want it to give me the EntryDate value as well as the TotalTime value combined into a single line that would read something like:

"10/31/12 9.25"

EDIT AGAIN I'm sorry for not specifying before, I'm using MSSQL

1
  • check my post below, i edited it if you are using mysql Commented Nov 8, 2012 at 4:20

2 Answers 2

8

Try this instead:

SELECT 
  CAST(EntryDate AS VARCHAR(25)) + CAST(TotalTime AS VARCHAR(10)) as DataLine 
FROM TimeSheet 
WHERE EmployeeID = 'AA01';

However, if entrydate is a date and the int totaltime is a time, it may be better to consider converting them as a datetime object by adding them as a date part to the time part depending on the RDBMS you are using. And if possible use a DATETIME datatype to represent both date and time parts instead of two parts. Like so:

SELECT DATEADD(hh, totaltime, entrydate)
FROM TimeSheet
WHERE EmployeeID = 'AA01';

SQL Fiddle Demo

Sign up to request clarification or add additional context in comments.

4 Comments

no need to convert EntryDate to varchar, i think TS said, it is already a text datatype
@hoser What is the type of entrydate? and what RDBMS you are using MSSQL or mysql ,..?
EntryDate is type Text. And I'm using MSSQL
@Hoser - It should work fine. Try to use ADDDATE function. See my edit.
0

MSSQL:

$sql = "SELECT EntryDate+''+CAST(TotalTime as char(25)) as DataLine FROM TimeSheet WHERE EmployeeID='AA01'";

MySQL

$sql = "SELECT CONCAT(EntryDate,' ',CONVERT(TotalTime,char(25))) as DataLine FROM TimeSheet WHERE EmployeeID='AA01'";

2 Comments

are you using MySQL or MSSQL?
It still doesn't work, giving me the same 'missing operator' error unfortunately. I tried using the MySQL version just for kicks but it told me CONCAT isn't a valid method, which I figured would happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.