Problem in Convert mm/dd/yyyy To dd/mm/yyyy - sql

I need to have same result for these two queries with format: 'dd/mm/yyyy
what should I do?
DECLARE #Date varchar(10) = '01/02/1958'
SELECT
CASE
WHEN TRY_CONVERT(Date, #Date, 103) IS NOT NULL
THEN CONVERT(Date, #Date, 103)
ELSE TRY_CONVERT(Date, #Date)
END
DECLARE #Date2 Date = '01/02/1958'
SELECT
CASE
WHEN TRY_CONVERT(Date, #Date2, 103) IS NOT NULL
THEN CONVERT(Date, #Date2, 103)
ELSE TRY_CONVERT(Date, #Date2)
END

Since you are using 2 types of date in your strings, when you write DECLARE #Date2 Date = '01/02/1958', you are saying 'I whant to convert that string in a date based on my server configuration'.
With that :
DECLARE #Date varchar(10) = '01/02/1958'
SELECT
CASE
WHEN TRY_CONVERT(Date, #Date, 103) IS NOT NULL
THEN CONVERT(Date, #Date, 103)
ELSE TRY_CONVERT(Date, #Date)
END
DECLARE #Date2 Date = '01/02/1958'
SET #Date2 = CONVERT(Date,(CONVERT(VARCHAR,#Date2,101)),103);
SELECT
CASE
WHEN TRY_CONVERT(Date, #Date2, 103) IS NOT NULL
THEN CONVERT(Date, #Date2, 103)
ELSE TRY_CONVERT(Date, #Date2)
END
I'm saying 'Ok, convert the #Date2 from US to EU convention. When manipulating Date formats, be sure to always cast your string with a correct format

Related

DateDiff In months between two different types date formats

I have two date formats, EndDate in YYYYMMDD format and another Monthfirstdate in YYYY-MM-DD FORMAT. I need to get the difference between these two dates.
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE #Date DATE = dbo.value('DATE')
SELECT
E.EmployeeID
,E.StartDateKey
,E.EndDateKey
,E.FromDateKey
,E.ToDateKey
INTO #Employee
FROM
dbo.Employee E
---clean--
DECLARE #StartDate DATE = (SELECT DATEADD(YY, DATEDIFF(YY, 0, #Date) - 1, 0))
DECLARE #EndDate DATE = DATEADD(S, -1, DATEADD(MM, DATEDIFF(MM, 0, #Date) + 3, 0))
SELECT
E.EmployeeID
,CAST(CONVERT(VARCHAR(10), E.StartDateKey, 112) AS INT) AS _EmployeeStartDateKey
,CAST(CONVERT(VARCHAR(10), E.EndDateKey, 112) AS INT) AS _EmployeeFinishDateKey
,CAST(CONVERT(VARCHAR, D.MonthFirstDay, 112) AS INT) AS _DateKey
,???? AS [Tenure]
FROM #Employee AS E
INNER JOIN dbo.Date D ON
D.DateKey BETWEEN E.FromDateKey AND E.ToDateKey
WHERE
D.DateKey BETWEEN #tartDate AND #EndDate
AND D.DayOfMonth = 1
You can try below query in SQL Server:
DECLARE
#firstDate DATETIME = '2022-12-01',
#dateinStringFormat VARCHAR(20)= '20121201',
#endDate DATETIME;
--Converting date string to Datetime format(YYYY-MM-DD)
SELECT #endDate = CONVERT(CHAR(10), CONVERT(datetime, #dateinStringFormat), 120);
--Now, Calculating the date difference in Month
SELECT DATEDIFF(MONTH, #endDate, #firstDate) AS DateDiff;

Why Week conversion failed in SQL Server in my case?

I am trying to convert week of the date based on my criteria.
My date condition: if my #date is less than 4 AM, then #date - 1, else #date
declare #dates datetime
set #dates = '2019-01-01 03:59:59'
select
case
when convert(varchar(26), #dates, 108) <= '04:00:00'
then convert(varchar, dateadd(day, -1, #dates), 103)
else convert(varchar, #dates, 103)
end BusinessDate
Output:
31/12/2018 // as expected
Now I want to find the week number of the output. So I tried
declare #dates datetime
set #dates = '2019-01-01 03:59:59'
select
case
when convert(varchar(26), #dates, 108) <= '04:00:00'
then convert(varchar, dateadd(day, -1, #dates), 103)
else convert(varchar, #dates, 103)
end BusinessDate,
case
when convert(varchar(26), #dates, 108) <= '04:00:00'
then datepart(week, convert(datetime, convert(varchar, dateadd(day, -1, #dates), 103)))
else datepart(week, convert(datetime, convert(varchar, #dates, 103)))
end weeks
But I get this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Just subtract four hours:
select datepart(week,
dateadd(hour, -4, #dates)
)

Error converting a value of datetime to mm/dd/yyyy format

I am trying to retrieve a datetime value by converting it into just Date format like mm/dd/yyyy using the following query snippet
CASE
WHEN co.RequestID is not NULL
THEN CONVERT(VARCHAR(10), DateFrom, 101)
WHEN nis.[867_key] is not NULL
THEN CONVERT(VARCHAR(10), nis.ServicePeriodStart, 101)
ELSE CONVERT(VARCHAR(10), q2.ServicePeriodStart, 101)
END as READ_START
But for some reason it gave me some other format which is like yyyymmdd which is not what I wanted. I even tried replacing NVARCHAR(12) with VARCHAR(10) and datetime but it didn't work as intended. I think I have made the conversion properly and I suppose issue is with using Convert in CASE WHEN statement
I think the problem is with column.So Column is varchar first of all you need to convert it into Date Time
see example
DECLARE #Date VARCHAR(10) = '2014-08-06'
SELECT CONVERT(CHAR(20),'2014-08-06',101)
RESULT 2014-08-06
SELECT CONVERT(CHAR(20),CAST('2014-08-06' as DATETIME),101)
RESULT 08/06/2014
your query
CASE WHEN co.RequestID is not NULL
THEN CONVERT(varchar(20),CAST(DateFrom AS DATETIME),101)
WHEN nis.[867_key] is not NULL
THEN CONVERT(varchar(12),CAST(nis.ServicePeriodStart AS DATETIME),101)
ELSE CONVERT(varchar(20),CAST(q2.ServicePeriodStart AS DATETIME) ,101)
END as READ_START
This Worked Fine
CASE WHEN
co.RequestID is not NULL
THEN
CONVERT(varchar(2), DATEPART(MONTH, DateFrom))
+'/'+ CONVERT(VARCHAR(2),DATEPART(DAY, DateFrom))
+'/'+CONVERT(VARCHAR(4),DATEPART(YEAR, DateFrom))
WHEN
nis.[867_key] is not NULL
THEN
CONVERT(varchar(2), DATEPART(MONTH, nis.ServicePeriodStart))
+'/'+ CONVERT(VARCHAR(2),DATEPART(DAY,nis.ServicePeriodStart))
+'/'+CONVERT(VARCHAR(4),DATEPART(YEAR, nis.ServicePeriodStart))
ELSE
CONVERT(varchar(2), DATEPART(MONTH, q2.ServicePeriodStart))
+'/'+ CONVERT(VARCHAR(2),DATEPART(DAY,q2.ServicePeriodStart))
+'/'+CONVERT(VARCHAR(4),DATEPART(YEAR, q2.ServicePeriodStart))
END as READ_START

SQL date format conversion from INT(yyyymmdd) type to date(mm/dd/yyyy)

I have a table with datekey column values(20120728,20120728...) in format of yyyymmdd as int type, I need to make them into date format of mm/dd/yyyy while writing select statement.
DECLARE #date int;
SET #date = 20131107
SELECT CONVERT(date, CONVERT(varchar(8), #date), 112)
Please Try This
DECLARE #date int;
SET #date = 20120728
SELECT CONVERT(varchar(20), CONVERT(date, CONVERT(varchar(8), #date), 112),110)as datetime

SQL convert DATETIME TO VARCHAR?

I am working on a query something require DATE!!
DECLARE #YesterDay DATETIME, #Today DATETIME
SET #YesterDay = DateAdd(DD, DateDiff(DD, 0, GETDATE())-1, 0)
SET #Today = DateAdd(DD, DateDiff(DD, 0, GETDATE()), 0)
select #YesterDay = convert(varchar, getdate()-1 , 110)
select #Today = convert(varchar, getdate() , 110)
EXEC #return_value = [dbo].[post_sec_admin_list_user_log]
#pDateFr = #YesterDay ,
#pDateTo = #Today,
#pName = '',
#pSec = NULL
#DateFr is varchar(50)
#DateT0 is varchar(50)
the #dateFr and #dateTo are both varchar..
And I try to execute it, it print the time format as this 2011-06-09 16:15:38.927
error statement
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Additionally, the varchar format I need is MM-DD-YYYY
Anyone know where is my error at?
thanks
Your code is confusing:
DECLARE #YesterDay DATETIME, #Today DATETIME
SET #YesterDay = DateAdd(DD, DateDiff(DD, 0, GETDATE())-1, 0)
SET #Today = DateAdd(DD, DateDiff(DD, 0, GETDATE()), 0)
select #YesterDay = convert(varchar, getdate()-1 , 110)
select #Today = convert(varchar, getdate() , 110)
So you declare it as DATETIME, set value with DateDiff then overwrite that value with an varchar representation of a date that is recalculated using different method.
At line 4 and 5 #Yesterday and #Today variables are still DATETIME, because it's declared that way.
EDIT: As mentioned in the comment to my answer you need a variable to pass to the procedure.
So the correct fix would be to declare the variables as VARCHAR(50) at the beginning and do the conversion directly.
DECLARE #YesterDay VARCHAR(50), #Today VARCHAR(50)
SELECT #YesterDay = convert(varchar(50), dateadd(dd, -1, getdate()) , 110)
SELECT #Today = convert(varchar(50), getdate() , 110)
And then call the procedure the way you did originally.
My guess is that getdate()-1 part is wrong.
Also, you were making dateadd and datediff why? Try it like this:
SET #YesterDay = dateadd(dd, -1, GETDATE())
SET #Today = GETDATE()
select #YesterDay = convert(varchar(50), dateadd(dd, -1, getdate()) , 110)
select #Today = convert(varchar(50), getdate() , 110)
EXEC #return_value = [dbo].[post_sec_admin_list_user_log]
#pDateFr = #YesterDay ,
#pDateTo = #Today,
#pName = '',
#pSec = NULL