Displaying date format SQL Server - sql

When I try to insert into tableA with
insert into tableA
values (convert(date, '12-1-2012', 105))
then I try to
select * from tableA
it always shows the dates in yyyy-mm-dd format
But when I use
select CONVERT(varchar, thedate, 105) from tableA
then it shows dd-mm-yyyy
Can I make even select * always shows the dd-mm-yyyy format?
Like changing the default display of date format?

The best way to do this would be to make a view, and set one of the fields in the view as:
CONVERT(varchar,thedate,105) AS thedate
and reference your VIEW in your select statement instead of your TABLE name.

Maybe this would help:
How to change Date Format after installing SQL server

Related

Convert Varchar to Date type (not just to run a query, actually changing the column on the table)

This was asked several times but I wasn't able to implement any of the answers.
I have a table with a column with Dates that has been created as VARCHAR, I want to convert it to Date(103).
I've seen several responses with the following:
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]
This will work nicely to convert the result of a query...but I want to convert the actual column not just when I do a select query.
I probably just don't understand where to put this line of SQL code. Can anyone help me out?
There is no such thing as DATE(103); dates are stored using an internal format. I'll assume that is the format of the string.
What to do? Change the string to something that can be implicitly converted to a date. Then change the column:
update t
set datecol = convert(date, datecol, 103);
alter t alter datecol date;
If this returns an error, you need to figure out where the values don't convert properly. That would use:
select datecol
from t
where try_convert(date, datecol, 103) is null and
datecol is not null;
You'll need to figure out how to fix the broken values. And in the process learn one important reason not to store date/time values as strings.

Can I change date format of SQL Server table permanently?

I have 2 tables in my database.
Customers_Details
Transaction
Both tables have a date column. I want to change the date format from YYYY-MM-DD to DD-MM-YYYY permanently for both tables.
Can i do it in one query or do I have to write separate query for each table?
The query which I have written is not changing the format of date permanently.
select
[customer_ID], [Gender], [Area_code],
convert(varchar, dob, 105) as DOB
from
[dbo].[customer_details]
dates are stored in an internal format. You wouldn't want to look at that string of bits anyway. But you do want to store the data that way -- to use date functions, so ordering is correct, and so on.
What you can do is add a computed column that has the string form that you want:
alter table [dbo].[customer_details] add dob_ddmmyyyy as (convert(varchar(10), dob, 105);
You can then use dob_ddmmyyyy in queries.
You can use following method, (tested in SQL 2014 and higher)
select FORMAT (GETDATE(), 'yyyy-MMM-dd')
In your case it would be as follows:
select [customer_ID],[Gender],[Area_code],
format(dob, 'dd-MM-yyyy') as DOB
from [dbo].[customer_details]

How to convert date in mm/dd/yyyy format to yyyy-mm-dd hh:mi:ss.mmm

I'm inserting data into a table and before inserting I need to check if data exists.
I have a composite key consisted of two columns of datetime and int.
Before inserting I need to check if the data with the same time and id exists in the table.
The date that user is inserting is in 'mm/dd/yyyy'.
The datetime data in the table looks like this: '2016-01-12 00:00:00.000'.
The id field is int.
So, I have a query:
if not exists(select count(*) from table_1 where MyDate = #myDate and id = #id)
insert into table_1 .....
What is the right way to format the date user sends to match the datetime format in the table?
Check this sqlfiddle about how to use different date formats in your query. Might help you to solve it.
http://www.sqlfiddle.com/#!2/fd0b7/5
I am guessing that the question is about SQL Server, based on the syntax. The issues in the code snippet far transcend date formats.
First, the expression:
if not exists(select count(*) from table_1 where MyDate = #myDate and id = #id)
will never return true, because the subquery always returns one row with one column. If nothing matches, the column contains 0, which does exist.
You intend:
if not exists(select 1 from table_1 where MyDate = #myDate and id = #id)
Second, this check is not necessary if you wisely choose to have the database enforce the uniqueness constraint. So, define a unique index or constraint on the two columns:
create unique index unq_table_1_id_mydate on table_1(id, MyDate);
Now, the database won't let you insert duplicate values and no if is necessary.
Next, I would suggest that you fix the date format at the application layer. YYYY-MM-DD is an ISO standard date format and quite reasonable. However, if you don't want to do that, use convert():
insert into table_1(id, MyDate, .....)
select #id, convert(datetime, #MyDate, 101), . . .
The value in the database looks to be correct stored as a date/time value, so this should work fine.
You can use following line to convert date to required format in SQL server:
select FORMAT(#your_date, 'yyyy-MM-dd HH:mm:ss', 'en-US') from Your_Table

Convert Datetime values in Varchar column

I have dates stored in a varchar column, I know date values should be stored in a datetime column but it is not my database to change.
When I try and convert the datetime values into another format the output always comes out as 1900-01-01 even though it's stored in the database like 13/04/2012, I want it to be like 2012-04-13.
My code at the moment is like this:-
(SELECT ISDATE(CONVERT(VARCHAR(10), myDate, 120)) FROM myTable
Any ideas as to my it is always outputting 1900-01-01, this is in SQL Server 2008.
Try this
SELECT CONVERT(VARCHAR(10), CONVERT(DATE, myDate, 103), 120) FROM myTable

Convert column with data MM/DD/YYYY varchar to date in sql server?

I've found some similar questions but haven't been able to get anything to work yet. I'm very much a novice with little SQL experience.
I have a column END_DATE as Varchar(10) where all the rows follow the mm/dd/yyyy format and I would like to convert it to date. I have an empty column formatted as date if that helps. There are 36 million rows.
SELECT CONVERT(DATETIME,YourColumn,101) FROM YourTable
101 is mm/dd/yyyy format.
You zany backwards americans :)
To update your existing column
UPDATE YourTable
SET YourNewColumn = CONVERT(DATETIME,YourOldColumn,101)
Since it appears you have invalid data, use this method to isolate it:
UPDATE YourTable
SET YourNewColumn = CONVERT(DATETIME,YourOldColumn,101)
WHERE SomeTableKey BETWEEN ASmallCode AND ABiggerCode
Find a key in your table that you can use to divide up the data and try updating half the table... now halve it again and again until you find the offending data. Post the data here and we will come up with some code to allow for it.
I think you should convert END_DATE to DATETIME type, because you have 36 million rows and it will give a performance boost when you do not have to cast or convert it datetime with select statement.
To answer your question, you can do something like
select CAST(END_DATE AS DATETIME) FROM MyTable
The PARSE (SQL 2012) or CONVERT (any version) can do the conversion for you. Your UPDATE query will look something like one of these statements:
UPDATE the_table SET the_column = PARSE(end_date AS datetime2 USING 'en-US')
UPDATE the_table SET the_column = CONVERT(datetime2, end_date, 101)
DECLARE #End_DATE VARCHAR(10);
SET #End_DATE = '12/20/2013';
SELECT CAST(#End_DATE AS DATE)
RESULT: 2013-12-20
Empty Column formatted as Date, I am guessing you meant to say you have DATE datatype column and you would like to update this VARCHAR(10) date to , Date datatype column you could do something like this....
UPDATE Table_Name
SET New_End_DATE = CAST(End_DATE AS DATE)
Working SQL FIDDLE