Solved

Need some help with CREATE A VIEW with CAST

Posted on 2011-03-09
10
262 Views
Last Modified: 2012-05-11
Hello
I would like to create a view =ViewLieferscheine1.
In the SQL Table the A.VersandPostleitzahl is a int.

I would like to CAST the int to a nvarchar.
CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl......that is wrong and will not do it
 
How can I do this. Please help.
500 points with a solution.
Thank you.
Best regards,
Thomas

sql.Format("CREATE VIEW ViewLieferscheine1 AS SELECT\
	  A.LieferscheinNr, A.VersandName1, A.VersandName2,\
	  A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl,\
	  A.VersandOrt,A.VersandLand,A.Lieferscheindatum,\
	  A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,\
	  A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht\
	  FROM Lieferscheine AS A inner join Lieferscheinpositionen AS B\
	  ON A.LieferscheinNr=B.LieferscheinNr WHERE A.LieferscheinNr<%d\
	  GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,\
	  A.VersandStrassePostfach, A.VersandPostleitzahl,\
   	  A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,\
	  A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv",2000000);

Open in new window

0
Comment
Question by:tsp2002
  • 7
  • 3
10 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 35082403
Hi,


CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl

Remove A.

CAST(A.VersandPostleitzahl AS CHAR)AS VersandPostleitzahl



- Bhavesh
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082412
Hi,

You no need to give Table Alias while giving column alias.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082424
Hi again,

Secondly your where condition seems not complete

WHERE A.LieferscheinNr<%d

This is not valid syntax.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:tsp2002
ID: 35082788
Hello,
okay that works....thank you.

 A.LieferscheinNr<%d
how should I change this...also with a cast ?
The view should only have LieferscheinNr with a value <2000000
LieferscheinNr is a nvarchar in my SQL table.
Please let me know.
Thank you.


0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082870
Hi,

You can directly pass the value.
you no need to cast

sql.Format("CREATE VIEW ViewLieferscheine1 AS SELECT\
          A.LieferscheinNr, A.VersandName1, A.VersandName2,\
          A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,\
          A.VersandOrt,A.VersandLand,A.Lieferscheindatum,\
          A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,\
          A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht\
          FROM Lieferscheine AS A inner join Lieferscheinpositionen AS B\
          ON A.LieferscheinNr=B.LieferscheinNr WHERE A.LieferscheinNr<2000000\
          GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,\
          A.VersandStrassePostfach, A.VersandPostleitzahl,\
          A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,\
          A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv",2000000);




secondly...try to use VarChar instead of Char with specifying size.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082900
In above query,

I used VarChar(10) as example...

Please use actual size....

Your final view will be like .....
CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht
FROM 
		Lieferscheine AS A 
inner join Lieferscheinpositionen AS B ON A.LieferscheinNr=B.LieferscheinNr 

WHERE A.LieferscheinNr<2000000

GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,
          A.VersandStrassePostfach, A.VersandPostleitzahl,
          A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,
          A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv

Open in new window

0
 

Author Closing Comment

by:tsp2002
ID: 35082950
okay...thanks for your help.
That was fast and easy.
Have a great day.
Best regards,
Thomas
0
 

Author Comment

by:tsp2002
ID: 35083027

CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht
 
you forgot to remove the A.
 AS VARCHAR(10))AS A.VersandPostleitzahl




CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht

 
That was my error in the first place...thank you best regards to India from Germany

Open in new window

0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35083049
Hi,

You used varchar(10).

Please recheck the size with your table.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35083056
happy to assist you.
You to have a great time ahead.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

837 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