How do I covert a number to a date in SQL? - sql

I have a field named SECURED that has dates listed as 123120, 040320 which is actually 12/31/20 or 4/03/20.
I was able to convert the date in the SELECT area with:
SELECT LEFT(SECURED, 2) + '/' + replace(replace(SECURED, LEFT(SECURED, 2), ''),
RIGHT(SECURED, 2), '') + '/' + RIGHT(SECURED, 2) as 'Modified SECURED as Date'
WHERE CONVERT(date, SECURED, 101)
BETWEEN CONVERT(date, getdate() - 30, 101)
AND CONVERT(date, getdate(), 101)
How can I pull in information from the SECURED column with a date of 30 days ago?
It doesn't work and still sees a date as a number.

You can use datefromparts() to convert your string to a date:
datefromparts(
concat('20', substring(secured, 5, 2)),
substring(secured, 1, 2),
substring(secured, 3, 2)
)
Then you can check it against a given interval:
datefromparts(
concat('20', substring(secured, 5, 2)),
substring(secured, 1, 2),
substring(secured, 3, 2)
) between dateadd(day -30, cast(getdate() as date)) and cast(getdate() as date)

If secured is a string, you can do a little manipulation and a simple cast():
select convert(date, concat('20', right(secured, 2), left(secured, 4))
You might find it convenient to actually create a computed column so this is always available:
alter table t add secured_date as (try_convert(date, concat('20', right(secured, 2), left(secured, 4))
You can even persist the column and create an index, so your queries are more efficient.

There're many ways to convert a 6 digits INT to DATE:
You can do a convertion using LTRIM and CAST like this:
SELECT CAST(RIGHT(LTRIM(123120),2) + LEFT(LTRIM(123120),4) AS DATE)
Using LTRIM with SUBSTRING:
SELECT CAST(SUBSTRING(LTRIM(040320), 5, 2) + SUBSTRING(LTRIM(040320), 1, 4) AS DATE)
Using CONVERT:
SELECT CAST((RIGHT(CONVERT(VARCHAR(6), 123120),2) +
LEFT(CONVERT(VARCHAR(6), 123120),4)) AS DATE)
Or using CONVERT with SUBSTRING:
SELECT CAST(SUBSTRING(CONVERT(VARCHAR(6), 040320), 5, 2) +
SUBSTRING(CONVERT(VARCHAR(6), 040320), 1, 4) AS DATE)
Using STR:
SELECT CAST(RIGHT(STR(123120, 6) ,2) + LEFT(STR(123120, 6) ,4) AS DATE)
Or using STR with SUBSTRING:
SELECT CAST(SUBSTRING(STR(040320, 6), 5, 2) + SUBSTRING(STR(040320, 6), 1, 4) AS DATE)
This casting will format the date as expected but your dates need be
in format mmddyy
Standard EUA (style 10) DATE format (mm-dd-yy) without century (yy)
Edit after comments:
What's the table name of this query?
What's the SECURED field datatype? INT? VARCHAR?
You need convert a date and compare between last 30 days and today?
The query below resolve your problem? (change TABLE_NAME to your table name)
SELECT LEFT(SECURED, 2) + '/' + replace(replace(SECURED, LEFT(SECURED, 2), ''),
RIGHT(SECURED, 2), '') + '/' + RIGHT(SECURED, 2) as 'Modified SECURED as Date'
FROM TABLE_NAME
WHERE CAST(RIGHT(LTRIM(SECURED), 2) + LEFT(LTRIM(SECURED), 4) AS DATE)
BETWEEN GETDATE() - 30 AND GETDATE()

Related

SQL Server : converting varchar field to date

I have in my table a varchar column with bunch of dates in the following format dd-MM-yyyy
31-12-2018
01-01-2019
02-01-2019
I need to write a date based query that gets all the dates before 01-01-2019.
I have tried using both CAST and CONVERT to convert these table values without luck.
Using CAST, my code is below:
SELECT
CAST('''' + SUBSTRING(arc_billed_to_date, 4, 2) + '-' + SUBSTRING(arc_billed_to_date, 1, 2) + SUBSTRING(arc_billed_to_date, 6, 5)+ '''' AS date),
CAST('''' + SUBSTRING(arc_billed_to_date, 7, 5) + '-' + SUBSTRING(arc_billed_to_date, 1, 2) + '-' + SUBSTRING(arc_billed_to_date, 4, 2) + '''' AS date),
CAST('''' + SUBSTRING(arc_billed_to_date, 7, 5) + '-' + SUBSTRING(arc_billed_to_date, 4, 2) + '-' + SUBSTRING(arc_billed_to_date, 1, 2) + '''' AS DATE),
CONVERT(DATE, '12-31-2018') AS x
FROM
wkpol
Using Convert
select Convert(datetime,'''' + SUBSTRING(arc_billed_to_date,7,5) + '-' + SUBSTRING(arc_billed_to_date,4,2) + '-' + SUBSTRING(arc_billed_to_date,1,2)+ '''',105) as x from wkpol
The error I get is
Conversion failed when converting date and/or time from character string.
Any help is appreciated.
SELECT *
FROM wkpol
WHERE convert(date, arc_billed_to_date, 103) < convert(date, '01/01/2019');
Well you are going to get many blames for using varchar field for date. Anyway, assuming it is a matter of another thread you can do the conversion like:
select * from myTable
where cast(right(arc_billed_to_date,4) +
substring(arc_billed_to_date,4,2) +
left(arc_billed_to_date,2) as date) < '20190101';
You wouldn't be using any index either.
In addition to Sean's comments, you can also set the DateFormat as DMY
Example
Declare #YourTable table (SomeCol varchar(50))
Insert Into #YourTable values
('31-12-2018')
,('01-01-2019')
,('02-01-2019')
Set DateFormat DMY
Select AsDate = try_convert(date,SomeCol)
From #YourTable
Returns
AsDate
2018-12-31
2019-01-01
2019-01-02

Date Conversion of yymmddhhmm.yymmddhhmm in SQL

I'm getting this type of data in DateandTime Column in SQl.
1803301611.1803301611
Format is yymmddhhmm.yymmddhhmm and I have to convert this data in a date format.
The DateandTime Column date type is Varchar(5)
I don't think the format of your time string can be directly converted by SQL Server. But, we can try to piece together into a format which can be. This is the closest I could come, and it required prepending a 20 in front of the year to make a full 4 digit century:
SELECT
col AS input,
CONVERT(datetime, '20' + SUBSTRING(col, 1, 2) + '-' + SUBSTRING(col, 3, 2) + '-' +
SUBSTRING(col, 5, 2) + ' ' + SUBSTRING(col, 7, 2) + ':' +
SUBSTRING(col, 9, 2), 120) AS output
FROM yourTable;
Demo
We ideally should have been able to use mask 20 without the century, but I could not get it to work.
You can do :
select *, cast(concat(cast(left(col, 6) as date), ' ',stuff(right(col, 4), 3, 0, ':')
) as datetime
) as newdatetime
from table t;

SQL Server : format 700 as time

I've run an import which has updated many records in my tblRota.StartTime and tblRota.EndTime in the format 900 and 1700.
How can I reformat these to 09:00 and 17:00?
The datatypes of both columns is varchar.
Thank you.
You could use this query:
select stuff(right('0' + replace([StartTime], ':', ''), 4), 3, 0, ':'),
stuff(right('0' + replace([EndTime], ':', ''), 4), 3, 0, ':')
from [tblRota]
The steps are:
Remove the :: replace([StartTime], ':', '')
Get the time on 4 digits: right('0' + <3Or4DigitTime>, 4)
Insert the :: stuff(<4DigitTime>, 3, 0, ':')
Use some string manipulation:
UPDATE tblRota
SET StartTime = LEFT(RIGHT('0'+StartTime , 4),2)+':'+RIGHT(StartTime ,2),
EndTime = LEFT(RIGHT('0'+EndTime , 4),2)+':'+RIGHT(EndTime ,2)

SQL date conversion year/datepart to datetime

I am trying to convert from "tick" date format to a datetime format.
Example:
0000113326 to 2013-11-22 00:00:00.000
I know how to go the opposite way:
SELECT '00001' + RIGHT(CAST(YEAR(StartDate) AS varCHAR(4)), 2) + RIGHT('000' + CAST(Datepart(dy, StartDate) AS VARCHAR(3)), 3)
FROM table
I just need to reverse engineer it.
Any ideas?
Something like this?
DECLARE #tick varchar(20) = '0000113326'
SELECT dateadd(d, cast(right(#tick, 3) as int) - 1, '20' + substring(#tick, 6, 2) + '0101')

SQL Server: Date conversion problem?

I have a column which has date-values stored in varchar (format ddmmyy) like this
231280
121280
131185
...
How to convert these values into datetime data type?
Set DateFormat DMY
GO
Select Cast(Stuff(Stuff(SomeValue, 3, 0, '-'), 6, 0, '-') As datetime)
From MyData
Use substring to get year, month, and day, if year greater than 11 add 19 to it if not add 20, to get year in format 2009, but this is your query just instead of string add your column name
select CAST(
CASE WHEN CAST(SUBSTRING('231280', 5, 2) AS INT) >11
THEN '19'+SUBSTRING('231280', 5, 2)
else '20'+SUBSTRING('231280', 5, 2)
END
+'-'+SUBSTRING('231280', 3, 2)+'-'+SUBSTRING('231280', 1, 2) as datetime)
You'd have to use some substring footwork to convert your string to a known date format. Here's an example converting the string to format 3, "British/French":
declare #YourTable table (YourColumn varchar(50))
insert #YourTable
select '231280'
union all select '121280'
union all select '131185'
select convert(datetime, substring(YourColumn,1,2) + '/' +
substring(YourColumn,3,2) + '/' + substring(YourColumn,5,2), 3)
from #YourTable
Because this format is non standard, use
DECLARE #field char(6)
SET #field = '231280'
select convert(datetime, right(#field, 2) + substring(#field, 3, 2) + left(#field, 2) , 12)