SQL Server - convert short date in from old database - sql-server

I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.

You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)

Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24

declare #date date = '901124'
select CONVERT(date, #date) as [Date]

Related

Format GETDATE() in SQL Server

I'm trying to get yy-MM, I tried this but it doesn't work
DECLARE #Date VARCHAR(10) = CONVERT(date, DATEADD(month, -1, CONVERT(date, GETDATE(), 'yy-MM')));
SELECT #Date AS [Date]
How to make it return 2020-03 rather than 2020-03-30?
How to make it return as 2020-03 rather then 2020-03-30?
Are you looking for this?
SELECT convert(varchar(7), getdate(), 126)
Output
2020-04
Demo Here

Get the difference in weeks, data stored as YYYYWW in TSQL

I have customer_since stored as YYYYWW i.e., 201852.
The below script is what I have been using to work out the difference, however when I have this situation of 201901 - 201852, it gives 48 instead of 1.
is there mod function or something that can be incorporated here to resolve the issue?
CAST(CONCAT (DATEPART(YEAR, DATEADD(MONTH, + 9, GETDATE()))
, RIGHT('0' + RTRIM(DATEPART(WEEK, DATEADD(MONTH, - 3, GETDATE()))+1),2)
) AS INT) - a.customer_since AS Customer_since
You can use the following template to get the first (or the last) date of a given year and week. Then having valid date times, simply use DATEDIFF as it has built in functionality to get difference in weeks:
DECLARE #WeekNo int= 52
DECLARE #Year int=2018
SELECT DATEADD(wk,#WeekNo-1,DATEADD(yy,#Year-1900,0)) AS WeekStart,
DATEADD(wk,#WeekNo,DATEADD(yy,#Year-1900,0))-1 AS WeekEnd
GO
DECLARE #WeekNo int= 1
DECLARE #Year int=2019
SELECT DATEADD(wk,#WeekNo-1,DATEADD(yy,#Year-1900,0)) AS WeekStart,
DATEADD(wk,#WeekNo,DATEADD(yy,#Year-1900,0))-1 AS WeekEnd
GO
SELECT DATEDIFF(WEEK, '2018-12-24 00:00:00.000', '2019-01-01 00:00:00.000')
In one statement:
DECLARE #Input01 VARCHAR(6) = '201852'
,#Input02 VARCHAR(6) = '201901'
SELECT DATEDIFF(WEEK, DATEADD(WEEK, RIGHT(#Input01, 2)-1,DATEADD(YEAR,LEFT(#Input01, 4)-1900,0)), DATEADD(WEEK,RIGHT(#Input02, 2)-1,DATEADD(YEAR,LEFT(#Input02, 4)-1900,0)));
You can create a function which will accept the date string('201852') and will return a date like `Create Function Todate(#dt varchar(100))
returns Date
Begin
Declare #wk int=(Select RIGHT(#dt,2))
return (Select Dateadd(WK, #wk, cast(LEFT(#dt,4) as date)))
End`
Now all you have to do is pass the date string in the function and find a DATEDIFF with start and End date

Let the user select how many days of data he wants

declare #Days varchar(max)
set #Days = '-7'
select
dateadd(hour,datepart(hour,Timestamp + GETDATE() - GETUTCDATE()),cast(CAST((Timestamp + GETDATE() - GETUTCDATE()) as date) as datetime)) as [Time]
from [Employee]
where dateadd(hour,datepart(hour,Timestamp + GETDATE() - GETUTCDATE()),cast(CAST((Timestamp + GETDATE() - GETUTCDATE()) as date) as datetime)) >= DATEADD(day,' + #Days + ', GETDATE()))
I want the user to select the number of days(#Days) of data he wants. So if he wants the data for last 15 days, all he has to do is set #Days = '-15'. Timestamp is the date along with time Column in my Employee table but Timestamp is UTC. I have written the query above and it is returning some data but I am confused if the query is correct or not?
I think the following simple query should do the trick.
declare #Days INT = -7; --<-- Use int not varchar
SELECT *
FROM [Employee]
WHERE CAST([Timestamp] AS DATE) >= CAST(DATEADD(day, #Days, GETUTCDATE()) AS DATE);
DATEADD() function's 2nd parameter is an int, you can pass the variable #Days to the function as it is.

Concatenate string with numeric and convert into datetime

Working in SQL Server, I have a column that contains a year in numeric format. I need to make that year into a January 1st date of that 'year'. I've tried a few commands and the latest attempt is:
cast('01/01/' + X.[YEAR] as datetime)
What am I missing?
DECLARE #Year INT = 2010
SELECT CAST(CAST(#Year AS varchar) + '-1-1' AS DATETIME) -- 2010-01-01
Another way:
select GETDATE(),
DATEADD (day, - DATEPART(dayofyear, GETDATE()) + 1, CONVERT(date, GETDATE()))

How to convert string YYYYMM to datetime in sql?

I have a string '2016-03-05' which I want convert to a datetime.
Here is my code:
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 112).
Running that I receive the error
Conversion failed when converting datetime from character string.
Conversion format 112 you've used assumes input '20160305' (without dashes as date parts separator).
So either do
select convert(Datetime, '20160305', 112)
or (if your input really contains dashes then just remove them like:
select convert(Datetime, replace('2016-03-05', '-', ''), 112)
That's because the style you're passing to the CONVERT function does not support that format. You can do two things...
1- Drop the dashes
Declare #period as nvarchar(10)
Set #period = '20160305' -- I've dropped the dashes here
Select Convert(Datetime, #period, 112)
2- Change the style to something that supports this format
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 21) -- I've changed the style here
For a complete reference, read the MSDN documentation
SELECT LEFT(201605,4) + '-' + RIGHT(201605,2)
{OR}
SELECT RIGHT(201605,2) + '-' + LEFT(201605,4)
Try this it will work I guess according to your need
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
select CAST(#period as datetime)

Resources