table with a Date column in DD/MM/YYYY format - sql-server

I have an excel source with a date column in dd/mm/yyyy format. I need to load a table with data from this source in exactly the same format. using just DATE data type gives me output of yyyy-mm-dd. So how do I store it in dd/mm/yyyy format?

Don't worry about how the data is stored. Let the native DATE type do its thing. Use the CONVERT function to format your data for display purposes.
/* 103 = dd/mm/yyyy */
SELECT CONVERT(CHAR(10), YourDateColumn, 103) AS formatted_date
FROM YourTable;

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

File Date column – is yyyymmdd but should it be dd/mm/ccyy

How can I get in SQL server a file date in the following format: dd/mm/ccyy
I have this, but this creates yyyymmdd
CONVERT (char(8),Getdate(),112) AS [File date]
how can I get dd/mm/ccyy ?
select CONVERT (char,Getdate(),103)
A useful reference is date formats: date format cheatsheet

How to create query with format date DD-MM-YYYY

I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php

Convert datetime format to ISO8601 format in sqlserver 2008

I'm having a table IMAGEDATA in which the column VALUE stores datetime format in en-US (for ex. 06/08/2012 02:10:36 p.m.). I need to convert the datetime format to ISO8601 format (ex. 2012-08-22T13:10:39) for all the rows that are currently in the table. I'm novice in these topics. Could you please me some better approach it will be more helpful.Thanks.
Internally Datetime values are stored as 2 integers . They are not stored in the specific format in which they are inserted .In order to convert to 8601 format try the below code
Declare #date datetime
set #date = '06/08/2012 02:10:36 '
Select convert(varchar(30),#date,126)

Sql server getdate() convert problem

Hey I have time column and datatype date.. default value is getdate() but this return format like 2011-04-24
but I want like 24.04.2011 How can I convert that format?
what is the value format?
Getdate() is a datetime. That has no specific format. If you want a specific format you must convert it into a varchar.
select convert(varchar(10),getdate(),104)
There's more info on the different conversion codes here.
An SQL Server date column does not have an associated format.
You can specify a format when converting a date to a varchar column. See the MSDN page for convert. From MSDN, 104 is mm.dd.yyyy, so you could:
select convert(varchar(12),getdate(),104)
This prints 24.04.2011.

Resources