Convert Datetime values in Varchar column - sql

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

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]

Displaying date format SQL Server

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

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

Working with Time portion of a DateTime Column in SQL Server2008

i have a table in SQL Server 2008 which its name is Table1.
Table1 has a column named CreateDate which its data type is datetime.
Now, I wanna to get records that their createDate field values are more than for instance 1 hour.
In SQL Server 2008, you should use the built-in TIME datetype as much as possible.
If you have a DATETIME column, you can easily convert it to just TIME by using:
SELECT CAST(CreateDate AS TIME) FROM Table1
That should return just the TIME part of the DATETIME column.
Marc
try with some datetime functions ,like DATEADD functions
e.g : select dateadd(hh,-1,GETDATE())
This will get records that are older than one hour:
select * from Table1 where createDate < dateadd(hh, -1, getdate())