How to sort by varchar field containing a date in ascending order [duplicate] - sql

This question already has answers here:
Comparing dates stored as varchar
(3 answers)
Closed 3 years ago.
In design time we have given [DOJInGovrService] field as varchar datatype.
now when we are trying order by this parameter(DOJInGovrService) in ascending order it is not giving the desired result.
I know it is datatype problem but I can't change the datatype now as data is already entered.
SELECT ED.class,
ED.CurrentOfficePlace,
ED.DOB,
ED.DOJInCurrentOff,
ED.DOJInGovrService,
ED.DOJInSamvarg,
ED.EmpName,
ED.HomePlace, ED.Qualification
FROM tbl_EmplyeesBiodata ED
ORDER BY DOJInGovrService asc
Date entered is in format dd-MM-yyyy (i.e. 28-08-2004).
please help me

This is just one of many reasons why you should Always use appropriate data types.
Even when you have data in the table, you can change the data type using an alter table ddl statement:
ALTER TABLE tableName
ALTER COLUMN ColumnName datetime2;
However, you should copy this table first and try the alter on the copy, so that if it messes up your data you will not risk anything.
If the alter is successful then do it on your live table. if not, you can go with a different approach, involving these stages:
rename the DOJInGovrService column to DOJInGovrService_old. use sp_RENAME.
Add a column DOJInGovrService with the correct datatype (datetime2), using alter table ddl stement.
Update the new DOJInGovrService column with the values in DOJInGovrService_old. you will probably need to use convert.
drop the DOJInGovrService_old column using the alter table ddl statement.

Try to convert it to datetime
select ED.class,ED.CurrentOfficePlace,ED.DOB,ED.DOJInCurrentOff,ED.DOJInGovrService,ED.DOJInSamvarg,ED.EmpName,ED.HomePlace,ED.Qualification
from tbl_EmplyeesBiodata ED
order by convert(date,DOJInGovrService,105) asc
This is only direct solution. The best way to do that is create new column with date type. The column will helps you to create query without cast or convert.

Related

PostgreSQL UPDATE statement treats keyword as a column

sorry for the noob question, just getting started with SQL and I have a problem that I could not resolve.
Basically, I created a table with "date of birth" column and chose wrong TYPE (integer), so every time I am trying to enter date of birth as "1998-01-04" it is just showing 1998. So, I decided to update the column to TEXT type.
Tried these queries
UPDATE users
SET date_of_birth = VARCHAR
It shows me error that there are no VARCHAR columns. Tried with as 'VARCHAR', still not working, tried as 'TEXT' and TEXT, still the same error.
What am I doing wrong?
Strings are as bad or worse than integers for this purpose. You want to use:
alter table alter column date_of_birth type date;
Note: If you have existing data in the column, you need to either use the using clause to convert it to a date. Or NULL it all out.
use this for your alter table statement
ALTER TABLE users
ALTER COLUMN date_of_birth TYPE VARCHAR(50);
I am not sure why you are storing date as varchar whereas you can store it as date, if you want to change that to date then use
ALTER TABLE users
ALTER COLUMN date_of_birth TYPE DATE;

SQL convert varchar into Date

A long time ago I created a database, and completely forgot to set the column to Date and now looking at the data, I want to extract, it looks like 2006-05-06.
How would I run a SQL statement to convert it into the correct format (dd/MM/yyyy) 06/05/2006, I'm running with the British format "103".
What I was planning on doing, I've already added a second column (s_batch_convert2) to the database, hoping to convert into that and then delete the original column (s_batch_convert), renaming the new column to the old one.
UPDATE s_service_repairs
SET s_batch_convert2 = TRY_CONVERT(Date, s_batch_convert, 103)
Am I along the right lines?
You should convert your existing column to a bona fide date. It seems to have the right format:
alter table s_service_repairs alter column s_batch_convert date;
Then you can add a computed column for the format you want:
alter table s_service_repairs s_batch_convert_mmddyyyy as ( try_convert(varchar(10), s_batch_convert, 103) );

ORA-01843: not a valid month error

I have a column in Oracle DB which is varchar2 data type. Typical value stored in this column is like 06/16/2015 02:14:18 AM.
I am trying to get all records wherein this column is having records after 1st August 2015.
select *
from MYTABLE
where to_date(substr(MYCOLUMN,1,10),'dd-mm-yyyy') > to_date('01-08-2015','dd-mm-yyyy');
But, I am getting ORA-01843. Where am I doing wrong?
Respect the format in your VARCHAR
....where to_date(substr(MYCOLUMN,1,10),'mm/dd/yyyy')
I have a column in Oracle DB which is varchar2 data type. Typical value stored in this column is like 06/16/2015 02:14:18 AM.
The first question is why do you store DATE as string? Using appropriate data type is one of the most important part of database design and performance.
Understand that DATE doesn't have the format you see, it is internally stored in 7 bytes which is Oracle's proprietary format. Storing date as a string to have a fixed format is not recommended.
I would suggest first fix the design so that you don't have to do this overhead activity while comparing dates. In the longer run it will help you.
1. Add a new column as DATE data type.
ALTER TABLE table_name
ADD new_column DATE;
2. Update the new column.
UPDATE table_name
SET new_column = TO_DATE(old_column, 'mm/dd/yyyy hh:mi:ss pm');
3. DROP the old column.
ALTER TABLE table_name
DROP COLUMN old_column;
4. Rename the new column to old column name.
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
Now, you could compare dates easily:
SELECT * FROM MYTABLE WHERE mycolumn > to_date('01-08-2015','dd-mm-yyyy');
This will also use any regular index on the date column.
From performance point of view:
If you don't fix it now, you will keep facing performance issues. Because the immediate fix of SUBSTR will not let you use any regular index, you need to create a function-based index.
If in your table all values like 06/16/2015 02:14:18 AM then you can use trunc(to_date(MYCOLUMN,'mm/dd/yyyy HH:mi:SS PM'),'dd') against to_date(substr(MYCOLUMN,1,10),'dd-mm-yyyy').

How to change the length of a column in a SQL Server table via T-SQL [duplicate]

This question already has answers here:
What is the SQL to change the field length of a table column in SQL Server
(6 answers)
Closed 9 years ago.
I am looking to find out how to alter a fields properties contained in an SQL Server 2008 table via an SQL script.
I'm looking to change the 'Length' property specifically.
Does anybody know how to do this?
Thanks
So, let's say you have this table:
CREATE TABLE YourTable(Col1 VARCHAR(10))
And you want to change Col1 to VARCHAR(20). What you need to do is this:
ALTER TABLE YourTable
ALTER COLUMN Col1 VARCHAR(20)
That'll work without problems since the length of the column got bigger. If you wanted to change it to VARCHAR(5), then you'll first gonna need to make sure that there are not values with more chars on your column, otherwise that ALTER TABLE will fail.

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.