473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SQL question

Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy
Oct 9 '06 #1
5 1353
OK I found the problem ....
The gridview was expecting the column name (select x from) but the
calculation right(x,4) changed the column name in the result......

Guy

"Guy Cohen" <no*****@please.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy

Oct 9 '06 #2
Guy Cohen wrote:
Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy
Look up the following functions in the Online help:

DATEPART
SUBSTRING

For the date, you can issue this command before executing the SELECT:

SET DATEFORMAT dmy

--

Riki
Oct 9 '06 #3
"Guy Cohen" <no*****@please.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
No doubt there will be people here who will disagree with me here, but in my
experience it's much more efficient to let each layer / tier of your app do
what it's best at.

In this specific case, let your database layer fetch the data, and then use
your presentation layer for formatting that data and displaying it the way
you want. Presumably you're storing your date in SQL Server as either a
datetime or smalldatetime field? Therefore, there's no advantage in
converting it to a varchar just so you can return it to your front end as a
string. If you are binding the data e.g. to a GridView, you can format it
directly in the <asp:GridViewcontrol tag by using the DataFormatString and
HtmlEncode properties of the <asp:BoundFieldcontrol tag e.g.

<asp:GridView ID="MyGridView" runat="server">
<asp:BoundField DataField="MyDateField" HeaderText="Date"
DataFormatString="{0:dd/MM/yy}" HtmlEncode="false" />
</asp:GridView>

If you're simply populating e.g. a TextBox, you can do something like:

MyTextBox.Text = <DataSource>["MyDateField"].ToString("DD/MM/YY");

Just as an aside, you'd be well advised to avoid ambiguous date formats such
as DD/MM/YY - e.g. what would you understand by the date 02/03/04...?
If you use something like "dd MMM yyyy", that would be totally unambiguous
across all cultures.

2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable
That's a little different. Assuming you only ever need to know the last four
digits of the credit card number, then getting the database layer to do this
for you is IMO good practice because it reduces the amount of data sent from
server to client - that's always a "good thing" to do.

Therefore, you need to use SQL Server's SUBSTRING method - look it up in SQL
Server Books On-Line.
Oct 9 '06 #4
In SQL, you have some control, but .NET may changes things for you
regardless, so you have to determine where it is best to put things.

FORMAT:

In .NET code it is String.Format()
In SQL
SELECT CONVERT(varchar(20),GetDate(), 103)

Last four characters. If you are using VB.NET, Right(stringName, length)
works. In C#, you have to use .Substring(start, finish)
In SQL:

DECLARE @creditcard varchar(16)

SET @creditcard = '4111111111111111'

SELECT SUBSTRING(@creditcard, (LEN(@creditcard)-4), 4)


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"Guy Cohen" <no*****@please.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy

Oct 9 '06 #5
Hmm, "SELECT RIGHT(CreditCardNumber, 4) FROM myTable" - I don't suppose you
could let us know the IP address of your live server that will host your SQL
database because you've just told everyone that you're not storing credit
card numbers encrypted. Just a warning, regardless of the size of your
business of the purpose of your application, this is in voliation of most
credit card security recommendations. At least the number should be stored
encrypted to a specified minimum level - also you shouldn't really store the
(encrypted) details beyond the duration of the transaction (at which point
you only need to store the returned transaction reference from your payment
provider. The penalties applied by VISA/Mastercard, etc if a security
voliation on your companies part results in credit card details being stolen
are huge - probably enough to put most companies out of business.

- Paul.

"Guy Cohen" <no*****@please.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy

Oct 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.