Using SUBSTRING_INDEX() in SQL Server

Hi 
I started SQL recently as a beginner i came across the above problem. the book i referring is using MySQL and i want to know how can i do the following in SQL Server 
This is the query as per the example in the book
SELECT SUBSTRING_INDEX(location, ',', 1) FROM my_contacts;
Thanks in Advance 

There is no equivalent function in SQL Server. We need to write our own function. 
The below link would help you to do so:
Function link

Similar Messages

  • How to load decimal values into a file using bcp in SQL Server 2000?

    Hi everyone,
    I'm trying to load data from a database table to a text file using bcp on SQL Server 2000. When I'm loading the decimal values, the values before the decimal are not being loaded into the file. How could I load the values before decimals?
    Here are the DDLs and DMLs of what I'm trying to run:
    CREATE TABLE [dbo].[Product_Sales](
    [Year_of_Sale] [smallint] NOT NULL,
    [Product_Type] [varchar](25) NOT NULL,
    [Product_Group] [varchar](25) NOT NULL,
    [Category_Type] [varchar](25) NOT NULL,
    [Category_Group] [varchar](10) NOT NULL,
    [Product_Count] [bigint] NOT NULL,
    [Product_Amount] [decimal](19, 2) NOT NULL,
    [Category_Count] [bigint] NOT NULL,
    [Category_Amount] [decimal](19, 2) NOT NULL,
    CONSTRAINT [PK_Product_Sales] PRIMARY KEY CLUSTERED
    [Year_of_Sale] ASC,
    [Product_Type] ASC,
    [Product_Group] ASC,
    [Category_Type] ASC,
    [Category_Group] ASC
    ) ON [PRIMARY]
    ) ON [PRIMARY]
    INSERT INTO [Yearly_Sales].[dbo].[Product_Sales]
    VALUES(2010, 'Online', 'Web', 'Cameras', 'Electronics', 547, 0.00, 0, 0.00)
    EXEC [master].[dbo].[xp_cmdshell] 'bcp "SELECT * FROM [Yearly_Sales].[dbo].[Product_Sales]" queryout D:\Yearly_Sales\Product_Sales.dat -c -T -S'
    And the output I see in the file is:
    2010 Online Web Cameras Electronics 547 .00 0 .00
    The values before decimals are being truncated.
    Thanks,
    Bangaaram
    Known is a DROP, Unknown is an OCEAN.

    You can modify the decimal column(s) in the table to varchar type before export. But it does not look good.
    alter table [dbo].[Product_Sales]
    Alter column [Product_Amount] varchar(20)
    alter table [dbo].[Product_Sales]
    Alter column [Category_Amount] varchar(20)
    Does that make any sense?
    Known is a DROP, Unknown is an OCEAN.
    Basically you modify the decimal column to a varchar type before you run the bcp command for export. You can modify your source table or you can modify them on the fly. It is just a workaround.
    Modifying the source wouldn't be a good idea. Never do that if you can't get your desired output.
    Known is a DROP, Unknown is an OCEAN.

  • Create Fiat File Using Trigger in SQL Server

    Dear All,
        How to Create a Flat File using Trigger in SQL Server?

    Take a look at osql/sqlcmd, bcp, COM calls, xp_cmdshell, etc.  Here are a few links with code examples:
    https://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/
    http://www.nigelrivett.net/SQLTsql/WriteTextFile.html
    http://stackoverflow.com/questions/8132663/creating-a-text-file-on-local-machine-using-sql-server-2008
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How save & retrive data from database using flex, coldfusion, sql server

    hi , i am very new in flex, i am trying to insert data in
    database using flex2, coldfusion, sql server. any one can help me
    please. or give me any example.

    Hi try to goto the following sites, you will get some
    direction to think
    http://weblogs.macromedia.com/pent/archives/2006/11/example_of_flex.cfm
    http://pdxphp.org/node/258
    Good Luck

  • XDK APIs (C/C++/Java) and tools can also  be used with MS SQL Server

    I want to know if XDK APIs (Java or C/C++ APIs especially) and tools can be used with MS SQL Server or not?
    I want to write a simple application that updates XML ( acordXML used for insurance) document into SQL Server tables. Of course I am assuming to transform into appropriate XSU APIs format.

    No. Use SQLXML instead on a SQL Server.

  • How to tell whether a license used to install SQL Server is MSDN license or Volume license?

    I want to check license used to install SQL Server is MSDN license or Volume license for SQL Servers which are at least SQL Server 2008 on Windows 2008 R2.  Is there any script/command/tool that I can run on Windows or SQL Server to find such
    information? 

    Hello,
    This is a community forum for technical questions & issues.
    For question regarding pricing / licensing please contact a Microsoft sales partner or licensing expert.
    Call (1-800-426-9400), Monday through Friday, 6:00 AM to 6:00 PM PST to speak directly to Microsoft licensing specialist.
    http://www.microsoft.com/en-us/server-cloud/products/sql-server/buy.aspx
    T-SQL Articles
    T-SQL e-book by TechNet Wiki Community
    T-SQL blog

  • Correct Way to use Bind variables when using an MS SQL server backend

    Hey,
    I'm having some trouble figuring out how to use bind variables in a view when using an MS SQL server backend. For some reason when I'm using an ApplicationModule that has a MS SQL Server JDBC library loaded and I attempt to click OK when editing the following query:
    SELECT kit_status, component_id
    FROM numbered_inv
    WHERE trialname = :1
    I get an error stating that "SQL Query Error Message Incorrect Syntax near ':'". Is JDeveloper compatible with SQL server for bind variables because this query runs just fine if I replace the :1 with a say "Testing testing".
    Thanks in advance
    Edited by: NullCheck on Dec 15, 2010 2:06 PM

    Gotcha. I had tried %1% %0% ?0 and ?1 and various other permutations none of which worked. Got it working by setting the query to:
    SELECT kit_status, component_id
    FROM numbered_inv
    WHERE trialname = ?
    ORDER BY kit_status
    And then adding a bind variable with "Bind position" set to 0.
    Thanks again Shay!

  • [Forum FAQ] How do I create calculated measure using AMO in SQL Server Analysis Services?

    Introduction
    In SQL Server Analysis Services (SSAS), you can create a calculated measure in SQL Server Data Tool (SSDT)/Boniness Integrated Development Studio (BIDS). Sometimes you may need to create calculated measure by using AMO in a C# or VB project.
    In this article, I will demonstrate so how to create calculated measure using AMO in SSAS?
    Prerequisites
    Before create calculated measure using AMO, you need to ensure that the following components were installed in your server.
    The multidimensional database AdventureWorks Multidimensional Model 2012
    A SQL Server with SSIS and SSAS installed
    The AMO libraries installed:
    X86 Package (SQL_AS_AMO.msi)
    X64 Package (SQL_AS_AMO.msi)
    Solution
    Here is the detail steps to create calculated measure using AMO in SSAS.
    Open SSDT and create a new SSIS project.
    Drag Script Task to the design surface.
    Click SSIS-> Variables to open the Variables window and add two variables that used to connect to the server and database.
    Create a connection to connect to SSAS server.
    Rename the connection name to ssas.
    Double click the Script Task to open Script Task Editor.
    Add Connection and Database variables to ReadWriteVariables textbox and then click Edit Script button.
    Add AMO reference in the Solution Explore window.
    Copy the script below and paste it into the script.
    Dim objServer As Server
    Dim objDatabase As Database
    Dim strDataBaseID As String
    Dim objCube As Cube
    Dim objMdxScript As MdxScript
    Dim objCommand As Command
    Dim strCommand As String
    objServer = New Server
    objServer.Connect("localhost")
    objDatabase = objServer.Databases("AdventureWorksDW2012Multidimensional-EE2")
    strDataBaseID = objDatabase.ID
    If objDatabase.Cubes.Count > 0 Then
    objCube = objDatabase.Cubes("Adventure Works")
    If objCube.MdxScripts.Count > 0 Then
    objMdxScript = objCube.MdxScripts("MdxScript")
    objMdxScript = objCube.MdxScripts(0)
    Else
    objCube.MdxScripts.Add("MdxScript", "MdxScript")
    objMdxScript = objCube.MdxScripts("MdxScript")
    End If
    objCommand = New Command
    strCommand = "CREATE MEMBER CURRENTCUBE.[Measures].[Multipy Measures By 3]"
    strCommand = strCommand & " AS [Measures].[Internet Sales Amount] * 3, "
    strCommand = strCommand & " VISIBLE = 1 ; "
    objCommand.Text = strCommand
    objMdxScript.Commands.Add(objCommand)
    objMdxScript.Update()
    objCube.Update()
    End If
    objServer.Disconnect()
    Then you can run this SSIS package to create the calculated measure.
    Applies to
    Microsoft SQL Server 2005
    Microsoft SQL Server 2008
    Microsoft SQL Server 2008 R2
    Microsoft SQL Server 2012
    Please click to vote if the post helps you. This can be beneficial to other community members reading the thread.

    Thanks,
    Is this a supported scenario, or does it use unsupported features?
    For example, can we call exec [ReportServer].dbo.AddEvent @EventType='TimedSubscription', @EventData='b64ce7ec-d598-45cd-bbc2-ea202e0c129d'
    in a supported way?
    Thanks! Josh

  • How to create a crystal Report using C# and SQL Server

    Hi, im new in creating crystal report using SQL Server, and im making a project.. and could someone help me how to connect your .sdf (SQL Server File) from the bin to your crystal report? thanks a lot.. i followed some instructions like this one (https://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?forum=csharpgeneral)
    but i got an error on the adding of server name and database portion.. thanks a lot in advance

    Hello,
    Crystal Reports are supported on
    http://scn.sap.com/community/crystal-reports.
    Karl
    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book:
    Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

  • Is there any way to activate the keep identity setting when using a Transfer SQL Server Object Task?

    Is there any way to add the "Keep Identity" option to the tables selected for transfer in a Transfer SQL Server Object Task?  It seems that would be very useful, yet I can't find an easily available setting for it.  I would prefer to
    accomplish this within the Transfer SQL Server Object Task since it can copy any new fields added to the source tables, as opposed to having to rebuild a custom Data Flow Task any time a table structure is altered.  I thought setting "CopyPrimaryKeys"
    to True would do the trick, but I found that only ensures that the column settings are transferred; when the transfer happens, the destination id column is populated based on the specified seed and increment value, not what's actually in the source table. 
    I'd like to point out that replication isn't really an option here. This is a package that runs a few times a day to keep some data on our website up to date with data in our protected corporate environment.

    IIRW there is FastLoadKeepIdentity specifies
    whether to keep an identity in OLEDB destination 
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • JDeveloper 11.1.1.2.0 - Panel Dashboard using Data from SQL Server 2008

    Hi All,
    I am using JDeveloper 11.1.1.2.0 and am trying to create a cross application dashboard. This dashboard would show 2 panel's of data from an Oracle Database (Already completed this) and two panel's of data from a SQL Server 2008 Database.
    I have successfully installed a SQL Server 2008 database locally on my machine. I have downloaded the JDBC drivers from the Microsoft website and I have two JAR Files:
    * sqljdbc.jar
    * sqljdbc4.jar
    I have created a SQL Server connection within the JDeveloper connections panel and I can successfully query my database.
    I have created a New application module and set its connection to a JDBC connection with the credentials specified within the Connections tab. I then added the two JAR files as libraries to the Model project and
    ran the Application Module tester. This succesfully shows some records.
    Within the View Project I Created a new page and dropped the View Object on as a table. I then go to Run the page and get an error relating to the JDBC drivier not being found. I have read a few posts about adding it to Weblogic lib files and modifying the start up scripts but can anyone give me a definitive working solution? I also tried changing my Application Module to using a DataSource, but I also have the same problem within the Weblogic console (it cannot create a data source for SQL SERVER as it does not have the drivers).
    Any help is greatly appreciated.
    Thanks

    Thanks for the responses.
    I have edited C:\oracle\Middleware\wlserver_10.3\common\bin\commEnv.cmd to add the entries as follows...
    set WEBLOGIC_CLASSPATH=%JAVA_HOME%\lib\tools.jar;%BEA_HOME%\utils\config\10.3\config-launch.jar;%WL_HOME%\server\lib\weblogic_sp.jar;%WL_HOME%\server\lib\weblogic.jar;%FEATURES_DIR%\weblogic.server.modules_10.3.2.0.jar;%WL_HOME%\server\lib\webservices.jar;%ANT_HOME%/lib/ant-all.jar;%ANT_CONTRIB%/lib/ant-contrib.jar;%WL_HOME%\server\lib\sqljdbc.jar;%WL_HOME%\server\lib\sqljdbc4.jar
    You can now see the additional entries at the end of the classpath.
    The Weblogic home is set as follows set WL_HOME=C:\oracle\Middleware\wlserver_10.3
    I have placed the files in C:\oracle\Middleware\wlserver_10.3\server\lib
    I restarted the server. Logged into the Weblogic console and tried to create a datasource with the Microsoft Drivers but still no luck. I gave the Oracle Microsoft Driver a go and it seemed to work! I'm guessing that there is a driver bundle in.

  • Use parameters in SQL Server reporting services with sharepoint lists

    I want to make report from sharepoint 2010 list and forms library in sql server reporting services.
    but also want to apply parameters.
    I tried but parameters but it is not working.
    How I can apply sharepoint lists parameters in sql server reporting services ?
    How to run or view report of sharepoint lists or form library in sqlserver reporting services with required parameters?

    Hi John,
    When we use SharePoint list as datasource in SSRS it has two method to add parameter. Suppose we have a dataset (DataSet1) get data from SharePoint list. Please refer to the methods below.
    Method 1: We can add parameter in the Query Designer dialog box to add parameter.
    Click “Add Filter” icon in the “Applied filters” dialog box in Query Designer.
    Select the corresponding field (Title), fill with specific conditions, and select Parameter option in the end of the row.
    Create a dataset (DataSet2) fill with Title field.
    Double click the parameter we create above, set the available values from DataSet2 field Title.
    (Note: In the situation, we are get the single-value parameter.)
    Method 2: It is a general method to add parameter in SSRS report.
     Create another dataset (DataSet2) in the report fill with Title field.
    Add a parameter (Title) get available values from DataSet2 Title field.
    Add filter in the DataSet1.
    (Note: This method, we can configure multi-values parameter.)
    Reference:
    http://technet.microsoft.com/en-us/library/aa337432(v=sql.105).aspx
    If you have any questions, please feel free to let me know.
    Regards,
    Alisa Tang
    Alisa Tang
    TechNet Community Support

  • Query on text file using Openrowset in Sql server 2012

    Hi,
    I am using below query on Sql server 2008 and sql server 2012.It is working find in 2008 but giving error in 2012.
    below is query and error on sql 2012
    select * from OpenRowset('MSDASQL', 'Driver={Microsoft Access Text Driver (*.txt, *.csv)};DefaultDir=D:\MSSQL;',
     'select * from employee1.txt')
    OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
    Msg 7303, Level 16, State 1, Line 1
    Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)".
    I have read some document on msdn and install microsoft access database engine 2010, but still it not working.
    Can anyone suggest resolution of this problem.
    Thanks
    Shitalkumar kasliwal

    Read this link
    http://calyansql.blogspot.co.il/2013/02/fix-cannot-initialize-data-source.html
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Can I use Built-in SQL Server in Visual Studio 2010 ?

    Hi Friends, I am Atif.
    If I will upload my project to my Server Domain and I want to use Built-in Visual Studio 2010 Sql Server Expreess Database then it will work. If yes then Please suggest me How..??
    Actually I don't want to install SQL Server Database Seperately.
    Tell me How can i achieve this.
    Thanks and Regards.
    Atif.

    Hi Atif,
    Besides other post, to use SQL Server Express for development work, you just need to specify the data source (SQL server's name) and authentication details in a connection string. For more information about SQL Server Express connection string, you can review
    the examples in the following articles.
    Database Connectionstrings
    SQL Server Connection Strings for ASP.NET Web Applications
    Also please note that SQL Server Express edition has some limitations, for example: only one CPU can be used by the express edition, the maximum size of a database is 4 GB (10GB in SQL Server 2008 R2 Express and higher versions of SQL Server Express),
    it can only use up to 1GB of RAM.
    Thanks,
    Lydia Zhang
    Lydia Zhang
    TechNet Community Support

  • Can't use bcp in SQL Server procedure

    I need to export data of sales between the last 4 years to a csv using a stored procedure, however the output of that just gives User
    name not provided, either use -U to provide the username or use -T for Trusted Connection, then it gives the possible parameters to
    give to the command. What am I typing wrong? I've double checked the servername and i'm using -T.
    select
    dateSales, Sales
    into ##temp
    from
    dbo.Sales
    where
    dateSales between convert(varchar(20), year(DATEADD(YEAR, -4, getdate())))+'-01-01' AND getdate();
    select * from ##temp;
    EXEC xp_cmdshell 'bcp "select * from ##temp" queryout "C:\Users\PatriceW\Documents\sales.csv" -c -T -SSQLserver -t, -r\r\n'
    Thank you in advance.

    Unfortunately I can't reproduce it in my local.
    I tried belwo 2 in SSMS and also in Command prompt and both worked -
    EXEC xp_cmdshell 'bcp "select * from ##temp" queryout "C:\temp\sales.csv" -c -T'
    EXEC xp_cmdshell 'bcp "select * from ##temp" queryout "C:\temp\sales.csv" -c -T -S localhost'
    My BCP is inside C:\Program Files\Microsoft SQL Server\100\Tools\Binn.
    Few things to check:
    Ensure if the folder exists and you have permission to write to it. Try giving some local path.
    Try running the bcp from SSMS and CMD both. If it runs from CMD then SQL Server account has no permissions
    Do consider reading Permissions, Remarks section given in below:
     https://msdn.microsoft.com/en-us/library/ms162802.aspx
    -Vaibhav Chaudhari

Maybe you are looking for