Date format

Change format f the date

create table #table1 (coldate date)
insert #table1 values ('19000101'),('19000202'),(getdate())

declare @sql varchar(2000)

set @sql = 'select iif(coldate < ''19000102'','''',convert(char(10), coldate, 101)) coldate from #table1'

exec (@sql)

Open in new window


This  gives me an American format date is it possible to have date like DD-MM-YYYY

Cheers
RIASAsked:
Who is Participating?
 
Rich WeisslerProfessional Troublemaker^h^h^h^h^hshooterCommented:
0
 
RIASAuthor Commented:
Thanks Rich,
Will try and brb
0
 
RIASAuthor Commented:
Thanks mate, worked like a charm!
0
 
Mark WillsTopic AdvisorCommented:
I know it has been answered, just posting for completion....

Yes, STYLE code : https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql#date-and-time-styles

Note that 103 gives you slashes whereas 105 gives you hyphens

And most flexible is the format function : https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql

Heres an example
declare @coldate date = getdate()

select format(@coldate,'dd-MM-yyyy')

select format(@coldate,'d','en-gb')

select convert(char(10), @coldate, 105)

select convert(char(10), @coldate, 103)

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.