Is there any way to use SOQL to strip the time out of a datetime column or to construct a date?
e.g. SELECT DATE('2001-12-31') would return December 31, 2001
In some kind, you can do this, i.e.,
User a = [Select createddate from user limit 1];
system.debug(a.createddate);
system.debug(a.createddate.format('MM/dd, yyyy ') );
in the debug log, got
|DEBUG|2010-12-13 00:41:39
|USER_DEBUG|[3]|DEBUG|12/13, 2010
It seems can't transfer 12 to December directly.
Related
I want to select all the records from database table whose date time(AppointmentDate column) is equal to current date time + 12 hours.
so basically the records having appointment date which are exactly after 12 hours from current datetime.
how can we write this query in linq to entites?
Thanks in advance.
Pretty straight forward:
var twelveHoursFromNow = DateTime.Now.AddHours(12);
db.Records.Where(m => m.AppointmentDate == twelveHoursFromNow);
However, the equal may be a bit restrictive. As DateTime.Now could be 12:45:30am causing you to discard an appointment at 12:45:00pm. More likely than not, you're going to want a range.
db.Records.Where(m => m.AppointmentDate > rangeStartDateTime && m.AppointmentDate < rangeEndDateTime);
In an SSRS report, the user searches based on start date and end date.
The challenge is, as I discovered recently, he sometimes, not always, provides the time component while searching.
Currently, the filter is done like this:
if #pEndDate is null
SET #pEndDate = getdate()
SET #PEndDate = DateAdd(dd,1,#PEndDate)
SELECT ........
FROM .....
WHERE ( Createdon >= #PStartDate AND Createdon < #PEndDate)
This is fine when he searches without time (example - #PStartDate = 2/23/2015 and #PEndDate = 2/24/2015)
How should I structure the query to deal with the time portion when he provides it? (example - #PStartDate = 2/23/2015 15:00 and #PEndDate = 2/24/2015 15:00)
If this is answered elsewhere, please point me to it. Thank you.
If you just want to match the date part then there are lot options.
1) You can use the Date type for the parameter PEndDate and PStartDate to nullify the time part
2) You can use the Convert method to get only date part of the parameter while matching.CONVERT (DATE, #PEndDate) OR CONVERT(varchar,#PEndDate,103)
3) Get Date Part only from DateTime using DateTime functions
ATEADD(dd, 0,
DATEDIFF(dd, 0, #PEndDate))
4) Get Date Part only from DateTime using FLOOR and CAST functions
CAST( -- Convert the integer to DATE
FLOOR(-- Get largest Integer less than or equal to the decimal value
CAST(GETDATE() AS DECIMAL(12, 5)) -- Convert DATETIME to DECIMAL)
AS DATETIME) 'Date Part Only'
5) Get Date Part only from DateTime using DATEPART and CONVERT functions
CONVERT(VARCHAR(4),DATEPART(YEAR, #GETDATE))
+ '/'+ CONVERT(VARCHAR(2),DATEPART(MONTH, #GETDATE))
+ '/' + CONVERT(VARCHAR(2),DATEPART(DAY, #GETDATE))
'Date Part Only'
Use whichever method suits you and you find fancy.
UPDATE
As you mentioned you need to get the time part to 00:00 with date so you can try as,
SELECT CAST( convert(varchar(10),GETDATE(),112) AS DATETIME)
--This will give you 2015-02-27 00:00:00.000
SELECT DATEADD(ms,-3, DATEADD(day, DATEDIFF(day,0,GETDATE())+1,0))
--This will give you end of days time 2015-02-27 23:59:59.997
SELECT CONVERT(nvarchar,getdate(),103) + ' 12:59:59 PM'
--This will give you custom time 27/02/2015 12:59:59 PM
I have a list of tasks stored in an SQLite database with timestamp in GMT format as follows:
Timestamp: 2014-07-10 00:42:43
I am trying to get all 'todays tasks' but 'today' relevant to the timezone. (so the above Timestamp example in Eastern Standard time is actually a task from the July 9th not the 10th
My query:
"SELECT * FROM " + TABLE_TASKS + " WHERE date('timestamp','localtime') >= date('now', 'start of day','localtime')"
Doesn't select the tasks when the GMT time has rolled over to the next day even though in EST it is still july 9th.
why is this happening?
> create table t(timestamp);
> insert into t values (datetime('now'));
> select date('timestamp') from t;
> select date("timestamp") from t;
2014-07-10
> select date(timestamp) from t;
2014-07-10
In SQL, anything enclosed in single quotes is a string, and timestamp is not a date/time value in one of the supported formats.
If you wanted to quote an identifer (i.e., a table or column name), you would need to use double quotes.
However, timestamp is not a keyword, so you don't need to quote it at all.
I've searched this site and others, but couldn't find this exact scenario. I need to add a gridview to my page where the sqldatasource is based on a query that returns records that fall between Sunday and Saturday of the current week. (Each record has one date field) The records are for payroll purposes and the payroll week runs from Sunday to Saturday. I need to find all records that fall in the current pay week. Can anyone point me in the right direction to get started? I'm coding in VB.
This is a SQL question so you should tag it accordingly with your dbms.
Assuming you're using SQL-Server, DATEPART can be used to get the weekday as int of a datetime field and DATENAME can be used to get the name of the weekday.
For example(assuming Sunday to Saturday actually means from Monday to Friday):
SELECT t.*
FROM YourTable t
WHERE DATEPART(weekday, YourDateField) BETWEEN 2 AND 6
Note that it depends on regional settings what is the first day of the week.
Edit: If you want to select records from the current week.
SELECT t.*
FROM YourTable t
WHERE YourDateField >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
AND YourDateField < DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
http://msdn.microsoft.com/en-us/library/ms186819.aspx
I suppose you are searching a way to get the working time of an employee between two date
(from sunday to saturday). You need the exact date of Sunday before today.
This could be done via this function
Public Function FirstWeekDay(d as DateTime) As DateTime
While(d.DayOfWeek <> DayOfWeek.Sunday)
d.AddDays(-1)
End While
return d
End Function
The date returned is your startDate while your endDate depends where you want to stop your search.
According to your question this date is the following Saturday. Simply add 6 days to the startDate.
Now it's only a matter to pass the correct parameters to your query/storedprocedure:
SELECT w.* FROM workTime w WHERE w.empID = #empID AND w.workDate BETWEEN #startDate AND #endDate
when i'm inserting data to the sql server 2008 database through asp.net application date inserted to the database in the following format
"10 june 2011"
this is wrong and i need "dd/mm/yyyy" or "yyyy/mm/dd" format to be inserted.why does this happen
If your data type on the database side is either datetime or date (on sql server 2008), it shouldn't matter whether you insert the date as '10 june 2011' or as '6/10/2011' or as '2011-06-10'. If you see the data actually being displayed as '10 June 2011' that's more likely because your data type is varchar or nvarchar. If that's the case and the field is only meant to hold dates, I would advise you to change the data type to be actually a datetime or date.
Use DateTime.TryParseExact() method to parse the date.
string inputDate="10 June 2011";
string []format ={ "dd MMMM yyyy","dd MMM yyyy"};
DateTime date;
if (DateTime.TryParseExact(inputDate, format, CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
Console.WriteLine("Valid " + date);
}
Table column datatype was Nvarchar, so that it saves data in the above format which is wrong. By running the following I have solved my issue
SET DATEFORMAT DMY;
ALTER TABLE #tmpTest ALTER COLUMN MyCol DATE;