Ruan du Preez
Ruan du Preez

Reputation: 99

CAST Date and Time in SQL Server

In my Data Table I have a Date Column Format = yyyy/mm/dd and a Time column Format hh:mm:ss. I am trying to concat the two so I can use it in a calendar. I keep getting an error. Here is my qry: CAST(T0.[Date]) AS Date) + CAST(T0.[Time]) AS Time(7))

Where am I going wrong?

Upvotes: 1

Views: 10855

Answers (3)

cyan
cyan

Reputation: 747

probably the bracket near the [date] and [Time]

CAST(T0.[Date]) AS Date) + CAST(T0.[Time]) AS Time(7))

change to:

CAST(T0.[Date] AS Date) + CAST(T0.[Time] AS Time(7))

UPDATED: The data types datetime and time are incompatible in the add operator.

select cast(CAST(T0.[Date] AS date)as nvarchar(8)) + cast(CAST(T0.[Time] AS time(7)) as nvarchar(8))

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269613

The conversion error may be a formatting error. Use convert explicitly with a format:

select convert(datetime, t0.[DATE], 111) + CAST(T0.[Time]) AS Time)

There is the possibility that some strings do not match the format that you think they have.

Upvotes: 2

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

Try this

CAST(T0.[Date] AS SmallDateTime) + CAST(T0.[Time] AS Time)

Upvotes: 2

Related Questions