Converting nvarchar to date column - sql

I have a table that has a column of dates in m/d/yyyy format stored in nvarchar(50) data types (I inherited this...). I would like to convert these columns into date data types so they are correctly stored, but can't afford to accidentally drop the data in the columns.
I've tried doing this in SQL server 2008R2, using the designer to change the data type to date. This worked for my test column (kept the dates, corrected the format, etc.) but all nulls became 1900-01-01. I can live with that but it seems there's a more eloquent solution.
However, when I tried it on the real data column, I received an error:
Unable to modify table. Conversion failed when converting date and/or time from character string.
What would be the proper way to handle this? Sorry if this is duplicate. I've been looking for an hour and haven't figured it out.

You apparently have data somewhere in the table that SQL Server is unable to convert to date. You can find the rows with a query along these lines:
SELECT * FROM TableName WHERE ISDATE(ColumnName) != 1
Obviously, you'll need to substitute TableName and ColumnName with the actual names.
Once you've identified the rows with bad data, handling it is up to you... change the data manually for each row, set them to null, whatever you feel is acceptable in this case.

I would suggest you add a new column with a Date datatype then convert/copy the data over and finally delete the original column.

what you can do is
add another column to the table of type DATETIME
Update new column values by casting the existing column values as DATETIME
Remove the existing column

Maybe you can temporally duplicate the columns with the data type of date, with this you can run an update command to convert the varchar to datetime and store it in this new columns, when you are sure that all information is correct, you can delete the columns and rename the new ones to their original name.
I always do this when I need to ensure to not lose information on change data types.

You can use following query to update your date to ISO Format (yyyymmdd) which will gurantee to work in any server. SQL SERVER - DEMO covers 4 possible cases of your date like 'd/m/yyyy','dd/m/yyyy','d/mm/yyyy' & 'dd/mm/yyyy' and update them to ISO Format
Update mytable
Set d = right(d,charindex('/',reverse(d),1)-1) +
right('0' + substring(d, charindex('/',d,1) + 1, charindex('/', d, charindex('/',d,1) + 1 ) - charindex('/',d,1) -1 ),2) +
right('0' + left(d,charindex('/',d,1)-1),2)
Or you can use this method directly in a select query as
Select id,convert(date,
right(d,charindex('/',reverse(d),1)-1) +
right('0' + substring(d, charindex('/',d,1) + 1, charindex('/', d,
charindex('/',d,1) + 1 ) - charindex('/',d,1) -1 ),2) +
right('0' + left(d,charindex('/',d,1)-1),2)
) correctDate
From mytable

Related

How to split dash-separated values in SQL Server

I have a date saved in an nvarchar type and I want to split the day, month and year into separate nvarchar variables (that means three variables). The date looks as follows: exposure_date ='2018-12-04' and the format is yyyy-dd-mm
any help please?
My whole project is stuck on this.
The "correct" answer here is to fix your datatype. When storing data always choose an appropriate data type for the data you're storing. For a date (with no time part) then the correct datatype is date. if you're storing numerical data, then use a numerical datatype, such as int or decimal. (n)varchar is not a one size fits all datatype and using it to store data that has a data type designed for it is almost always a bad choice. I'm storing the data as an (n)varchar because I need it in a specific format is never an excuse; have your presentation layer handle to display format, not your RDBMS.
The first step, therefore would be to change your string representation yyyy-dd-MM of a date to the ISO format yyyyMMdd by doing:
UPDATE YourTable
SET exposure_date = LEFT(exposure_date,4) + RIGHT(exposure_date,2) + SUBSTRING(exposure_date,6,2);
Now you have a unambiguous representation, you can change the data type of your column without concerns of incorrect implicit casts or error:
ALTER YourTable ALTER COLUMN exposure_date date;
Then, finally, you can treat your data as what it is, a date, and use the DATEPART function:
SELECT DATEPART(YEAR,exposure_date) AS Exposure_Year,
DATEPART(MONTH,exposure_date) AS Exposure_Month,
DATEPART(DAY,exposure_date) AS Exposure_Day
FROM YourTable;
You can also try the following
Declare #myDate date
select #myDate= Cast(substring('2011-29-12', 1, 4)
+ '-' + substring('2011-29-12', 9, 2)
+ '-' + substring('2011-29-12', 6, 2)
as Date) --YYYY-MM-DD
Select #myDate as DateTime,
datename(day,#myDate) as Date,
month(#myDate) as Month,
datename(year,#myDate) as Year,
Datename(weekday,#myDate) as DayName
The output is as shown below
DateTime Date Month Year DayName
--------------------------------------------
2011-29-12 29 12 2011 Thursday
You can find the live demo here
You can try below -
select concat(cast(year(cast('2018-12-04' as date)) as varchar(4)),'-',
cast(month(cast('2018-12-04' as date)) as varchar(2)), '-',
cast(day(cast('2018-12-04' as date)) as varchar(2)))
from tablename
If you have fixed format, then you could use this simple query with substring method:
select substring(dt, 1, 4) + '-' +
substring(dt, 9, 2) + '-' +
substring(dt, 6, 2) [YYYY-MM-DD]
from (values ('2018-31-12')) tbl(dt)
Let's go directly to the main issue, which is you are using the wrong datatype to store dates, you should store them as DATE, the datatypes are there for a reason and you need to choose a proper one for your column.
So, you need to ALTER your table and change the column datatype to DATE instead of NVARCHAR datatype.
ALTER <Table Name Here>
ALTER COLUMN <Column Name Here> DATE;
Then all things will easy, you just run the following query to get the desired output
SELECT YEAR(<Column Name Here>) TheYear,
MONTH(<Column Name Here>) TheMonth,
DAY(<Column Name Here>) TheDay
FROM <Table Name Here>
Which is the right and the best solution.
You can also (if you are not going to alter your table) do as
CREATE TABLE Dates(
StrDate NVARCHAR(10)
);
INSERT INTO Dates VALUES
(N'2018-12-04'),
(N'Invalid');
SELECT LEFT(StrDate, 4) StrYear,
SUBSTRING(StrDate, 6, 2) StrMonth,
RIGHT(StrDate, 2) StrDay
FROM Dates;
OR
SELECT YEAR(StrDate) StrYear,
MONTH(StrDate) StrMonth,
DAY(StrDate) StrDay
FROM (
SELECT TRY_CAST(StrDate AS DATE) StrDate
FROM Dates
)T

date time stored as varchar in sql how to filter on varchar

I am working on a project in which dates and times ar stored as a varchar e.g. "30-11-2017,7:30" first date in dd-mm-yyy format and then time separated with a comma. I am trying to filter on it but it is not working correctly kindly guide me how to filter data on date.
select *
from timetrack
where startDateAndTime >= '30-11-2017,7:30'
In attached image records have been shown. When I apply above query it shows no records
You can easily convert your date to SQL datatype datetime uisng parse function, for example select parse('30-11-2017,7:30' as datetime using 'it-IT').
So, in your case, you can apply this function in where clause, so you can easily apply comparison between dates:
select *
from timetrack
where parse(startDateAndTime as datetime using 'it-IT') >= '2017-11-30 07:30:00.000'
Your format is apparently italian :) But you have to specify your own date in the format convertable to datetime, as I have done in above example.
NOTE: parse is available starting with SQL Management Studio 2012.
Unless you are using ISO date format (yyyy-MM-dd HH:mm:ss or close) applying ordering (which inequalities like greater than or equal use) will not work: the date order is disconnected from the string ordering.
You'll need to parse the date and times into a real date time type and then compare to that (details of this depend on which RDBMS you are using).
If, you want to just filter out the date then you could use convert() function for SQL Server
select *
from timetrack
where startDateAndTime >= convert(date, left(#date, 10), 103)
Else convert it to datetime as follow
select *
from timetrack
where startDateAndTime >= convert(datetime, left(#date, 10)+' ' +
reverse(left(reverse(#date), charindex(',', reverse(#date))-1)), 103)
You need the date in a datetime column, Otherwise you can't filter with your current varchar format of your date.
Without changing the existing columns, this can be achieved by making a computed column and making it persisted to optimize performance.
ALTER TABLE test add CstartDateTime
as convert(datetime, substring(startDateAndTime, 7,4)+ substring(startDateAndTime, 4,2)
+ left(startDateAndTime, 2) +' '+ right(startDateAndTime, 5), 112) persisted
Note: this require all rows in the column contains a valid date with the current format
Firstly, you need to check what is the data that is entered in the 'startDateAndTime' column,then you can convert that varchar into date format
If the data in 'startDateAndTime' column has data like '30-11-2017,07:30', you would then have to convert it into date:
SELECT to_date('30-11-2017,07:30','dd-mm-yyyy,hh:mm') from dual; --check this
--Your query:
SELECT to_date(startDateAndTime ,'dd-mm-yyyy,hh:mm') from timetrack;

Varchar to Date and Time

I have a column MyColumn and data type is Varchar (50) in sql server 2012. It stores dates, time, and other info. I can modify the query to generate only date and time rows but format has issues.
Q1) How can I change result from 20141213 to 12132014. I used the below and it did not work. It only works when data type for MyColumn is DATETIME or related but with Varchar it won't work.
SELECT REPLACE(CONVERT(VARCHAR(10), MyColumn, 110),'-','') from MyTable
Q2) MyColumn stores time as example 0815. How can I include semi-colon to result as 08:15?
Please assist.
Thanks.
i will not recommend saving date in a string, you can save data type as Date or DateTime Datatype,
answer for Q1 is
SELECT SUBSTRING(MyColumn, 5,2) + RIGHT(MyColumn, 2) + LEFT(MyColumn, 4)
but remember its works only when your sting in YYYYMMDD format and if you change formating then wrong answer will come or may be get error
and answer for Q2 is
SELECT LEFT(MyColumn, 2) + ':' + RIGHT(MyColumn, 2)

How to store hour datepart in SQL in table

I have a table with 2 columns: Customer_ID, which is a string, identifying each client and Time_id: a string with 14 characters, identifying timestamp of a transaction. Example:
Customer_id; Time_id
12345; 20140703144504
I want to be able to use datediff in hours datepart, but I can´t seem to be able to convert time_id properly. I use the following query:
update transation_table
set time_id= (
convert(timestamp, time_id)
)
It works, but removes hours datepart, which is what I need. For day datepart I can do it, converting to datetime. How can I keep in the table the hh?
edit: I´m running MS SQL Server 2014.
best regards
Using the convert and string concatenation below, you can use DATEPART on the resulting value.
DECLARE #tmp TABLE(
Customer_id VARCHAR(50),
Time_id VARCHAR(50)
)
INSERT INTO #tmp
SELECT '12345','20140703144504'
select
*,CONVERT(DATETIME,
SUBSTRING(Time_id,5,2) + '/' +
SUBSTRING(Time_id,7,2) + '/' +
SUBSTRING(Time_id,1,4) + ' ' +
SUBSTRING(Time_id,9,2) + ':' +
SUBSTRING(Time_id,11,2) + ':' +
SUBSTRING(Time_id,13,2)
,101
)
from #tmp
Use FORMAT to get a string representation of the value in a supported format (ODBC canonical in the Date and Time styles chart), then use TRY_CONVERT to return an actual datetime value:
SELECT TRY_CONVERT(DATETIME,
FORMAT(CAST('20140703144504' AS BIGINT),
'####-##-## ##:##:##'),
120);
This requires SQL Server 2012+.
As mentioned elsewhere, the data should be stored in a single datetime2 column, or paired date and time columns. The above functions can be used to help convert existing data to the new column(s).

finding data lying between a specific date range in sql

I want to find records from my database which lie between any user input date range(say between 10/2/2008 to 26/9/2024). I tried using
SELECT NAME
,TYPE
,COMP_NAME
,BATCH_NO
,SHELF
,MFG_DATE
,EXP_DATE
,QTY
,VAT
,MRP
FROM STOCK_LOCAL
WHERE
convert(VARCHAR(20), EXP_DATE, 103)
BETWEEN convert(VARCHAR(20), #MEDICINEEXP_DATE, 103)
AND convert(VARCHAR(20), #MEDICINEEXPDATE, 103)
but with this query i need to enter perfect date range which is available in my database, it is not giving me data lying in between any date entered.
Thanks in advance
Since it is a poolr designed schema there isnt going to be any decent/Efficient solution for this.
In sql server if you are storing Date or Date & Time data. Use the Data or DATETIME datatypes for your columns.
In your case you are trying to compare a string with passed date. and even when you tried to convert the string (Date) into date datatype you didnt do it correctly.
My suggestion would be Add new columns to your table with Date datatype and update these columns with existing date/string values.
For now you can convert the Date(string) into date datatype using the following code.
DECLARE #MEDICINEEXP_DATE DATE = 'SomeValue1'
DECLARE #MEDICINEEXPDATE DATE = 'SomeValue1'
SELECT query....
FROM TableName
WHERE
CAST(
RIGHT(EXP_DATE, 4)
+SUBSTRING(EXP_DATE,CHARINDEX('/',EXP_DATE)+1,2)
+LEFT(EXP_DATE,2)
AS DATE) >= #MEDICINEEXP_DATE
AND CAST(
RIGHT(EXP_DATE, 4)
+SUBSTRING(EXP_DATE,CHARINDEX('/',EXP_DATE)+1,2)
+LEFT(EXP_DATE,2)
AS DATE) <= #MEDICINEEXPDATE
Note
This solution will get you the expected results but very inefficient method. It will not make use of any indexses on your EXP_DATE Column even if you have a very buffed up index on that column.