Solved

Syntax question for SQL Stored Procedure

Posted on 2007-03-21
2
178 Views
Last Modified: 2010-03-20
Hi Experts

I need to create a date string then do a test against it with another date within a SQL Stored Procedure. So I am first of all extracting the Year I want into a variable called @FinancialYr then need to set a date of 1/4/@FinancialYr.

My existing code is;

set @FinancialYrStart = cast(("01/04/"&@FinancialYr) as datetime)
print  convert(datetime, @FinancialYrStart)

The second line is where I am trying to view the result. I am getting an error ;
"Syntax error converting the varchar value '01/04/' to a column of data type int."

Can anyone give me an idea where the syntax is falling down ?

Many thanks
RangerLad
0
Comment
Question by:RangerLad
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18762729
the error is in line 1, where you do the concat of the string 01/04/ with the variable.
here a corrected  version:

set @FinancialYrStart = convert(datetime, "01/04/" + cast ( @FinancialYr as varchar(4)) , 101 )
print  @FinancialYrStart
0
 

Author Comment

by:RangerLad
ID: 18762757
Thanks angelIII thats exactly the result I was looking for, many thanks

RangerLad
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What logic to build in order to get a weekly reminder 9 51
SQL Server Update Query Streamline 4 29
SQL Server 2012 r2 Make faster Temp Table 17 103
export sql results to csv 6 35
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question