Add a Specific Time to a Date Value - sql-server

I have a stored procedure that receives a date value like this:
#AsOf date
In the query I am using this date to filter a datetime field. I need to convert the date passed in to a datetime field with the time portion set to 09:00:00 PM so I can add a filter in the WHERE clause like this:
(DateCreated < #TimeAsOfNinePM)
I know this is easy, but it's just escaping me at the moment. I'm using SQL Server 2008.

Try this out:
DECLARE #TimeAsOfNinePM datetime
DECLARE #AsOf date
SET #AsOf = '2010-10-01'
SET #TimeAsOfNinePM = dateadd(hh,21, cast(#AsOf as datetime))
PRINT #TimeAsOfNinePM
The trick is you need to convert the date datatype to a datetime datatype to add hours to it.

Related

Converting a varchar date to date datatype in mssql

My column is storing value as 201909(i.e September 2019), I need to convert it to format 2019-09-01 and store in it another column in a MSSQL .Please help
You can add a statinc '01' string with your current string and then try to convert as below-
DECLARE #D VARCHAR(20)
SET #D = '201909'
SELECT CAST(#D+'01' AS DATE)
Output is-
2019-09-01

SQl query datetime from nvarchar error

In SQL,
declare #date datetime ='Jul15'
select CONVERT(varchar(10),#date,105)
getting error
Conversion failed when converting date and/or time from character
string.
Please help
It is because Jul15 is not a valid SQL Server Date or DateTime value, hence the error when it is trying to convert it to date/datetime to assign it to a datetime variable.
Ideally you should be using ANSI-Standard when working with date or datetime values in SQL Server, ANSI-Standard Date value has the format of YYYYMMDD, therefore your statement should look something like this.....
declare #date datetime ='20150701'
select CONVERT(varchar(10),#date,105)

Why can't I use a datetime parameter in ssrs?

I have an SSRS Date/Time parameter generated from a shared dataset query against a SQL Server datetime field. The parameter displays correctly in a report textbox but it will not work in an embedded dataset query, even against the same table that the datetime value was generated from.
In order to use the parameter for a dataset query I have to parse both sides of a where clause to get it to work in Preview in SSDT:
(convert(varchar,invoice.snapshot_datetime,120)) = (convert(varchar,#snapshotdatetime,120))
This is tremendously inefficient.
How can I get my where clause to work without parsing the invoice.snapshot_datetime column?
Server Details
The SQL Server Language is English (United States).
SQL Server dateformat is mdy (from dbcc useroptions).
Getdate() returns '2015-05-20 10:27:56.687' in SSMS
Assuming your date range is between 1900-01-01 and 2079-06-06 you can cast to SmallDateTime to truncate the seconds out of your datetime variable:
DECLARE #DateTime datetime
SET #DateTime = CAST(CAST(#snapshotdatetime as SmallDateTime) as DateTime)
(thanks to t-clausen.dk for his answer here)
Now, since your actual column is of type DateTime, it does keep seconds (and milliseconds), and you will need to eliminate them as well.
However, using functions on your column will prevent the SQL Server from using any indexes you might have on this column, so a better approach would be to use a DateTime range:
DECLARE #FromDateTime datetime, #ToDateTime datetime
SET #FromDateTime = CAST(CAST(#snapshotdatetime as SmallDateTime) as DateTime)
Since the cast will round the minutes of the small date time up if it's over 29.998 seconds, and down if it's below 29.999 seconds. You always want to round down since it's From datetime, you need to cheke if you need to decrease a minute:
IF datepart(second, #snapshotdatetime) > 29
OR (datepart(second, #snapshotdatetime) = 29
AND datepart(millisecond, #snapshotdatetime) > 998)
SET #FromDateTime = DATEADD(minute, -1, #FromDateTime)
SET #ToDateTime = DATEADD(minute, 1, #FromDateTime)
and then, in your where clause, use this:
invoice.snapshot_datetime <= #FromDateTime
AND invoice.snapshot_datetime >= #ToDateTime
If you haven't found solution yet, try this:
select (convert(varchar,GETDATE(),112))
it will return 20180206 (yyymmdd)

How to turn DD/MM/YYYY 00:00:0000 to MM/YYYY and keep it as date format and not string

How to turn DD/MM/YYYY 00:00:0000 to MM/YYYY and keep it as date format and not string in MS SQL
i have tried many options but still did not find the right one.
Simply you cannot keep date as mm/yyyy as a date/datetime. But you could keep it as a string and also convert when needed back to a date as below.
Sql-Server Fiddle Example
declare #dt datetime = getdate(),
#mmyyyy varchar(7)
--To format as mm/yyyy
select #mmyyyy = right(convert(varchar(10), #dt, 103),7)
--To convert back to datetime type from mm/yyyy varchar
select #mmyyyy [mm/yyyy],
convert(datetime,'01/'+ #mmyyyy,103) back_To_Date

T-SQL: How to update just date part of datetime field?

In SQL Server 2008 I need to update just the date part of a datetime field.
In my stored procedure I receive the new date in datetime format. From this parameter I have to extract the date (not interested in time) and update the existing values date part.
How can I do this?
One way would be to add the difference in days between the dates to the old date
UPDATE TABLE
SET <datetime> = dateadd(dd,datediff(dd,<datetime>,#newDate),<datetime>)
WHERE ...

Resources