Convert nvarchar date to datetime - sql

I have dates stored in a sql server database as nvarchar but I need to create a report and pull out data from the last day base on the date.
I use this when the data type is a DateTime:
SELECT *
FROM [table]
WHERE timein >= DateAdd(hh, -24, GETDATE())
I think I need to convert the GETDATE() -24 to a string to compare it to the db
The format needs to be like this:
April-30-15
Can anyone help me create a query that will select records for the past 24 hours using this date format?

Convert your timeIn string to a date and compare using dates not strings. If you replace the hyphens with spaces it will be able to cast to a date. I assume you want values since the start of previous day (ignoring the current time) so I cast that to a date also.
SELECT *
FROM [table]
WHERE cast(replace(timein, '-', ' ') as date) >= cast(DateAdd(dd, -1, GETDATE()) as date)

Related

Converting Unix time to DateTime 103

I have a stored procedure that joins two tables of hotel booking data, however, the API that I pull my data from uses Unix time. I need to convert this to DateTime to match with my companies fields.
Currently, my conversion looks like this.
IIF([start] IS NOT NULL,
CONVERT(varchar(10), [start], 103),'') as 'ArrivalDate'
This just returns the value 1547310796 so no conversion has been done.
How do I convert the value to match 103 Date Time?
This should do it:
SELECT DATEADD(second, 1547310796 - DATEDIFF(second, GETDATE(), GETUTCDATE()), '1970-01-01')
The DATEDIFF(second, GETDATE(), GETUTCDATE()) part will give you how far behind you are from UTC time. You need to subtract that many seconds from the UTC timestamp to get the local time.
I just wanted to come back to this and add another answer in that works if you just want the date and not time.
SELECT cast(DATEADD(S,[start], '1970-01-01') as date) as 'ArrivalDate'
works on Oracle/Sql
select TO_date ('19700101000000','YYYYMMDDHH24MISS') + NUMTODSINTERVAL(<YOUR_COLUMN>+ decode (sessiontimezone, '+01:00', 3600, '+02:00', 7200, 0) , 'SECOND') as ArrivalDate

How to get timestamps correspond to the current days date? With Microsoft SQL

The number of rows in the A table, where the date value of the timestamps in the [time_created] and / or [time_updated] fields correspond to the current day's date
You can use the CONVERT() function.
CONVERT(date, YOUR-COL-NAME-HERE)
So if you are looking for rows with today's date, you should have something like:
WHERE CONVERT(date, YOUR-COL-NAME-HERE) = CONVERT(date, GETDATE())
GETDATE() returns the current date as a datetime. You can use convert to extract the date part from it:
SELECT *
FROM mytable
WHERE CONVERT (DATE, GETDATE()) IN
(CONVERT(DATE, time_created), CONVERT(DATE, time_updated))

How to compare current time to retrieve record stored in SQL

I have a database in which one of the column name is 'date' I need to count the number of entries that have been added with today's date after 09.00.00
So far I have done this..I am wondering where do I specify the time '09.00.00'
SELECT COUNT(*) AS total from DATABASENAME
WHERE date >= Convert(datetime, Convert(int, GetDate()))
Here's code that should work. You're essentially converting a datetime to a date to remove the time, then back to a datetime and adding in the hour that you want.
SELECT COUNT(*) AS Total
FROM TableName
WHERE Date >= DATEADD(HOUR, 9, CONVERT(DATETIME, CONVERT(DATE, GETDATE())))
Well is the date column store date and time or date only, if it is date only you need to add the time also in the column and also avoid converting data type in where condition try the above query with a parameter variable

Get tomorrows date

I am trying to get tomorrows date in a sql statement for a date comparison but it is not working.
Below is my code:
select *
from tblcalendarentries
where convert(varchar,tblcalendarentries.[Start Time],101)
= convert(varchar, GETDATE() +1, 101)
To get tomorrows date you can use the below code that will add 1 day to the current system date:
SELECT DATEADD(day, 1, GETDATE())
GETDATE()
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
DATEADD(datepart , number , date)
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
So adding this to your code in the WHERE clause:
WHERE CONVERT(VARCHAR, tblcalendarentries.[Start Time], 101) =
CONVERT(VARCHAR, DATEADD(DAY, 1, GETDATE()), 101);
First off, GETDATE() will get you today's date in the following format:
2013-04-16 10:10:02.047
Then using DATEADD(), allows you to add (or subtract if required) a date or time interval from a specified date. So the interval could be: year, month, day, hour, minute etc.
Working with Timezones?
If you are working with systems that cross timezones, you may also want to consider using GETUTCDATE():
GETUTCDATE()
Returns the current database system timestamp as a datetime value. The database time zone offset is not included. This value represents the current UTC time (Coordinated Universal Time). This value is derived from the operating system of the computer on which the instance of SQL Server is running.
Try the below:
SELECT GETDATE() + 1
This adds one day to current date
Specify size of varchar in convert()
where convert(varchar(11),tblcalendarentries.[Start Time],101) = convert(varchar(11), GETDATE() +1, 101)
I would write:
where
DATEADD(day,DATEDIFF(day,0,tblcalendarentries.[Start Time]),0) =
DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
This avoids converting dates to strings entirely, whilst also removing the time portion from both values.

SQL Server datetime in query

In an event management database I have list of all events with a field startdate
What I want to do is not to show events passed by an hour. Only events which have not passed by current date and time.
My date and time is in this format 2011-08-24 17:30:00.000
Any kind of help is appreciated
I read that as you want data where startdate is before a specific date;
select
*
from T
where startdate < dateadd(hour, -1, '2011-08-24 17:30:00.000')
SELECT *
From EventTable
WHERE startdate > DATEADD(HOUR, -1, GETDATE())
GetDate() - Returns the current database system timestamp as a
datetime value.
DATEADD(datepart , number , date ) - Returns a specified date
with the specified number interval (signed integer) added to a
specified datepart of that date.