I need to get SQL date from 2018-11-01 to nov-18.
Asked
Active
Viewed 177 times
-6
-
2Who upvotes these questions? – Salman A Feb 22 '19 at 13:41
-
1Personally, I'd worry about the display format in your presentation layer. Formatting a date means changing it from a `date(time)` data type to a `varchar`; which brings its own set of problems. – Thom A Feb 22 '19 at 13:44
1 Answers
4
Use format()
select format(cast('2018-11-01' as date),'MMM-yy', 'en-US')
OUTPUT:
dateval
Nov-18

Fahmi
- 37,315
- 5
- 22
- 31
-
I am getting error like this when I tried using this "Argument data type nvarchar(max) is invalid for argument 1 of format "function. – dattu chowdary bolla Feb 22 '19 at 13:31
-
-
1`format(cast('2018-11-01' as date),'MMM-yy' ,'en-US')` would be safer in that it will return the specified results irrespective of the language of the login that runs it – Martin Smith Feb 22 '19 at 13:35
-
1
-
1Wait, you're storing dates as a `varchar(MAX)` @dattuchowdarybolla ? Why??? – Thom A Feb 22 '19 at 13:40
-
I got the answer, I converted the date into datetime and later formatted. – dattu chowdary bolla Feb 22 '19 at 13:41
-
1@Larnu - even worse. `nvarchar(max)` so it will take double the space (despite never having a non ASCII character) – Martin Smith Feb 22 '19 at 13:49
-