sql

Insert converted varchar into datetime sql

帅比萌擦擦* 提交于 2021-02-20 19:53:51
问题 I need to insert a varchar in my table. The type in the table is a datetime so I need to convert it. I didn't think this would be to big of a problem however it keeps inserting 1900-01-01 00:00:00.000 instead of the date I want. When I do a select with my converted date it does show me the correct date. I'll show you the code: INSERT INTO Item (CategoryId, [Date], Content, CreatedOn) SELECT CategoryId, Convert(datetime, '28/11/2012', 103), Content, GetDate() FROM Item i JOIN Category c ON i

Insert converted varchar into datetime sql

大憨熊 提交于 2021-02-20 19:51:09
问题 I need to insert a varchar in my table. The type in the table is a datetime so I need to convert it. I didn't think this would be to big of a problem however it keeps inserting 1900-01-01 00:00:00.000 instead of the date I want. When I do a select with my converted date it does show me the correct date. I'll show you the code: INSERT INTO Item (CategoryId, [Date], Content, CreatedOn) SELECT CategoryId, Convert(datetime, '28/11/2012', 103), Content, GetDate() FROM Item i JOIN Category c ON i

If-else statement in DB2/400

半腔热情 提交于 2021-02-20 19:10:53
问题 I am trying to run an SQL that contains if-else statement in AS400 but it doesn't work. I am creating a View using i Series Navigator in order to run it. SELECT IF FIELD1 IS NOT NULL THEN 'AAA' ELSE 'BBB' END IF FROM LIB.TABLE1 The error I am getting is: SQL State: 42601 Vendor Code: -199 Message: [SQL0199] Keyword IS not expected. Valid tokens: , FROM INTO. Cause . . I tried without writing is null but instead SELECT IF FIELD1 ='' THEN 'AAA' ELSE 'BBB' END IF FROM LIB.TABLE1 then I get the

If-else statement in DB2/400

若如初见. 提交于 2021-02-20 19:09:32
问题 I am trying to run an SQL that contains if-else statement in AS400 but it doesn't work. I am creating a View using i Series Navigator in order to run it. SELECT IF FIELD1 IS NOT NULL THEN 'AAA' ELSE 'BBB' END IF FROM LIB.TABLE1 The error I am getting is: SQL State: 42601 Vendor Code: -199 Message: [SQL0199] Keyword IS not expected. Valid tokens: , FROM INTO. Cause . . I tried without writing is null but instead SELECT IF FIELD1 ='' THEN 'AAA' ELSE 'BBB' END IF FROM LIB.TABLE1 then I get the

How to add rank column?

≡放荡痞女 提交于 2021-02-20 18:56:34
问题 I would like to select records and determine rank number for each similar data. My data is as follows. MEMBER ID | LOAN AMOUNT 1 | 2,000.00 2 | 1,000.00 3 | 4,000.00 4 | 1,000.00 The result I wanted is shown below. RANK|MEMBER ID|LOAN AMOUNT 1 |3 |4,000.00 2 |1 |2,000.00 3 |2 |1,000.00 3 |4 |1,000.00 RANK is a new column. I am using MS SQL server 2008 and created a view table as shown below but it does not resulting to what is wanted. select rank=count(*), s1.MemberID, s1.Loan_Amount from

T-SQL - string concatenation

筅森魡賤 提交于 2021-02-20 18:54:40
问题 Hope someone can help - I am a novice SQL hacker (and very bad at it indeed!) I have two tables on SQL Server 2005 TABLE 1 and TABLE2: TABLE1 COL1 COL2 1 10 2 20 3 30 4 10 4 20 5 20 6 30 7 10 7 20 TABLE2 COL1 COL2 10 A 20 B 30 C COL2 in TABLE2 is a character representation of the numerical data in COL2 TABLE1. I hope this is understandable? I have worked out how to select COL1 and COL2 from TABLE1 and concatenate the results to show this: COL1 COL2Concat 1 10 2 20 3 30 4 10, 20 5 20 6 30 7 10

T-SQL - string concatenation

混江龙づ霸主 提交于 2021-02-20 18:53:50
问题 Hope someone can help - I am a novice SQL hacker (and very bad at it indeed!) I have two tables on SQL Server 2005 TABLE 1 and TABLE2: TABLE1 COL1 COL2 1 10 2 20 3 30 4 10 4 20 5 20 6 30 7 10 7 20 TABLE2 COL1 COL2 10 A 20 B 30 C COL2 in TABLE2 is a character representation of the numerical data in COL2 TABLE1. I hope this is understandable? I have worked out how to select COL1 and COL2 from TABLE1 and concatenate the results to show this: COL1 COL2Concat 1 10 2 20 3 30 4 10, 20 5 20 6 30 7 10

SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

这一生的挚爱 提交于 2021-02-20 13:29:57
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中,经常会遇到需要表复制的情况,如将 一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用 SELECT INTO 和 INSERT INTO SELECT 表复制语句了。 1. INSERT INTO SELECT语句 语句形式为: Insert into Table2(field1,field2,...) select value1,value2,... from Table1 要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下: -- 1.创建测试表 create TABLE Table1 ( a varchar ( 10 ), b varchar ( 10 ), c varchar ( 10 ), CONSTRAINT [ PK_Table1 ] PRIMARY KEY CLUSTERED ( a ASC ) ) ON [ PRIMARY ] create TABLE Table2 ( a varchar ( 10 )

Create database if db not exist

不羁的心 提交于 2021-02-20 08:33:27
问题 I want to make SQL Server scritp for creating database if not exist. IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DataBase') BEGIN CREATE DATABASE DataBase USE DataBase CREATE TABLE TableName ( Id INT PRIMARY KEY IDENTITY (1, 1), Name VARCHAR(100) ) --more tables here --some procedures here too END From code above i getting this error: Msg 911, Level 16, State 1, Line 5 Database 'DataBase' does not exist. Make sure that the name is entered correctly. How to make database with

Create database if db not exist

老子叫甜甜 提交于 2021-02-20 08:31:49
问题 I want to make SQL Server scritp for creating database if not exist. IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DataBase') BEGIN CREATE DATABASE DataBase USE DataBase CREATE TABLE TableName ( Id INT PRIMARY KEY IDENTITY (1, 1), Name VARCHAR(100) ) --more tables here --some procedures here too END From code above i getting this error: Msg 911, Level 16, State 1, Line 5 Database 'DataBase' does not exist. Make sure that the name is entered correctly. How to make database with