Skip to content Skip to sidebar Skip to footer

Using Cast In Sql With Multiple Column Selections

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,

Solution 1:

Try this instead:

SELECTCAST(EntryDate ASVARCHAR(25)) +CAST(TotalTime ASVARCHAR(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

Solution 2:

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'";

Post a Comment for "Using Cast In Sql With Multiple Column Selections"