7

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

4 Answers 4

10

'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

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

Comments

2

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

Comments

0

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?

Comments

0

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

Comments

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.