Solved

How do i cast date format in Sql Server ?

Posted on 2016-11-03
9
78 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 66

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

717 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