Solved

How do i cast date format in Sql Server ?

Posted on 2016-11-03
9
69 Views
Last Modified: 2016-11-03
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
Comment
Question by:karthik80c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41872144
For inserts the best thing is using the format YYYYMMDD. So in your case '20161109'.
You don't need the time, do you?
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 41872168
>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
 

Author Comment

by:karthik80c
ID: 41872270
Hi Vitor Montalvão,

Yes i need the time too . what should be the time format i should go for
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:karthik80c
ID: 41872277
Yeah sure Jim I will try with that
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 41872286
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
 

Author Comment

by:karthik80c
ID: 41872294
Thanks Vitro . You are the Man of SQL Buddy
0
 

Author Closing Comment

by:karthik80c
ID: 41872304
Got the Answers Thanks To Experts
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41872306
Best is to use 24-hour clock:

'20161109 23:00'

It's shorter and 100% unambiguous.
3
 

Author Comment

by:karthik80c
ID: 41872312
Thanks Scoot
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question