Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Server Syntax

Posted on 2011-10-31
6
Medium Priority
?
266 Views
Last Modified: 2012-05-12
I've just switched the backend of my application from MS Access to SQL Server.
This sytax worked with my Access database but it won't return anything from SQL Server:
SELECT Count(id) AS Countid FROM MyTable where PersonAddedOn like '%"& (Date()) & "%'
Would you please let me know what the correct syntax is?
Thank you
0
Comment
Question by:baxtalo
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Expert Comment

by:tim_cs
ID: 37059487
Try this.

'%' + CONVERT(varchar(10),GETDATE(),101) + '%'
0
 
LVL 12

Assisted Solution

by:Paul_Harris_Fusion
Paul_Harris_Fusion earned 1000 total points
ID: 37059558
The function Date() in Access returns todays date.   The equivalent in SQL Server is GetDate( which returns current date time.  

Also, concatenation is + rather than &

However, getting the same results as your Access query will requiire you to control the way in which the date is rendered as a string.

An example of one syntax is shown below

Select Count(id) as CountId
From MyTable
Where PersonAddedOn like '%' + SELECT CONVERT(VARCHAR, GETDATE(), 101)  + '%'

This formats todays date in MM/DD/YYYY format using the 101 format.
Change the 101 if you need a different date format.
More information on controlling the date format can be found here:
http://www.sql-server-helper.com/tips/date-formats.aspx


0
 

Author Comment

by:baxtalo
ID: 37059595
I've just tried it but it didn't return anything. I forgot to mention that I'm using Classic ASP on my page. This code is on my Confirmation page and my goal is to display how many submissions have been sent to the database today. In my SQL Server database the PersonAddedOn column is 'datetime' and it records the date and time of submission when I enter a record.
<%
Set objConn = CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=sqloledb;SERVER=DASTVMSQLCX01;DATABASE=MyDatabase;UID=DDUser;PWD=DB123456;"
objConn.Open

' To get the RecordCount, without the [RecordSet = CreateObject]
' This keeps the code a little cleaner.
Set sqlCount = Server.CreateObject("ADODB.Command")
sqlCount.ActiveConnection=objConn
sqlCount.Prepared = true
sqlCount.CommandText = "SELECT Count(id) AS Countid FROM MyTable where PersonAddedOn like '%' + CONVERT(varchar(10),GETDATE(),101) + '%' "
set objRs = sqlCount.Execute
CountID = int(objRs("CountID"))
%>
<%=Countid%>
<%
ObjRs.Close
Set ObjRs = Nothing
%>
Submissions Today

Open in new window

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.

 
LVL 15

Accepted Solution

by:
tim_cs earned 1000 total points
ID: 37059641
You'll want to convert the PersonAddOn to then.  

where CONVERT(varchar(10),PersonAddedOn,101) like '%' + CONVERT(varchar(10),GETDATE(),101) + '%'
0
 

Author Closing Comment

by:baxtalo
ID: 37059663
Thank you very much; it worked.
I've learned a lot.
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 37059669
Since your date is stored as date time, then you just need to ignore the time part if that is your requirement
e.g.

Select Count(id) as CountId
From MyTable
Where  CONVERT(VARCHAR, PersonAddedOn , 101)  
  =  CONVERT(VARCHAR, GETDATE(), 101)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

810 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