I remember at some point I had some problem with this issue and I couldn't find something that suites a newbie.
The point that I was missing was that I couldn't pass the string representation of the date value to a query string. The key is to convert the string date to its equivalent datetime or smalldate .
For example imagine you'd like to select all Employees who where born on a specific date stored in a variable called myDate:
the BirthDay column in our Employee table has type smalldatetime
----------------------------------------------
string myDate = @"06/06/1978";
....
SqlCommand comm = new SqlCommand("SELECT * FROM EMPLOYEE WHERE [BirthDay] = Convert(smalldatetime,'" + myDate + "', 103))";
....
----------------------------------------------
Make suer that you don't miss single quotes around value of myDate in your query string.
For more info regrading the Convert function look at:
I have two tables that I am needing to link by the tables date field. In one table the date field is defined as varchar(23). The time for this field is always zeros. Example: '2005-12-27 00:00:00.000'
The other table is defined as datetime, and it does have the date and time in this field. Example: 2005-12-27 08:00:35.000
The problem i am having is 2005-12-27 00:00:00.000 does not = 2005-12-27 08:00:35.000.
Because I will never have more than one record on the same date I would like to be able to only compare the date. Example 2005-12-27 = 2005-12-27
Since the fields are 2 different field types, this is giving me a problem. Could someone please help. I have tried everything I know to do.
What I really need is the a way to format the datetime fields date into a string such as '2005-12-27'.
I use the merge statement in a sproc to insert, update and delete records from a staging table to a production table.
In the long sql, here is a part of it,
When Matched and
((Student.SchoolID <> esis.SchoolID OR Student.GradeLevel <> esis.GradeLevel OR Student.LegalName <> esis.LegalName OR Student.WithdrawDate <> esis.WithdrawDate Student.SPEDFlag <> esis.SPEDFlag OR Student.MailingAddress <> esis.MailingAddress)
Then update
Set Student.Schoolid=esis.schoolid, .....
My question is how about if the column has null values in it.for example
if schoolID is null in production table is null, but in staging table is not null, will the <> return true.or if either side of <> has a null value, will it return true.
I don't want it to omit some records and causing the students records not get updated.If not return true, how to fix this?
I'm having a problem with what should be a simple TSQL statement. I have a table which has a datetime field updated. After the update if I type
select * from patient_medication where rec_status_date = '11/10/2000' it returns the rows I want.(All the dates have a time of 00:00:00.000. But if I type select * from patient_medication where rec_status_date <> '11/10/2000' or select * from patient_medication where rec_status_date != '11/10/2000'
The rows that have a value are returned, but none of the null values are returned. Will nulls not work with this comparison? Thanks
In my current job I am learning MS SQL - while having to write valid SQL statements. I need some help designing a query that checks hire dates for establishing participation for (something).
So the statement establishing participation has to have a WHERE clause that will check HIRE_DATE and establish two different goals:
1. WHERE Hire_Date < OCTOBER 1 of this current year 2. WHERE Hire_Date < OCTOBER 1 of previous year
Different question now that I am thinking about it: some of these previous SQL statements have a @ symbol, what does that mean in SQL? Can't find an online resource that references that symbol Example: IF @Type IN ('Workflow','All')
HiI have a table that contains information that has start dates and enddates. They are stored in short date format.I have built a web page that initially returned all the information. Ithen want to return information spcific to todays date, where I useddim todDatetodDate = nowthen I didshTodDate = FormatDateTime(todDate, 2)which returned the date to a web page that was in the same format aswhat was in the access table.SELECT * from Events WHERE stDate = # ' & shTodDate & ' #;"This was fine beacuse it returned all events that started on that day.This doesn't help though because some events last a few days, so Itried the followingSELECT * FROM Events WHERE stDate >=#'&shTodDate&'# ANDendDate<=#'&shTodDate&'#;This still only returned events that started on the current day.Does anyone know how I can use <= or >= when comparing dates?ThankspuREp3s+
Hi all,I am trying to create a stored procedure that will check a date field.I want to check for records that are equal to or greater then 90 daysfrom the current date. I am trying to check this against a fieldcalled LastUpdate. Is there an easy way in SQL to do this?TIA
I am trying to use a date comparison in a statement using the year statement as well. Here is what I have:
Case [LastHireDate] When YEAR([LastHireDate]) < Year(@EndYearlyDate) then '12' When Month([LastHireDate]) = '1' then '12' When Month([LastHireDate]) = '2' then '11' When Month([LastHireDate]) = '3' then '10' When Month([LastHireDate]) = '4' then '9'
[Code] ....
When I am looking at it [LastHireDate] is showing that red line underneath. The < symbol has a red line and @EndYearlyDate has a red line. I can not seem to get them to clear and am, wondering what I am missing. When I execute the error comes up that it does not like the < sign in there.
I have a datetime variable coming from my ASP.NET application that hasa time portion. I give my users the option to perform an equals,greater than, less than, or between comparison. The trouble comes inthe way the application builds the criteria string. The WHERE clausepassed in is in the format, "(start_dt = '2005/05/16 07:00:00.000')".What I want to do is only compare the date portion of start_dt to thedate portion of the passed in time. Manipulating the start_dt with thebuilt-in SQL functions isn't a problem, but altering the date passed infrom the ASP.NET would be a massive framework change in the app.Is there any way to only compare the date portions of both the SQLfield and the passed in value?Thanks.
I need to take all records from table @A where ID = 1. Also i need to process the records with datewise from table @A. Here is the table structure
DECLARE @A TABLE (ID INT, ACCOUNT VARCHAR(10), EFFDT DATE) INSERT INTO @A VALUES (1,'AAA','2015-10-01') INSERT INTO @A VALUES (1,'BBB','2015-10-01') INSERT INTO @A VALUES (1,'CCC','2015-10-01') INSERT INTO @A VALUES (1,'AAA','2015-10-05') INSERT INTO @A VALUES (1,'DDD','2015-10-01') INSERT INTO @A VALUES (2,'AAA','2015-10-02') INSERT INTO @A VALUES (2,'BBB','2015-10-02') INSERT INTO @A VALUES (2,'CCC','2015-10-02') INSERT INTO @A VALUES (2,'DDD','2015-10-02')
[code]...
how to achieve this in SQL query, i cannot use CTE or temp table as i need to use this code in another tool, it has to be single query, can use subquery or join would be better.
I would like to return the nearest date of Table B in my table like for
ID W001 in table B should return ID A002 CreatedDatetime: 2014-06-03 20:05:48.000 ID W002 in table B should return ID A004 CreatedDatetime: 2014-06-04 01:05:48.000
I have a QA Deployment Date field that is being returned in a custom report I created. I also found a sample date range parameter:
What I want to accomplish:
I want to select a From and To Date and filter the report to only display the rows that have the QA Deployment Date within the selected range.
For example.. I want to select From Date (8/1/2105) and To Date (8/31/2015) and I only want to return only the results that have a QA Deployment date between that selected range.
I'm stuck trying to compare date values from users selection against a database. I also need to add a condition statement on if the results return no match. How would I do that?
Also, I'm not even sure if my SELECT statement is right. The user will hit a date that will be in format m/d/yyyy and that will be compared to see if it exist in the database. The datebase column "date_created" is default with date/time( 8 ). But when I compare the to together, nothing returns back. The page loads successfully, but no results from the database.
Here is my code: dim conpubs as sqlconnection dim cmdSelectAuthors as sqlcommand dim dtrAuthors As sqlreader
conpubs = New sqlconnection(configurationsettings.appSettings("STD")) conpubs.open() cmdSelectAuthors = New sqlcommand("Select * From HotNews WHERE date_created=" & y, conpubs) dtrAuthors = cmdSelectAuthors.ExecuteReader() Response.Write("<table><tr><td>")
While dtrAuthors.Read() Response.Write(dtrAuthors("title") & "<br>") End While
I'm stuck trying to compare date values from users selection against a database. I also need to add a condition statement on if the results return no match. How would I do that?
Also, I'm not even sure if my SELECT statement is right. The user will hit a date that will be in format m/d/yyyy and that will be compared to see if it exist in the database. The datebase column "date_created" is default with date/time( 8 ). But when I compare the to together, nothing returns back. The page loads successfully, but no results from the database.
Here is my code: dim conpubs as sqlconnection dim cmdSelectAuthors as sqlcommand dim dtrAuthors As sqlreader
conpubs = New sqlconnection(configurationsettings.appSettings("STD")) conpubs.open() cmdSelectAuthors = New sqlcommand("Select * From HotNews WHERE date_created=" & y, conpubs) dtrAuthors = cmdSelectAuthors.ExecuteReader() Response.Write("<table><tr><td>")
While dtrAuthors.Read() Response.Write(dtrAuthors("title") & "<br>") End While
Hi , I need to compare two date fields in two different tables.One of the field is varchar(8) and other is dateime.When there is a date in one field and NULL in other field , how do I compare these two vales?
I would like some suggestions on how to keep from displaying the value "1/1/1900" on my asp pages when my recordset field returns a Sql date value that is null
The tables share the common columns id and deptno. To get the above result, the emp1.edate must be between emp.sdate and emp.edate.
If that condition is met, we need to retrieve the refid and refid1 values corresponding to the lowest edate as last_refid and last_refid1. Then we need the refid and refid1 values corresponding to the emp1.edate between emp.stdate and emp.edate to be retrieved as the current_refid and current_ refid1.
select a.id,a.deptno,a.locid,b.refid,b.refid1 from emp b
Guys I want to get the date ranges instrument wise for which the instrument readings are constant.
For example for instrument 1 the readings are constant i.e 10 from 10/12/2008 till 12/12/2008 & then again it is constant from 14/12/2008 till 17/12/2008. Same goes for instrument id 2.It is constant from 07/03/2008 till 20/03/2008. I need to get the output like this.
How do I order a query by a date field ASC, but have any NULL valuesshow up last? i.e.7/1/20037/5/20037/10/2003<NULL><NULL>Any help will greatly be appreciated
Would like to restrict the dates users can put as parameters. For example, I have a report showing all orders in year 2007. I have exposed Start Date and End Date parameters. When users click the 'View Report' button, the report will filter for orders where attribute EnteredDate is >= Start Date and <= End Date.
I want to restrict the users from entering a start date greater than today's date. Would also like to restrict them from entering an end date that is less than the start date parameter. How can I do this?
Hi, In my Excel file, The Application date column contains empty for some rows. In SSIS I am using one Data Conversion to that Application Date column to change it as Date[dt_Date]. This data conversion is giving error Conversion failed. In Sqlserver table, I declare ApplicationDate column datatype as DateTime. I want to keep those empty date values as Null in Sqlserver. I tried the IMEX=1 property still it is not working. How to solve this error?
I am attempting to create search parameters for a gridview control and I am experiencing a small issue. When I get to a date parameter I am unable to display null values. I setup a sqldatasource and created the parameters below to handle the selections for minimum date required and the maximum date required for the date columns in the database. The problem is I do not know how to display null dates. Is there a way to incorporate something into the search page to show null dataes?
Sql Where Clause 1 WHERE (LSS_Requests.TypeCode = @TypeCode) AND (LSS_Requests.PersonNo LIKE '%' + @PersonNo + '%') AND 2 (LSS_Requests.TicketNo LIKE '%' + @TicketNo + '%') AND (LSS_Requests.Name LIKE '%' + @Name + '%') AND 3 (LSS_Requests.RequestName LIKE '%' + @RequestName + '%') AND (LSS_Requests.RequiredDate >= @Fromrequireddate) AND 4 (LSS_Requests.RequiredDate <= @ToRequiredDate) AND (LSS_Requests.OriginationDate >= @SearchFromOriginationDate) AND 5 (LSS_Requests.OriginationDate <= @SearchToOriginationDate) AND (LSS_Requests.LastUpdated >= @SearchFromUpdatedDate) AND 6 (LSS_Requests.LastUpdated <= @SearchUpdatedToDate) AND (LSS_Users_1.userFullName LIKE @SearchDDLUsers) AND 7 (LSS_Users.userFullName LIKE @SearchddlCIAsignee) AND (LSS_Requests.TypeCode = 'CC') AND (LSS_lu_Status.stNm LIKE '%' + @StatusName + '%')
I have a calendar parameter in one of my reports. The values of the calendar are of the form 'yyyy-mm-dd hh:mms'. And they are string values.
Now when I generate the report I have a textbox that takes the parameter label (e.g., parameter!calendar.label). However, I would like to format (or trim) the label as just 'yyyy-mm-dd'. Does anyone know how I can do this? I tried to change the parameter value format to datetime but this just throws an invalid datatype error...
I am using a DAL and i want to insert a new row where one of the columns is DATE and it can be 'NULL'. I am assigning SqlTypes.SqlDateTime.Null. But when the date is saved in the database, i get the minvalue (1/01/1900) . Is there a way to put the NULL value in the database using DAL????how can i put an empty date in the database? THANK YOU!!!
I want to have a FromDateTextBox and a ToDateTextBox where the user can enter in dates (most likely in mm/dd/yy format, although intelligently handing other formats might be a plus). Then I want to use these dates as the basis for a WHERE clause like:<some sql...> WHERE start_date BETWEEN 'FromDateTextBox.Text' AND 'ToDateTextBox.Text' (Note this WHERE clause will be used as the basis for an SqlDataSource FilterExpression). 1. I believe the date strings need to be in the format 'yyyy-mm-dd' to search SQL server is this correct?2. What's a decent way to convert the strings from the textboxes to the required format?3. How can I avoid an SQL injection attack?