Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 102
  • Last Modified:

How do i cast date format in Sql Server ?

Hi Experts ,

Am trying to insert the date filed value as
09-11-2016 11:00 PM
and sql server is throwing error as Out of range exception . How do i cast the date as (dd-mm-yyyy) format .
0
karthik80c
Asked:
karthik80c
2 Solutions
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
For inserts the best thing is using the format YYYYMMDD. So in your case '20161109'.
You don't need the time, do you?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>How do i cast the date as (dd-mm-yyyy) format .
Just to clarify, dates are not stored in a specific format, so the storage answer is you don't convert (so Vitor's answer above works).  

If you wish to display the date in dd-mmm-yy format, then use convert(varchar, your_data_here ,5).   For a handy reference check out SQL Server Date Styles (formats) using CONVERT()
2
 
karthik80cAuthor Commented:
Hi Vitor Montalvão,

Yes i need the time too . what should be the time format i should go for
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
karthik80cAuthor Commented:
Yeah sure Jim I will try with that
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Yes i need the time too . what should be the time format i should go for
Just add the time to the date separating them with a space. Example: '20161109 11:00PM'.
1
 
karthik80cAuthor Commented:
Thanks Vitro . You are the Man of SQL Buddy
0
 
karthik80cAuthor Commented:
Got the Answers Thanks To Experts
0
 
Scott PletcherSenior DBACommented:
Best is to use 24-hour clock:

'20161109 23:00'

It's shorter and 100% unambiguous.
3
 
karthik80cAuthor Commented:
Thanks Scoot
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now