SQL Server date time conversion issue - sql-server

Below is the SQL server query I am using,
SELECT
*,
CONCAT(CONVERT(date, startdate, 104), RIGHT(+' '+ CONVERT(VARCHAR, startTime), 8)) AS NewDate,
CONCAT(CONVERT(date, '7.8.2018', 104), RIGHT(+' '+ CONVERT(VARCHAR, '3:51:23'), 8)) AS NewDate2,
CASE
WHEN CONCAT(CONVERT(date, startdate, 104), RIGHT(+' '+ CONVERT(VARCHAR, startTime), 8)) < CONCAT(CONVERT(date, '7.8.2018', 104), RIGHT(+' '+ CONVERT(VARCHAR, '3:51:23'), 8))
THEN '1'
ELSE '0'
END AS comment
FROM
[dbo].[LMT2_ServerLicenseUser]
WHERE
CONCAT(CONVERT(date, startdate, 104), RIGHT(+' '+ CONVERT(VARCHAR, startTime), 8)) > CONCAT(CONVERT(date, '7.8.2018', 104), RIGHT(+' '+ CONVERT(VARCHAR, '3:51:23'), 8))
and the output is shown below in the screenshot:
I am comparing columns 'NewDate' and 'NewDate2' here and if the condition is true then assigning value in a new column called 'comment' which is showing wrong values.
Please help me to understand where is the issue is it a date time conversions issue or something else.

You can try use to cast(startdate as datetime) + cast(startTime as datetime) let the column Convert to DateTime Type then compare, becasue you can't compare string DateTimeformat.
Select *
,CONCAT( CONVERT(date, startdate, 104), RIGHT(+' '+ CONVERT(VARCHAR,startTime),8)) AS NewDate
,CONCAT( CONVERT(date, '7.8.2018', 104), RIGHT(+' '+ CONVERT(VARCHAR,'3:51:23'),8)) AS NewDate2
,case
cast(startdate as datetime) + cast(startTime as datetime) > cast('7.8.2018' as datetime) + cast('3:51:23' as datetime)
then '1'
else '0'
end as comment
from [dbo].[LMT2_ServerLicenseUser]

Related

Remove the time part in Datediff

This is my query:
SELECT DISTINCT
'wk ' + TRIM(CONVERT(varchar(max), DATEADD(Week, DATEDIFF(week, 0, CONVERT(VARCHAR(max), period)), -1))) [week]
FROM
ABC
GROUP BY
TRIM(CONVERT(varchar(max), DATEADD(Week, DATEDIFF(week, 0, CONVERT(VARCHAR(max), period)), -1)))
I get a result like this: wk Aug 7 2022 12:00AM
But I want this format wk Aug 7, 2022
There was an error in the answer, it always returned january, the error is fixed now
You could build your string using the datepart function
declare #period datetime = '20220807' -- getdate()
SELECT 'wk ' +
left(datename(month, #period), 3) +
' ' + convert(varchar(2), datepart(day, #period)) +
' ,' + convert(varchar(4), datepart(year, #period))
looks like this wk Aug 7 ,2022
To convert your select clause to use this,
declare #table1 table (id int, period date)
insert into #table1 (id, period) values (1, '20220807'), (2, '20211130')
SELECT 'wk ' +
left(datename(month, dateadd( week, DATEDIFF( week, 0, CONVERT(VARCHAR(12), convert(date, period))), -1)), 3) +
' ' + convert(varchar(2), datepart(day, dateadd( week, DATEDIFF( week, 0, CONVERT(VARCHAR(12), convert(date, period))), -1))) +
' ,' + convert(varchar(4), datepart(year, dateadd( week, DATEDIFF( week, 0, CONVERT(VARCHAR(12), convert(date, period))), -1)))
from #table1
it looks like this
COLUMN1
wk Aug 7 ,2022
wk Nov 28 ,2021

Use MAX(Date) value inside a SQL recursion

I have SQL query with performs recursion based on selected Min(Date) and Max(Date) from more than 1 table. But when i try to run the query it throws GROUP BY, HAVING, or aggregate functions are not allowed in the recursive part of a recursive common table expression 'ctedaterange'. exception. I also tried using Top 1 Date but still no hope.
SQL Query
ALTER VIEW [dbo].[CYExtraction]
AS
WITH ctedaterange
AS (SELECT
[Dates] = (SELECT
MIN(CreatedOn) AS CreatedOn
FROM (SELECT
MIN(CreatedOn) AS CreatedOn
FROM (SELECT
MIN(CreatedOn) AS CreatedOn
FROM Items) AS it
UNION ALL
SELECT
*
FROM (SELECT
MIN(CreatedOn) AS CreatedOn
FROM Bibs) AS bib
UNION ALL
SELECT
*
FROM (SELECT
MIN(CreatedOn) AS CreatedOn
FROM Porders) AS po) AS AllItems)
UNION ALL
SELECT
[dates] + 1
FROM ctedaterange
WHERE [dates] + 1 <= (SELECT
MAX(CreatedOn) AS CreatedOn
FROM (SELECT
MAX(CreatedOn) AS CreatedOn
FROM (SELECT
MAX(CreatedOn) AS CreatedOn
FROM Items) AS it
UNION ALL
SELECT
*
FROM (SELECT
MAX(CreatedOn) AS CreatedOn
FROM Bibs) AS bib
UNION ALL
SELECT
*
FROM (SELECT
MAX(CreatedOn) AS CreatedOn
FROM Porders) AS po) AS AllItems))
SELECT
[Dates] AS PK_Date,
DATENAME(MONTH, [Dates]) + ' ' + CAST(YEAR([Dates]) AS varchar(4)) AS
Month,
CASE
WHEN DATEPART(m, [Dates]) <= 3 THEN 'Q1 ' + CAST(YEAR([Dates]) AS varchar(4))
ELSE CASE
WHEN DATEPART(m, [Dates]) <= 6 THEN 'Q2 ' + CAST(YEAR([Dates]) AS varchar(4))
ELSE CASE
WHEN DATEPART(m, [Dates]) <= 9 THEN 'Q3 ' + CAST(YEAR([Dates]) AS varchar(4))
ELSE CASE
WHEN DATEPART(m, [Dates]) <= 12 THEN 'Q4 ' + CAST(YEAR([Dates]) AS varchar(4))
ELSE ''
END
END
END
END AS 'Quarter',
CASE
WHEN DATEPART(m, [Dates]) <= 6 THEN 'HY1 ' + CAST(YEAR([Dates]) AS varchar(4))
ELSE CASE
WHEN DATEPART(m, [Dates]) <= 12 THEN 'HY2 ' + CAST(YEAR([Dates]) AS varchar(4))
ELSE ''
END
END AS 'HalfYear',
YEAR([Dates]) AS 'Year',
CAST('1-' AS varchar(5)) + CAST(DATEPART(M, [Dates]) AS varchar(10)) + '-' + CAST(YEAR([Dates]) AS varchar(4)) AS DateName
FROM ctedaterange
GO
The main thing is that I need to wrap it inside my SQL Views. Any help to my problem will be appreciated.
Thanks,
You can move the aggregations to an extra cte, so this might be what you want:
ALTER VIEW [dbo].[CYExtraction]
(PK_Date, [Month], [Quarter], [HalfYear], [Year], [DateName])
AS
WITH cteAllItems (mindates, maxdates) AS (
SELECT MIN(CreatedOn), MAX(CreatedOn) FROM Items
UNION ALL
SELECT MIN(CreatedOn), MAX(CreatedOn) FROM Bibs
UNION ALL
SELECT MIN(CreatedOn), MAX(CreatedOn) FROM Porders
),
ctedaterange (Dates, MaxDate) AS (
SELECT MIN(mindates), MAX(maxdates)
FROM cteAllItems
UNION ALL
SELECT DATEADD(day, 1, Dates), MaxDate
FROM ctedaterange
WHERE DATEADD(day, 1, Dates) <= MaxDate
)
SELECT
Dates AS PK_Date,
DATENAME(MONTH, Dates) + N' ' + DATENAME(YEAR, Dates),
N'Q' + DATENAME(QUARTER, Dates) + N' ' + DATENAME(YEAR, Dates),
N'HY' + CAST((MONTH(Dates)-1)/6+1 AS nvarchar(1)) + N' ' + DATENAME(YEAR, Dates),
YEAR(Dates),
CONVERT(nvarchar(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, Dates), 0), 105)
FROM ctedaterange
GO

SQL server date time

I am trying to get object which its start date + start time have passed or equal to current datetime and its end date + end time hasn't pass the current datetime. I tried convert current datetime to nvarchar as all my columns are nvarchar. I also tried converting my columns to datetime type but it doesn't work too. Please help
Column:
startDate - nvarchar (dd/mm/yyyy)
startTime - nvarchar (hh:mm) 24 hr
endDate - nvarchar(dd/mm/yyyy)
endTime - nvarchar(hh:mm) 24hr
i did try to but datetime betwween startDate + startTime AND endDate +startTime,
but it doesn't seems to work :
SELECT * FROM Promo
WHERE membership = '1'
AND promoStatus = '1'
AND CONVERT(NVARCHAR, GetDate(), 101) + ' ' +
CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2) BETWEEN
startDate + ' ' + startTime
AND endDate + ' ' + endTime
i did try other method too :
SELECT * FROM Promo
WHERE membership = '1'
AND promoStatus = '1'
AND startDate + ' ' + startTime <= CONVERT(NVARCHAR, GetDate(), 101) + ' ' +
CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2)
AND endDate + ' ' + endTime >= CONVERT(NVARCHAR, GetDate(), 101) + ' ' +
CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2)
You could do it like this:
SELECT *
FROM Promo
WHERE
membership = '1'
AND promoStatus = '1'
AND CAST(CONVERT(VARCHAR(16), GETDATE(), 120) AS DATETIME) BETWEEN
CONVERT(DATETIME, #startDate, 103) + CONVERT(DATETIME, #startTime)
AND CONVERT(DATETIME, #endDate, 103) + CONVERT(DATETIME, #endTime)
This line:
CAST(CONVERT(VARCHAR(16), GETDATE(), 120) AS DATETIME)
gets the current date time up to its minutes, meaning this will strip off the seconds and milliseconds part of the GETDATE().
And this line:
CONVERT(DATETIME, #startDate, 103) + CONVERT(DATETIME, #startTime)
will combine your date and time variables to form a new DATETIME value.
Note that you could have done away with with this if you design the tables using the proper data types.
Try this
SELECT * FROM Promo
WHERE membership = '1'
AND promoStatus = '1'
AND CAST(CONVERT(VARCHAR(16), GetDate(), 120) AS DATETIME)
BETWEEN CONVERT(DATETIME, startDate + ' ' + startTime, 120)
AND CONVERT(DATETIME, endDate + ' ' + endTime, 120)

Comparing DateTIme Column value with DateTime String SQL Server

I am trying to compare DateTime string to a DateTime Value Column in DB but its returning me 0 records Here is the query
DECLARE #p_date DATETIME
SET #p_date= CONVERT( DATETIME, '9/1/2015 10:06:22 PM', 131 )
SELECT UpdateUserId, UpdateTimeA
From SubmitSheets
WHERE CONVERT( DATETIME, UpdateTimeA, 131 ) = #p_date
the value in UpdateTimeA column is 2015-09-01 22:06:22.447
Like I said in my comment, I'm not sure 131 is the right style to convert to datetime (at least, I get a very different value).
If you want to compare dates without the second fraction, you'll have to compare as strings. Converting both dates to a varchar(19) will cut off the fractions.
Finally found a way
SELECT UpdateUserId,UpdateTimeA
From SubmitSheets
WHERE CONVERT(VARCHAR(16),UpdateTimeA,100) = CONVERT(VARCHAR(16),CONVERT( DATETIME, '9/1/2015 10:06:22 PM',101),100)
I think the #p_date parameter is not in same format. Try this:
DECLARE #p_date DATETIME
SET #p_date= CONVERT( DATETIME, '9/1/2015 10:06:22 PM')
SELECT UpdateUserId, UpdateTimeA
From SubmitSheets
WHERE CONVERT(DATETIME, (CONVERT(VARCHAR, UpdateTimeA, 101) + ' ' + CONVERT(VARCHAR, UpdateTimeA, 108)), 131) = CONVERT(DATETIME, #p_date, 131)
So another tryout may be
An example:
DECLARE #a TABLE
(
ID int,
datet datetime
)
INSERT INTO #a
(ID, datet)
VALUES (1 , CONVERT(DATETIME, GETDATE(), 131))
,(2 , CONVERT(DATETIME, GETDATE(), 131))
,(3 , CONVERT(DATETIME, GETDATE(), 131))
,(4 , CONVERT(DATETIME, GETDATE(), 131))
,(5 , CONVERT(DATETIME, GETDATE(), 131))
,(6 , CONVERT(DATETIME, GETDATE(), 131))
,(7 , CONVERT(DATETIME, GETDATE(), 131))
,(8 , CONVERT(DATETIME, GETDATE(), 131))
,(9 , CONVERT(DATETIME, GETDATE(), 131))
SELECT ID, datet, ConvDatetime
FROM
(
SELECT ID, datet
,CONVERT(DATETIME, (CONVERT(VARCHAR, datet, 101)
+ ' '
+ CONVERT(VARCHAR, datet, 108)), 131) AS ConvDatetime
FROM
#a
) AS dt
WHERE
dt.ConvDatetime= CONVERT(DATETIME,
(CONVERT(VARCHAR, GETDATE(), 101)
+ ' '
+ CONVERT(VARCHAR, GETDATE(), 108))
, 131) -- CONVERT( DATETIME, '9/1/2015 10:06:22 PM', 131 )

Getting Error when converting to datetime :Date Column + Time Column

Below is my sql query, Inner query returning the value but outer query returning error:
"Conversion failed when converting character string to smalldatetime data type. "
SELECT x.*
FROM (
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, Event_Date, 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), Convert(TIME, Event_Time)), 101)
,[Event_Date]
,Location
,Street
,City
,[State]
,Country
,ZipCode
,[Subject]
,[Detail]
,[LeadInstructor]
,[CoInstructor]
FROM [Temp_EVT_EVENT]
) x
SMALLDATE can accept time value upto 18:00:00. But you used TIME format 18:00:00.000000 that is why it show error. Check the query and update the status.
First run this query separately:
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, CAST('2014-11-14' AS DATETIME), 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), CONVERT(VARCHAR(8), CAST('18:13:04' AS DATETIME), 108)), 101) AS column1
if you get the answer. '2014-11-14 18:13:00'
Then try to run:
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, CAST(Event_Date AS DATETIME), 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), CONVERT(VARCHAR(8), CAST(Event_Time AS DATETIME), 108)), 101) AS column1 FROM Temp_EVT_EVENT
if you get the answer correctly. Then Run:
SELECT x.*
FROM (
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, CAST(Event_Date AS DATETIME), 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), CONVERT(VARCHAR(8), CAST(Event_Time AS DATETIME), 108)), 101) AS column1
,[Event_Date]
,Location
,Street
,City
,[State]
,Country
,ZipCode
,[Subject]
,[Detail]
,[LeadInstructor]
,[CoInstructor]
FROM [Temp_EVT_EVENT]
) x
Basing on your Event_Date format, look here: Date Formats
It seems that the correct format is 120, and not 101.
So:
SELECT x.*
FROM (
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, Event_Date, 120)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), Convert(TIME, Event_Time)), 120)
,[Event_Date]
,Location
,Street
,City
,[State]
,Country
,ZipCode
,[Subject]
,[Detail]
,[LeadInstructor]
,[CoInstructor]
FROM [Temp_EVT_EVENT]
) x

Resources