I've been using the GetDate() function to populate a column in a table.
But it populates it with both the date and time: 2000-11-08 11:22:28
I'd like it to just put in the date: 2000-11-08
I've tried the Convert function as well as other functions, to no avail.
Some attempts have reversed month and day (I'm in Australia, so all PCs and Servers are set with a dd-mm-yy date format)
i want to use the date part of the function getdate in my sql query. the getdate function return the current date and time. i want only the date part. how can i do it.
I'm trying to return the month part of the current date, along with the month number. I want it to return:
01 - January 02 - February, etc
It's fine for October to December, but for the other nine months my code returns 1 - January and I want to be sure the leading 0 is added. How can I do this? Here is my current code:
CONVERT(nvarchar,MONTH(Getdate())) + ' - ' + DATENAME(MONTH,Getdate()) as MonthName
We need to select rows from the database that have been recently inserted/updated. We have a main primary table (COMMIT_TEST) and a second update table (COMMIT_TEST_UPDATE). The update table contains the primary key and a LAST_UPDATE field which is a datetime (to tell us when an update occurred). Triggers on the primary table are used to populate the update table.
If we insert or update the primary table in a transaction, we would expect that the datetime of the insert/update would be at the commit, however it seems that the insert/update statement is cached and getdate() is executed at the time of the cache instead of the commit. This causes problems as we select rows based on LAST_UPDATE and a commit may occur later but the earlier insert timestamp is saved to the database and we miss that update.
We would like to know if there is anyway to tell the SQL Server to not execute the function getdate() until the commit, or any other way to get the commit to create the correct timestamp.
We are using default isolation level. We have tried using getdate(), current_timestamp and even {fn Now()} with the same results. SQL Queries that reproduce the problem are provided below:
/* Different functions to get current timestamp all have been tested to produce the same results */ /* SELECT GETDATE() GO SELECT CURRENT_TIMESTAMP GO SELECT {fn Now()} GO */ /* Use these statements to delete the tables to allow recreate of the tables */ /* DROP TABLE COMMIT_TEST DROP TABLE COMMIT_TEST_UPDATE */ /* Create a primary table and an UPDATE table to store the date/time when the primary table is modified */ CREATE TABLE dbo.COMMIT_TEST (PKEY int PRIMARY KEY, timestamp) /* ROW_VERSION rowversion */ GO CREATE TABLE dbo.COMMIT_TEST_UPDATE (PKEY int PRIMARY KEY, LAST_UPDATE datetime, timestamp ) /* ROW_VERSION rowversion */ GO /* Use these statements to delete the triggers to allow reinsert */ /* drop trigger LOG_COMMIT_TEST_INSERT drop trigger LOG_COMMIT_TEST_UPDATE drop trigger LOG_COMMIT_TEST_DELETE */ /* Create insert, update and delete triggers */ create trigger LOG_COMMIT_TEST_INSERT on COMMIT_TEST for INSERT as begin declare @time datetime select @time = getdate()
insert into COMMIT_TEST_UPDATE (PKEY,LAST_UPDATE) select PKEY, getdate() from inserted end GO create trigger LOG_COMMIT_TEST_UPDATE on COMMIT_TEST for UPDATE as begin declare @time datetime select @time = getdate()
update COMMIT_TEST_UPDATE set LAST_UPDATE = getdate() from COMMIT_TEST_UPDATE, deleted, inserted where COMMIT_TEST_UPDATE.PKEY = deleted.PKEY end GO /* In our application deletes should never occur so we dont log when they get modified we just delete them from the UPDATE table */ create trigger LOG_COMMIT_TEST_DELETE on COMMIT_TEST for DELETE as begin if ( select count(*) from deleted ) > 0 begin delete COMMIT_TEST_UPDATE from COMMIT_TEST_UPDATE, deleted where COMMIT_TEST_UPDATE.PKEY = deleted.PKEY end end GO /* Delete any previous inserted record to avoid errors when inserting */ DELETE COMMIT_TEST WHERE PKEY = 1 GO /* What is the current date/time */ SELECT GETDATE() GO BEGIN TRANSACTION GO /* Insert a record into the primary table */ INSERT COMMIT_TEST (PKEY) VALUES (1) GO /* Simulate additional processing within this transaction */ WAITFOR DELAY '00:00:10' GO /* We expect at this point that the date is written to the database (or at least we need some way for this to happen) */ COMMIT TRANSACTION GO /* get the current date to show us what date/time should have been committed to the database */ SELECT GETDATE() GO /* Select results from the table we see that the timestamp is 10 seconds older than the commit, in other words it was evaluated at */ /* the insert statement, even though the row could not be read with a SELECT as it was uncommitted */ SELECT * FROM COMMIT_TEST GO SELECT * FROM COMMIT_TEST_UPDATE
Any help would be appreciated, we understand we could make changes to the application/database to approximate what we need, but all the solutions have identified suffer from possible performance issues, or could still lead to missing deals (assuming the commit time is larger than some artifical time window).
Hi all I was wondering whether it was possible to remove or delete part of a String in a Field? Lets take for example I have:
- A Table called: Table_1 - A Field called: MyField - MyField contains the value: Hello I am on the msdn forums
Is it possible to perform an UPDATE Query which deletes part of that sentence? If this was hard coded it would be rather simple but if the phrase was changing which would lead to the sentence also changing is it possible to do this?
Lets say I wanted to remove the part which said msdn forums. Then UPDATE the field again which should leave out the bit msdn forums.
Is there a command that will let me set getdate() a in a smalldatetime field so that the there is no time associated with it?
For example, I have a table that I want to load the date a user does an action. If I use getdate() I'll get a value such as 5/25/2006 08:26:56.340, whereas I would just like a value 5/25/2006.
I can work it out by doing the following: select (datename(month,getdate())+'-'+datename(day,getdate())+'-' +datename(year,getdate()))
However it seems to me that there should be a simpler way.
Hi,I have a funny situation.Within: MSSQL 2000 SP3, everything below described is running on samePC.there is a program running, which sends information to two otherprograms.This information is a timestamp of the program in datetime format,which has it's own clock.The clock is incremented each 5 seconds of the program, whichcorespondes to aprox. one second of the real time.It means, each on second of real time, the computer time is updated +5seconds.Now, two other applications, are getting this information at the samemoment.FIRST of this applications, updates local time of the computer withthe time recieved.SECOND application, writes a protocol to file, with timestamp read atmoment of writing from operating system.Until now, all times are equal (the differences are not biger thatms).Now, the SECOND application, after writing a log into file (withproper timestamp), calls SP in database.It passes as input prm. the time recieved from very first program,which is the same time as the current system time, which is the sametime the SECOND application writes to the log file.This SP (besides other things) at the very beginning writes a log intotable, where two times are logged:- getdate() to first column,- timestamp recieved as input parameter.Now the funny thing.I would expect, the times are equal.getdate() = '2007.04.25 10:00:00.000'prm_recieved = '2007.04.25 10:00:00.000'I would expect, that the time from getdate() will be shifted withmiliseconds (because of call etc).getdate() = '2007.04.25 10:00:00.123'prm_recieved = '2007.04.25 10:00:00.000'I would even expect, that the time is shifted 5 seconds ahead:getdate() = '2007.04.25 10:00:05.000'prm_recieved = '2007.04.25 10:00:00.000'or, 5 seconds and some miliseconds:getdate() = '2007.04.25 10:00:05.123'prm_recieved = '2007.04.25 10:00:00.000'What I can not UNDERSTAND, why sometimes the time is equal, orsometimes is ALMOST equal (within the diff of miliseconds), and whysometimes the time is like this(!!!) :getdate() = '2007.04.25 10:59:55.000'prm_recieved = '2007.04.25 10:00:00.000'It seams to me, the getdate is getting somehow the PERVIOUS localsystem time, which was acctualy already upgraded ! Becasue all otherapp's are having the proper value.All other apps are writen in C++ and are very simple.I was trying to set the SQLServer running property higher - with noresult.I need to mention, there is SQLServer Agent running, and one procedurewith endless loop, with waitfor delay equal 2 seconds.But non of them (changing the waitfor delay to other value, disablingSQLAgent) fixes the problem.Can somebody then tell me, where from is the time taken, or what isthe root problem of this issue?Or what can it be?Best regards,Matik
Hi,I am creating creating a table with a Date column dd-mm-yyyy. But Icant seem to find a SQL function that just returns today's date.getDate() returns the time as well so I cant use it.The reason is simply that I want to update/overwrite over and overagain all records from current day but not touch the ones fromyesterday etc and with the timestamp in there I just end up addingmore and more rows for the same day.In other words I only want to preserve rows are from yesterday orolder but overwrite ones from today.Any help will be appricated.Thank you!Yas
the ssis expression language getdate() function returns the current date with the current time. i only need to get the current date, without the current time. for example: 9/1/2006
how would i construct the proper expression to return this value?
I have used the GETDATE() function within an expression to create a directory name based on the current date. I am in the Sydney time zone and the new day's folder name doesn't change until after 11 am - so GETDATE() is picking up the date and not adjusting for the time zone. How do I either set the time zone within the package or make the GETDATE() function look at time zone of the system on which it is run?
What is the proper way to ensure when pulling date between two getdates, that you include from midnight of the first getdate to 23:59 hours in the second getdate?
WD.WRKD_WORK_DATE and WD.WRKD_WORK_DATE between DATEADD(DD, - 11, GETDATE()) and DATEADD(DD, - 5, GETDATE())
Hello Chaps,Just a little sounding on removing out of hours from some datetimedate that I have.Basically we have a helpdesk that logs calls when they are entered andwhen they are resolved. Now, if this happens during the day we justsubtract one date from the other to give the time it took to resolvethe issue. If it is not solved during the day we have an additional 11hours to add due to people going home and not worrying about peoplesprinters which is good but it buggers up stats that I'm trying toproduce.I haven't got anywhere with this at the mo and am looking at a routinethat compares the resolved time and see's is there is more that 24hours difference and if so remove 11 hours but wondered if you brightsparks had any ideas(also once I write something I tend to find the answer straight afterto confirm me stoppidity).Any ideas let us know.Ginters
I have a very beginer's question. I am trying to compare datetime field with time only. how can i just compare with time?
Select start_time,end_time from tblvouchers as sv,tblvoucherlines as svl where sv.voucher_no = svl.voucher_no and sv.equip_no = '01325' and svl.line_date ='03/14/2004' and svl.start_time = '18:00' and svl.end_time = '00:00'
How can I display data that only compare the date part (ignoring the time part) between the value of the date parameter and the database?. It displays data only if I pass the whole date (date and time)
I tried with the following query in the report services 2000, but it even didn't pass the query parse
SELECT * FROM table
WHERE (CONVERT(CHAR(8), table.PRODUCTION_DATE, 112) = CONVERT(CHAR(8), @parameterDate, 112))
Does anybody knows how to remove the time part of the datetime format?
some tables in my database's using datetime format, and the time's not default (12:00:00 AM) but it save the exact time. Example : 7/17/2007 11:02:06 AM
when i use datetime for parameters, there's no value returned. While it's impossible for users to entered the exact time when they want to preview reports.
So, how to remove the time part in datetime format?
I'm trying to extract the time part of a smalldatetime field in a usprocedure to return just the time. I've used Datepart() to get the hours or minutes but you can't get both without getting messy. Anyone know a way to keep it neat & simple?
I have writen a nested stored procedure when i am passing a datetime paramter from the first procedure into the internally called procedure it is dropping the time part.
I wrote the query like this "select datepart(year, Msg_Time) from C20_Messages" This query result is:- 2006 2006 2006 2005 2005 2005 2005 Here I want to get only the year part without repeating. I mean 2005 2006
In the hereunder written message I talk about point in time restore.It is now based upon the fact that there are no hardware problems or what soever.I just would like to roll back to a situation of some time (minutes, hoursor what ever) ago.Used to the ingres database a point in time restore can take place UP toany, any, any time since the last FULL backup. (any time up to now !!!)I can't understand why a point in time restore can only be done based upontransaction log backups. The current transaction log is also available in myopinion. (Turn off the power, turn on the power and you will notice that theautomatic recovery is based upon this transaction log file; so in that casethis file is used)That's what my question is about. Is it correct that a point in time restorein a SQL server environment can only be done up to the last transaction logbackup.ByeArno de Jong,The Netherlands.
I have a query which contains 12 left outer join. I remove some of the joins that don't have parameters. The result is coming same but usually when we remove joining it should take less exec time but for me it is taking more time. What could be the reason?
We have 2 databases ( Guider and Talker ) and we have a WCF service that is logged in with a domain identity.
In our SQL Server we have the service ID added to the Data Server Logins and both Guider and Talker are given access to the user.
When we access Guider we have no problems getting data.
When we access Talker we have a login failure:
Cannot open database 'Talker' requested by the login. The login failed.
Login failed for user 'AcornCommunicationServices'.
The thing that gets me is that the user is created at the Server level, in both Databases, and at the server level both databases are checked for the user. master has been set as the default database for the user.
Basically, as far as I can see Talker and Guider are configured identically! So I cannot figure out why I cannot login to the second database!
Is there a specific setting I'm missing somewhere to grant login access to the user? I'm using
From the table above the current running shift is Id = 1
Based on that I have following question:
1 - Is there a way to specified for a DateTime field that it will recieved only Time part ?
2 - When a shift change occurs, I need to compare the Current time with the time shift in the table. Then according to that I set the IsCurrent flag to proper new Shift ID.. how to perform this ?
I am using SSIS 2012 SP1 to import a comma delimited csv file into a SQL table.
One of the fields carries a time value:
Source = textfile, column=DT_STR(8), value format = "hhmmss", e.g. "011525" Destination = field in SQL table, data type = time(0)
To get it from the textfile to the SQL table I am:
1.) Creating a derived column called [d_Time of Entry]with the following formula -
SUBSTRING([Time of Entry],1,2) + ":" + SUBSTRING([Time of Entry],3,2) + ":" + SUBSTRING([Time of Entry],5,2)
2.) Performing a data conversion task to convert [d_Time of Entry] from DT_STR(8) to time(0) The upload fails because values that start with a zero, i.e. times before 10am, have their leading 0's stripped before being derived.Β You can see this because "011525" is derived as "11:52:5" when it should be "01:15:25".
I'm using some files to show certain pages on certain date for an example
File name : aa.doc start date: 10/02/00 end date : 10/03/00
But it expires on 10/02/00, here is the strored procedure:
Before the date comes, it expires the page Here is my stored procedure:
" SELECT startdate, enddate,archivedate and (startdate is null or (getdate() >= startdate and getdate() <= enddate)) and (archivedate is null or (getdate() <= archivedate)) group by startdate, enddate order by startdate desc "
I have SSRS 2005 SP2 configured to work in Sharepoint integration. Everything works fine except that I am not able to programmatically change any property of report viewer web part (instance) that I have added on on home page of my sharepoint site. I can do same thing via sharepoint UI but not through program. When my programs runs it fetches all web parts been added on home page, then I need to iterate through each one and find report viewer web part. While iterating, as soon as I arrive to report viewer web part it is named as "Error web part" with error message as "Windows SharePoint Services cannot deserialize the Web Part. Check the format of the properties and try again"
If someone has a solution, please respond at your earlist.
I have a table with a column named measurement decimal(18,1). If the value is 2.0, I want the stored proc to return 2 but if the value is 2.5 I want the stored proc to return 2.5. So if the value after the decimal point is 0, I only want the stored proc to return the integer portion. Is there a sql function that I can use to determine what the fraction part of the decimal value is? In c#, I can use dr["measurement "].ToString().Split(".".ToCharArray())[1] to see what the value after the decimal is.