Convert a string to a date in Access - sql

I'm migrating data between tables in Access 2003. In the old table, the date was stored as a text field in the format YYYYMMDD.
I want to store the field as a datetime in the new table. I've tried using CDate() in my SQL statement, but it just displays as #Error in the results.
What am I doing wrong?

e.g. cdate(format("20091231", "####/##/##"))
So, in your case it will be
SELECT cdate(format(mystringFieldThatIsInYYYYMMDDFormat, "####/##/##"))
FROM myData

Related

Trouble converting CSV date from non standard format SQL

I have dates in the format 01jan2020 (without a space or any separator) and need to convert this to a date type in SQL Server 2016 Management Studio.
The data was loaded from a .CSV file into a table (call it TestData, column is Fill_Date).
To join on a separate table to pull back data for another process, I need the TestData column Fill_Date to be in the correct format (MM-DD-YYYY) for my query to run correctly.
Fill_Date is currently in table TestData as datatype varchar(50).
I want to either see if it is possible to convert it with TestData table or directly insert the result into a 2nd table that is formatted.
Thanks (NEWB)
I ended up solving by converting the data while dropping into a temp table, deleting old value, and then inserting from that table back into the TestData table.
CONVERT(VARCHAR,CONVERT(date,[fill_date]),101) AS fill_date

Different result in view and stored procedure

My table structure like this
I am create view for this table
SELECT Entered FROM dbo.tbPatientReport
In the table data like this
If i execute view from sql query then it will give output like
select Entered from view_abc
Desired Output
You should format date as you want in result.
e.g.
SELECT FORMAT( Entered,'MM/dd/yyyy hh:mm:ss tt','en-US') AS 'US'
FORMAT is one of the new built-in String Function introduced as a Part of Sql Server 2012. It returns the value formatted in the specified format using the optional culture parameter value. It is not an Sql Server native function instead it is .NET CLR dependent function.
SYNTAX: FORMAT ( value, format [, culture ] )
Edit
Can you test this will be working.
SELECT CONVERT(VARCHAR(20), SYSDATETIME(), 22)
Format: MM/DD/YY HH:MI:SS AM
Output: 10/25/13 2:21:00 PM
Only problem is you cannot get FullYear.
Your [Entered] column [Datetime] in your table structure does not allow nulls, it's not ticked.
What version of SQL Management Studio are you using? And are you selecting the data in a plain select query or are you choosing to View the data?
In older versions "View" opens the edit window with your code on top.
While in the editing view the last row will always be NULL as it is expecting new data.

Convert datatype of existing data from Char to DateTime with SQL

I am relatively new to SQL Server and I am trying to update the datatype of about 3000 records from a Char to Datetime so I can use the DATEDIFF function. I created a view that achieves the conversion but what I think I need to do is alter the data in the origin table.
SELECT
CONVERT(datetime, CONVERT(char(8), TRANS_ACCOUNTINGDATE_ALLCAMPAIGNS_2010_ALLPROCESSINGACCOUNTS_ALL))
FROM Accounting;
What I think I need to do is an alter table and iterate over each row performing the conversion. Trying to change the data type using the GUI is not working for me.
Any help would be appreciated.
Thanks
The datatype is an attribute of the COLUMN, not just of the data inside the column. You can't put datetime data into a char field - that's the purpose of data types!
You need to add a new field and run an UPDATE statement to populate it with your converted data. Then you can drop the original field and rename your new one to the original name.

UPDATE each item in a column

I'm having trouble figuring out a solution to this. I have several tables in my database with attributes having a date type. However, in one of my tables I was not thinking during the design process so the attribute is not of date but is a varchar. The dates in the "incorrect" table is formatted as dd-MMM-yyyy whereas all of the other dates are formatted as yyyy-mm-dd.
How can I run through the "incorrect" column and do CAST(myDate AS date) on each mis-typed attribute?
I'd migrate to a date column.
Add a nullable date column
Run an update query to set the value of each column to the result of your cast
Drop the varchar column
Refactor your code or rename the new column to the name of the old one

inserting only date in a column in sql server

Is there a way to insert only date in a datetime column in sql server without the time?
for example
date (datetime)
===============
12-01-2000
16-02-2000
or i should store this as a varchar and cast it when retriving so that i can convert to whatever form i need.
my solution is to store it as varchar and convert it to datetime whenever needed
SELECT CONVERT(VARCHAR(10),GETDATE(),111) -- get datepart only
or
also check this post about creating date type :
create user defined data types:
create type Date from dateTime
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
If you are using SQLServer 2008 you can use the date data type.
The following SQL will strip out any time values and set them all to zero. So you won't need to worry whether a time value is there or not.
Select Cast(Floor(Cast(MyDateColumn as float)) as DateTime) as MyDateColumn
From dbo.MyTable
Just use smalldatetime or date. Convert your dates to your format before you update your date values or after you select date values in your app.
You can change format of date format in sql queries or in your app.
Here is a list on date formats in sql
http://www.sql-server-helper.com/tips/date-formats.aspx
Here's a link on date data types
http://databases.about.com/od/sqlserver/a/date_time.htm
Good Luck!