converting to date time with only date and hour passed in - sql

I am using SQL server, i want to insert datetime into my datetime column. The parameters i recieved as follows
#hour int,
#date varchar(10) -- format YYYY-MM-DD
How do I convert to this datetime format:
YYYY-MM-DD HH:MM:SS
MM will be 00 as we only consider the hour.
HH:MM will be in 24 hours format
Example :
#date = '2014-10-2'
#hour = '8'
Should be converted to
2014-10-2 08:00:00
How do I do this?

I consider that both the variables are of varchar type since you enclosed both the variables with quotes
SELECT CONVERT(DATETIME, #date + ' ' + convert(varchar(10),#hour) + ':00:00')

here is one another way to do this:-
declare #hour int=8,
#date varchar(10)='2014-10-2'
select dateadd(hh,#hour,CONVERT(datetime,#date))

Related

is it possible to convert date format from encrypted date string in MSSQL

I want to return date from a table.
The value of the startDate format is : 1588244766682
When I execute the query :
select CAST(startDate AS DATE) , CAST(GETDATE() AS DATE) from arch_report;
I got an error :
Explicit conversion from data type numeric to date is not allowed.
Is it possible to convert this type of date to date type?
I may miss some parts.
Any help would be appreciated.
Thanks in advance
You could use DATEADD to calculate the date.
We'll divide by 1000 (since your startDate value is in milliseconds) to get the number of seconds since the unix epoch.
declare #startTime bigint;
set #startTime = 1588244766682;
SELECT DATEADD(second, #startTime/1000, '1970-01-01') AS start_time;

How to convert date from Germany to local date in sql?

I have a column in a table with time stamps from Germany (Utc + 6) in their local time (Utc -1). How do I convert all those datetimes in my local time?
I think the correct way of converting UTC datetime into local datetime is using CLR function. You can find an example below.
https://www.mssqltips.com/sqlservertip/2339/converting-utc-to-local-time-with-sql-server-clr/
There are -7 hour between germany and central time zone in us
CT (-6)
Berlin (+1)
If your column is datetime USE THE FOLLOWING
Select DATEADD(HOUR,-7,[DATECOLUMN])
Update 1
Consider that #bdate and #edate are the begin and end date of the daylight savings so you can use this query
Select case when DATEADD(HOUR,-7,[DATECOLUMN]) between #bdate and #edate Then
DATEADD(HOUR,-8,[DATECOLUMN]) else
DATEADD(HOUR,-7,[DATECOLUMN]) end
If your data is in a string format like "dd.mm.yy," then you would use the code page 104 to tell SQL how to parse your date from string to a DateTime format.
CONVERT(DateTime, DateField, 104)
If your string is in some other format, then look up the correct codepage in the table here (CAST and CONVERT in T-SQL):

Converting datetime variable format

I need help in converting a DateTime variable with format mmm dd yyyy
to a Datetime variable with format yyyy-mm-dd
How about this?
Select CAST('Sep 07 2016' AS DATE)
Returns
2016-09-07
I'm assuming your datetime variable is a VARCHAR - this can be directly cast via CONVERT:
Declare #YourVariable Varchar (15) = 'JAN 01 1996'
Select Convert(Date, #YourVariable)
1996-01-01

How to assign current date with specific time to column?

How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/

Time format hhmm to hh:mm sql server 2005

I want to find the time difference so i am trying to use DATEDIFF option.
But the problem is i am having the time values in hhmm format(eg: 1715). So it showing out of range error. so i want to convert it to hh:mm format. How to convert hhmm to hh:mm format?
Have you tried something like
DECLARE #TimeVal VARCHAR(4)
SELECT #TimeVal = '1715'
select CONVERT(DATETIME,LEFT(#TimeVal,2) + ':' + RIGHT(#TimeVal,2),8)
Output
1900-01-01 17:15:00.000