Question

I am executing this query in SQL Server 2008

 SELECT (CONVERT(DATE, GETDATE()))

and it shows the result 2013-07-22.

I need to print this result as 22713, where 22 is the date 7 is the month 13 is the year.

How could I do this?

Was it helpful?

Solution

SELECT CAST(DATEPART(dd,GETDATE()) as varchar(10))
  +CAST(DATEPART(mm,GETDATE()) as varchar(10))
  +RIGHT(CAST(DATEPART(YY,GETDATE()) as varchar(10)),2)

SQLFiddle demo

OTHER TIPS

1) It bad practice use SQL for string operation. More right external tools 2) I use other RDBMS. Below query work with it:

select (extract(day from ua.stamp))||(extract(month from
ua.stamp))||(extract(year from ua.stamp)) from useractions ua

Furthemote, this link can help you: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/55d23bb0-6e1e-4a03-9bed-94e7c755ec4d/get-the-day-from-date-value-in-sql-server

You could read the string, go over it with a tokenizer, split it and put it back together the way you want it to be. What language are you using?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top