Dynamic SQL - Date To Varchar
Jun 18, 2008
I cannot execute a dynamic SQL function when I convert a datetime to a string:
declare @date_key datetime
set @date_key = '5/1/08'
select * from OPENROWSET('SQLOLEDB','Data Source=test;Trusted_Connection=yes; Integrated Security=SSPI'
,'Execute dbFinance..spCalc ''' + convert(varchar(20), @date_key, 101) +'''')
The code below produces the exact same text but DOES work:
select * from OPENROWSET('SQLOLEDB','Data Source=test;Trusted_Connection=yes; Integrated Security=SSPI','Execute dbFinance..spCalc ''05/01/2008''')
View 6 Replies
ADVERTISEMENT
Jul 31, 2007
I have a column of data in a table that has date formatted as '2006-03-26 00:00:00.000'
What T-SQL command that will alter the column so that it is now Varchar '03-26-2006'?I also want to know how to do the opposite... if I have '03-26-2006' via command, how do I convert the column of the table to be datetime from varchar
View 1 Replies
View Related
Jan 11, 2008
For the example stored procedure below, lets say I want to use the "sp_executesql" stored procedure instead of "EXECUTE".
CREATE PROCEDURE [dbo].[spGetEmployees]
@managerId int,
@employeeIdList nvarchar( 200 )
AS
EXECUTE
(
'SELECT *
FROM [dbo].[hrEmployees]
WHERE [ManagerID] = ' + CAST( @managerId AS nvarchar ) + '
AND [EmployeeID] IN (' + @employeeIdList + ')'
)
I want to rewrite it something like this. Please see MSDN documentation ( http://msdn2.microsoft.com/en-us/library/ms188001.aspx ) for sp_executesql stored procedure usage.DECLARE @selectStatement nvarchar(500)
SET @selectStatement = 'SELECT * FROM [dbo].[hrEmployees] WHERE [ManagerID] = @paramManagerID AND [EmployeeID] IN (' + @employeeIdList + ')'
DECLARE @paramList nvarchar(500)
SET @paramList = '@paramManagerID int'
EXECUTE sp_executesql @selectStatement, @paramList, @paramManagerID = @managerId
Reason for using "sp_executesql" is the performance gain.However, as you can see, the @employeeIdList cannot be included as part of the Parameter List ( @paramList )like the @managerId since it **has** to be passed in as a varchar ( example: @employeeIdList = '1,2,3,4' ).
My Question Is there a way to include it as a parameter instead of it being part of the embedded dynamic SQL syntax?
View 1 Replies
View Related
Nov 21, 2006
I'm using SSIS to import data from a table (SQL) containing varchar fields. The problem is, that those varchar fields are changing over time (sometimes shrinking and sometimes expanding). I.e. from varchar(16) to varchar(20).
When I create my SSIS package, the package seem to store information about the length of each source-field. At runtime, if the field-length is larger then what the package expects an error is thown.
Is there anyway around this problem?
Oh, yeah... My destination fields are a lot wider then the source fields, so the problem is not that the varchar values doesn't fit in my destination table, but that the package expects the source to be smaller...
Regards Andreas
View 3 Replies
View Related
Aug 4, 2007
Hi All:
I am trying to execute a dynamic sql, the dynamic sql makes use of an inbound paramter defined as varchar.
When I try to execute it fails, because it does not plavce the inbound paramter in quotes.
Any help would be appreciated.
In the bound search as an eaxmple can be" 'NY'
@P_SEARCH_VALUE='NY'
SET @V_SQL_FILTER = N' WHERE STATE = '+@P_SEARCH_VALUE
SET @V_SQL=@V_BASE_SQL+@V_SQL_FILTER
EXEC sp_executesql @V_SQL
Here is the v_sql out put:
SELECT TOP 100 * FROM V$ZIPCODE_LOOKUP_ALL WHERE STATE = NY
As you can see the sql will fail because the NY is not in quotes.
I tried using '@P_SEARCH_VALUE''' and other forms but could not get it work.
View 6 Replies
View Related
Jan 13, 2004
This is probably a very simple question, but there it goes.
I have a DB table with the following varchar value:
1/12/2004
I want to convert the specified data to the following:
Monday, January 12th, 2004
I guess i want to:
Convert(DATETIME, '1/12/2004'), But...
I want it to be inserted into the new table as a long date type value.
Is this possible?
Thanks in advance!
View 5 Replies
View Related
Jan 10, 2002
Hi:
here i am giving one example with Emp table and fields ssn,empjoindate
I have a table with field empjoindate as varchar(10).I need number of employees joined between 12/23/01 and 01/04/02.I ran this query using select ssn from EMP where convert(varchar(10),empjoindate,101) between '12/23/01' and '01/04/02'.it is not fetching any data.
Please help me
Thanks
Tej
View 8 Replies
View Related
Jan 10, 2006
I have a preexisting database that has dates in the format mm/dd/yyyy but it is set up as varcar. How can I apply numeric operators to it to find how old someone is from todays date? I then need to take that value and populate another column on the same row.
View 1 Replies
View Related
Jul 20, 2012
i am pulling data from a read only database and the dates are i think in varchar format (they sort by 01-01-2012, 01-02-2011...etc)the dates are also in US format.how can i convert these to a EUROPE date format and also datetime so that i can use the > and just show logged events greater than a specific date
Code:
SELECT logId, logdate, logdesc
FROM dbo.log
WHERE (logdate >'07/07/2012')
the code about will not work as the logdate is in varchar.
View 6 Replies
View Related
May 27, 2008
I have a datetime column and I would like to update another column in the same table with the varchar value of date. e.g. date column has 06-08-2008 as a value. I would like to have the same value in the varchar field.
wondering which function can I use to accomplish this task?
thanks
View 4 Replies
View Related
May 30, 2008
I have Field which contains Date in dd/mm/yy fromat. I need to convert it into DateTime format so that it can be processed.
Please help
View 2 Replies
View Related
Nov 18, 2005
Hi
I have a varchar field with values like "Jun 13 1995 12:" I want to change the type of the field to Date and the format of the values to "mm/dd/yyyy".
If I change the type of the field in the design view to Date, SQL gives me an error saying that the type cannot be changed and data would be lost.
So I think changing the dates to the mm/dd/yyyy format first and then changing the field type from varchar to date would work.
Can someone please help me as how to do this.
Thanks
View 6 Replies
View Related
Aug 2, 2006
Hi All,
We are trying to create a new view within SQL 2000. We have two colums from seperate tables, 1 is a datetime data type which stores the Date the other is a varchar that stores the time.
What we would like to do is merge the two into one datetime colum so we can work with this one datetime column. Unfortunaly it is not posible to have this entered into the one field at data entry. If needed we can change the time column from a varchar to a datetime datatype.
Also our SQL knowledge is minimal.
Hope this makes sense any help would be deeply appreciated.
Regards
Darren
View 9 Replies
View Related
Jan 28, 2007
have a field called sigdate datatype varchar that saves dates
like these
01/2/2007
12/2/2007
06/19/2007
01/21/2007
i want to show distinct months/year in a drop down
so the query should output
01/2007
12/2/2007
06/2007
here is my code
SELECT Distinct(convert(varchar(2),month([sigdate]))+convert(varchar(4),year([sigdate]))) as [monthyear] from stdreg
but its not working
any ideas
todd
View 4 Replies
View Related
Mar 5, 2007
I have developed a CF application and now need to add some charts to it for admin purposes. I have a form page that uses a calendar widget to let a manager select a date range and submits to display data for that timeframe. I need to convert the formfields to a date for my WHERE clause in my query. My experience to this point has just been simple queries of one table. below is a sample I found and modified numerous time to try and make it work with no success.
Can anyone give me a hand with this?
SELECT COUNT ((CHG_TYP_CD)) AS CHG_TYP,
CHG_TYP_CD
FROM WRK_RQS
WHERE(CAST('form.startdate' AS datetime) >= RQS_SBM_TS)
AND
(CAST('form.startdate' AS datetime) <= RQS_SBM_TS)
GROUP BYCHG_TYP_CD
View 4 Replies
View Related
Aug 14, 2007
Hi
I have a table named prodwin in which i have to update a column name mfgdt which is varchar(10) to 7 months which I am able to do but the problem is that since it is only 10 character it is cutting the end of the year for eg
Before the date was
5/22/2000
UPDATE PRODWIN_MM SET MFGDT = DATEADD(MM,7,MFGDT)
Now it is this
Dec 23 200
Please tell me what to do , should i use cast or convert ?
Thanks in Adv
View 7 Replies
View Related
Jan 22, 2008
Hi all I would like anyone to help I want 2 convert a datetime to 2008-02-02 21:00 the output must leave the time just 2008-02-02
If (@DateCreated <> 0)
Begin
Set @varSelectSql = @varSelectSql + ' And convert(varchar(10),DateCreated,121) = '+ cast(@DateCreated as varchar(10))
End
View 4 Replies
View Related
Feb 3, 2008
Hi,
Can any one help me in that case?
I have 2 colomns one have the month (1,2,3,4,....,12) and the other have the years (2007,2008,....)
I want to merge both in one date field MM/DD/YYYY and the day will always be '01'
Thank you
M.Maraghy
View 2 Replies
View Related
Jul 20, 2004
for some odd reason our other programmer used varchar datatype to store dates. he claims it gives him more control. now i am trying to sort it based on date. so i create a procedure:
CREATE PROCEDURE GetAllWeekEnding
AS
Select convert(datetime, we) as we2 FROM tblArchive order by we2
GO
if i use the convert function in the procedure, i'll get an error msg when i run the code. this is the code i am using.
Dim MyConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringSQL"))
Dim MyCommand As SqlCommand
MyCommand = New SqlCommand("GetAllWeekEnding", MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure
MyConnection.Open()
Dim mydr As SqlDataReader = MyCommand.ExecuteReader()
While mydr.Read()
DropDownList1.Items.Add(mydr("we2"))
End While
mydr.Close()
MyConnection.Close()
the error message is: No accessible overloaded 'ListItemCollection.Add' can be called without a narrowing conversion
any ideas?
View 5 Replies
View Related
Feb 28, 2001
Hi,
I have to change a due_date field with a varchar(10) to a datetime. I created a new table with datetime but when I imported data into the table it errored out due to the datatype. A varchar(10) going into a datetime(8).
Is there way to resolve this without losing data?. or how do you result this problem?.
Thanks for your help,
Jeff.A.
View 1 Replies
View Related
Nov 27, 2015
Having an issue getting this done. The datatype in the table is varchar(50). The values in the table are
11122015
11122015
11122015
Nothing has worked so far.
View 3 Replies
View Related
Mar 31, 2008
Hi guys.
i have a field in a table which datatype is varchar and i m storing datetime and number in it.. but when i try to use select statement like ( between startdate and enddate) or number (= ) operator it's not working..
select * from table
where cast(Field as datetime) between @startdate and @enddate
second query that i m trying to exectue is
select * from table
where cast(Field as bigint) = @Fieldid
but both are not returning any value...can anyone tells me where i m doing mistake
View 21 Replies
View Related
Mar 4, 2004
Hi everyone.
I know, I know, it should have been a datetime from the start...but here's the problem.
I'm trying to sort by my date field but because it looks like: "04/03/2004 12:14:21 PM" it's not ordering it properly using:
ORDER BY [Date]
Are there any work arounds for this? Is there some way of doing:
ORDER BY covert(datetime, [Date], 103) or something?
Cheers
Andrew
View 3 Replies
View Related
Dec 2, 2011
In my table I am using varchar to save a date like this 2-dec-2011. Now I have a requirement where i need to select employee who where present in a month so for that I need a query so that i can pull out all the employees who are present in dec like :
Code:
select * from emp_attendence where date = 'dec-2011'
But this returns nothing ...
View 8 Replies
View Related
Jul 21, 2014
My current Query takes the DATE value stored in P.CreatedDate and makes it VARCHAR, then stores it in the table. I need to format this to return YYYYMMDD. How would I go about this?
Current Code:
Code:
CAST(P.CreatedDate AS VARCHAR) AS DateEntered,
View 3 Replies
View Related
Jul 12, 2013
I have a column (varchar (50)), It has values from 1-12.
I want to cast them to their appropriate months.
Example: 1 = January
2 = Febuary
3 = March
etc.
View 3 Replies
View Related
Jan 17, 2007
I have a varchar field which should contain dates but unfortunately it is fill with all sorts of imaginary formats which only the one who has typed may understand.
9+03/24/2006
3/16/2006
11:15AM
5-Oct
8/8/2006
9
.
`06/27/2006
12505
1/9/2007
1/12/2007
1/12/2007
1/15/2007
in a data set of 15000 rows most of them are like '1/15/2007'
and 305 roes are like the rest in the sample.
can you sugest ma a simple way to convert posible data to format '1/15/2007' format which i can use in a SELECT...INSERT STATEMENT and convert the rest to NULL
View 7 Replies
View Related
May 23, 2008
how do you get it to sort correctly for the date? at the minute its sorting on the characters rather than actual date value
Code Snippet
INSERT INTO @TempItems (OrderID)
SELECT OrderID
FROM Orders o
INNER JOIN Customers c ON c.CustomerID = o.CustomerID
INNER JOIN Employees e ON e.EmployeeID = o.EmployeeID
ORDER BY
CASE @SortOrder --Order ASC
WHEN 0 THEN cast(OrderID as varchar(100))
WHEN 1 THEN cast(c.CompanyName as varchar(100))
WHEN 2 THEN cast(e.FirstName as varchar(100))
WHEN 3 THEN cast(o.OrderDate as varchar(100))
WHEN 4 THEN cast(o.RequiredDate as varchar(100))
WHEN 5 THEN cast(o.ShippedDate as varchar(100))
ELSE '1'
END,
View 4 Replies
View Related
Sep 27, 2007
Hi all
With this query
select convert(varchar(16), getdate(), 101)+' '+LEFT(convert(varchar, getdate(), 108),5)
I get
mmddyyyy hh:mm
How can I change my query to get
mmddyyyy hh:mm but no 0 when month from 1 to 9
example can not be: 02/12/2007 03:34
but should be: 2/12/2007 03:34
Don't know why excel not accept month with 0 from 1 to 9
Thks
Daniel
View 5 Replies
View Related
Jan 21, 2008
Hi All,
I have 2 varchar fields on MS 2005 table
First field is date and format is 080118(YYMMDD)
and second is salary field like 00002000(positive) and 00002000- (negative)
how can I move them to date and numeric fields on another table....
thanks
View 1 Replies
View Related
Aug 6, 2013
I have a column which is
Data type : varchar(10)
Date format : 8/5/2013
I want to convert that column to the following
Data type varchar: (8)
Date format : 20130805
View 5 Replies
View Related
Sep 26, 2007
With this clause "select convert(varchar(16), getdate(), 101)" I can get mmddyyyy but
How can I get time such as mmddyyyyhhmm
Thanks
Daniel
View 9 Replies
View Related
Jan 31, 2014
I have a table with Month , Year as varchar. I realized it was a big mistake. Since its getting too complicated to query this way.
Year Month Productname
2013 11 ACB
2013 11 CDE
I would now like to add another column called date and store these Year Month as a date to my existing table
Year Month ProductName Date
2013 11 ACB 2013-11-01
2013 11 CDE 2013-11-01
Is there a way I can do it for all the columns of the existing table ??
View 3 Replies
View Related