Solved

How to store a date field in format mm/dd/yyyy into a SQL Server 2008 table?

Posted on 2016-10-10
4
24 Views
Last Modified: 2016-10-10
I am using SQL Server 2008.

If I pass a date value in format "mm/dd/yyyy" as a parameter into a date field titled DateInput within a table named tblDate, is there a way to store this value in this format into this SQL Server table?
0
Comment
Question by:zimmer9
  • 2
4 Comments
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41837299
If you store the value in a column of data type string then yes. If you store it as a Date, DateTime, or DateTime2 data type then no. However you can format the data on the way out. Your SELECT query would look something like this:
SELECT
  CONVERT(VARCHAR, DateInput, 103) as [DateInput]
FROM
  tblDate

Open in new window

0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 41837304
>is there a way to store this value in this format into this SQL Server table?
No.  Dates are stored as a double number, and not in any specific cosmetic format such as mm/dd/yyyy.

I also wouldn't recommend storing dates as char/varchar's as well, as doing so would reflect character sorting and not date sorting, and you would still have to convert it back to a date format in order to do date specific functions such as DATEADD, DATEPART, DATEDIFF.

>table named tblDate
Tell us more about this tblDate.  I have my own tblDate article you can read called SQL Server Calendar Table, and what you're looking for may be in there.
0
 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 250 total points
ID: 41837307
Be warned, using a CONVERT statement in a SQL query can reduce performance. This is not something you should normally be doing in SQL. Conversions like this should be in the business logic layer of your application. However, if you need it then the SQL query provided will give you a formatted string as you desire.
0
 

Author Comment

by:zimmer9
ID: 41837322
I think you meant 101 but I understand. Thanks.  :)
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now