Home is there a better way of concatenating date type
Reply: 0

is there a better way of concatenating date type

user9115
1#
user9115 Published in September 19, 2018, 11:30 am

I have Date Type colum with data looking like this:

2011-07-01 00:00:00.000

And i want to recive from that something like

2011-7-1

there is my way, but probably could be done better

SELECT CAST(YEAR(ModifiedDate) AS varchar(15)) + '-' + CAST(MONTH(ModifiedDate) AS varchar(15)) + '-' + CAST(DAY(ModifiedDate) AS varchar(15))
FROM Sales.SalesOrderDetail
share|improve this question
  • What version of SQL Server? From 2012 onwards, FORMAT is available, which makes this (almost) trivial. – Jeroen Mostert Apr 16 at 14:10
  • do you have to have hypens? this will be slashes SELECT CONVERT(char(10), GetDate(),126) – MichaelEvanchik Apr 16 at 14:12
  • I'm working on 2014, thanks for pointing that. – Philipchuck Apr 16 at 14:13
  • 1
    Cast to date instead of trying to manipulate localized strings. Why create a string when you want a date ? – Panagiotis Kanavos Apr 16 at 14:14
  • 4
    Why not leave it to the client to do the formating in whatever way it sees fit? Just return the date type directly. – Alejandro Apr 16 at 14:14

3 Answers 3

active oldest votes
up vote 1 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 4.366031 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO