Eric Klaus
Eric Klaus

Reputation: 955

Converting smalldatetime datatype to varchar datatype

How to convert smalldatetime to varchar? I've tried everything from http://msdn.microsoft.com/en-us/library/ms187928.aspx, but it didn't work.

I want to convert smalldatetime into varchar, because I want to use it in select like this:

select 'Some text'+@Date

thanks in advance

Upvotes: 7

Views: 17636

Answers (4)

CyberBlock
CyberBlock

Reputation: 1

Prueba con esto

DECLARE @FechaDesde datetime2
SET @FechaDesde = ''
SELECT  LEFT(CONVERT(VARCHAR, @FechaDesde, 120), 10)
print @FechaDesde
---FECHA HASTA
DECLARE @FechaHasta datetime2
SET @FechaHasta = ''
SELECT  LEFT(CONVERT(VARCHAR, @FechaHasta, 120), 10)
print @FechaHasta
SELECT *  from compras where fec_emis between @FechaDesde and @FechaHasta

Upvotes: 0

t-clausen.dk
t-clausen.dk

Reputation: 44336

'121' is the format of the date in this case 'yyyy-mm-dd hh:mi:ss.mmm(24h)', char(16) is the number of characters you wish to include, first 16 in this case.

select 'Some text'+convert(char(16), @date, 121)

Cast and Convert

Upvotes: 10

anbuj
anbuj

Reputation: 519

SELECT CONVERT(VARCHAR(20), YourDateColumn, 103) as NewColumnName

here 103 make the date format as dd/mm/yyyy if you want mm/dd/yyyy, you have to use 100

Upvotes: 2

Jodrell
Jodrell

Reputation: 35726

Here is a more up to date link.

The expression

'Some text ' + CONVERT(VarChar(20), [Data])

works fine, fiddle here, what style did you want?

Upvotes: 0

Related Questions